@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
39 lines (38 loc) • 3.91 kB
JavaScript
;const n=require("./Provider-Dxd1Q51u.cjs"),o=require("react"),C=require("@emotion/styled"),b=require("@emotion/react"),q=require("lodash");require("./cjs/utils/light/index.js");const m=require("./cjs/hooks/useLocale/locales/index.js"),L=require("./cjs/HassConnect/HassContext.js");require("@iconify/react");require("use-debounce");const S=require("home-assistant-js-websocket"),E=require("./cjs/hooks/useStore/index.js");function j(){const e=o.useContext(L.HassContext);if(e===void 0||q.isEmpty(e))throw new Error("useHass must be used within a HassProvider, have you wrapped your application in <HassConnect hassUrl={HASS_URL} />?");return e}const d={};function H(e){Object.assign(d,e)}function v(e,i){const{search:t,replace:s,fallback:r}=i??{};return d[e]?typeof t=="string"&&typeof s=="string"?d[e].replace(`${t}`,s).trim():d[e]:r||e}function _(){return d}const $=(e,i)=>{const{fallback:t=v("unknown")}=i??{},[s,r]=o.useState(t),{getConfig:a}=j();return o.useEffect(()=>{(async()=>{const l=(await a())?.language,u=m.default.find(c=>c.code===l);if(u){const c=await u.fetch();r(c[e]??t)}})()},[e,t,a]),s};function w(){const e=E.useStore(s=>s.connection),[i,t]=o.useState(null);return o.useEffect(()=>{if(!e)return;const s=S.subscribeConfig(e,r=>{t(r)});return()=>{s()}},[e]),o.useMemo(()=>i,[i])}function y({locale:e,children:i}){const t=w(),[s,r]=o.useState(!1),a=o.useRef(!1),h=o.useRef(null),l=L.useInternalStore(c=>c.setError),u=L.useInternalStore(c=>c.setLocales);return o.useEffect(()=>{if(!(e??t?.language))return;const p=m.default.find(({code:f})=>f===(e??t?.language));if(h.current!==p?.code&&(r(!1),a.current=!1,l(null)),!p)a.current=!1,l(`Locale "${e??t?.language}" not found, available options are "${m.default.map(({code:f})=>`${f}`).join(", ")}"`);else{if(a.current)return;a.current=!0,h.current=p.code,p.fetch().then(f=>{a.current=!1,r(!0),H(f),u(f)}).catch(f=>{a.current=!1,r(!0),l(`Error retrieving translations from Home Assistant: ${f?.message??f}`)})}},[t,s,u,l,e]),s?i:null}const g=b.keyframes`
0% {stroke-width:0; opacity:0;}
50% {stroke-width:5; opacity:1;}
100% {stroke-width:0; opacity:0;}
`;function k({className:e}){return n.jsx("div",{className:e,children:n.jsxs("svg",{children:[n.jsx("path",{d:"m 12.5,20 15,0 0,0 -15,0 z"}),n.jsx("path",{d:"m 32.5,20 15,0 0,0 -15,0 z"}),n.jsx("path",{d:"m 52.5,20 15,0 0,0 -15,0 z"}),n.jsx("path",{d:"m 72.5,20 15,0 0,0 -15,0 z"})]})})}const z=C(k)`
position: fixed;
inset: 0;
background-color: #1a1a1a;
svg {
position: absolute;
top: 50%;
left: 50%;
width: 6.25em;
height: 3.125em;
margin: -1.562em 0 0 -3.125em;
path {
fill: none;
stroke: #f0c039;
opacity: 0;
}
path:nth-of-type(1) {
animation: ${g} 1s ease-in-out 0s infinite alternate;
}
path:nth-of-type(2) {
animation: ${g} 1s ease-in-out 0.1s infinite alternate;
}
path:nth-of-type(3) {
animation: ${g} 1s ease-in-out 0.2s infinite alternate;
}
path:nth-of-type(4) {
animation: ${g} 1s ease-in-out 0.3s infinite alternate;
}
}
`,x=C.div`
width: 100%;
height: 100%;
`,A=o.memo(function({children:i,hassUrl:t,hassToken:s,loading:r=n.jsx(z,{}),onReady:a,options:h={}}){const l=o.useRef(!1),u=o.useMemo(()=>{try{return new URL(t).origin}catch(c){return console.log("Error:",c),null}},[t]);return!u||u==="null"||u===null?n.jsx(n.Fragment,{children:"Provide the hassUrl prop with a valid url to your home assistant instance."}):n.jsx(n.HassProvider,{hassUrl:u,hassToken:s,...h,children:c=>n.jsx(n.Fragment,{children:c?n.jsx(x,{children:n.jsxs(y,{locale:h.locale,children:[a&&!l.current&&(a(),l.current=!0,null),i]})}):n.jsx(x,{children:r})})})});exports.FetchLocale=y;exports.HassConnect=A;exports.localize=v;exports.updateLocales=H;exports.useConfig=w;exports.useHass=j;exports.useLocale=$;exports.useLocales=_;
//# sourceMappingURL=index-DKqwfDib.cjs.map