@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.89 kB
JavaScript
;const n=require("./Provider-CYRAeT2G.cjs"),r=require("react"),j=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");require("deep-object-diff");const S=require("home-assistant-js-websocket");function x(){const e=r.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,c){const{search:t,replace:i,fallback:s}=c??{};return d[e]?typeof t=="string"&&typeof i=="string"?d[e].replace(`${t}`,i).trim():d[e]:s||e}function E(){return d}const $=(e,c)=>{const{fallback:t=v("unknown")}=c??{},[i,s]=r.useState(t),{getConfig:o}=x();return r.useEffect(()=>{(async()=>{const l=(await o())?.language,u=m.default.find(a=>a.code===l);if(u){const a=await u.fetch();s(a[e]??t)}})()},[e,t,o]),i};function w(){const{useStore:e}=x(),c=e(s=>s.connection),[t,i]=r.useState(null);return r.useEffect(()=>{if(!c)return;const s=S.subscribeConfig(c,o=>{i(o)});return()=>{s()}},[c]),r.useMemo(()=>t,[t])}function y({locale:e,children:c}){const t=w(),[i,s]=r.useState(!1),o=r.useRef(!1),h=r.useRef(null),l=L.useStore(a=>a.setError),u=L.useStore(a=>a.setLocales);return r.useEffect(()=>{if(!(e??t?.language))return;const p=m.default.find(({code:f})=>f===(e??t?.language));if(h.current!==p?.code&&(s(!1),o.current=!1,l(null)),!p)o.current=!1,l(`Locale "${e??t?.language}" not found, available options are "${m.default.map(({code:f})=>`${f}`).join(", ")}"`);else{if(o.current)return;o.current=!0,h.current=p.code,p.fetch().then(f=>{o.current=!1,s(!0),H(f),u(f)}).catch(f=>{o.current=!1,s(!0),l(`Error retrieving translations from Home Assistant: ${f?.message??f}`)})}},[t,i,u,l,e]),i?c:null}const g=b.keyframes`
0% {stroke-width:0; opacity:0;}
50% {stroke-width:5; opacity:1;}
100% {stroke-width:0; opacity:0;}
`;function _({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 k=j(_)`
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;
}
}
`,C=j.div`
width: 100%;
height: 100%;
`,z=r.memo(function({children:c,hassUrl:t,hassToken:i,loading:s=n.jsx(k,{}),onReady:o,options:h={}}){const l=r.useRef(!1),u=r.useMemo(()=>{try{return new URL(t).origin}catch(a){return console.log("Error:",a),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:i,...h,children:a=>n.jsx(n.Fragment,{children:a?n.jsx(C,{children:n.jsxs(y,{locale:h.locale,children:[o&&!l.current&&(o(),l.current=!0,null),c]})}):n.jsx(C,{children:s})})})});exports.FetchLocale=y;exports.HassConnect=z;exports.localize=v;exports.updateLocales=H;exports.useConfig=w;exports.useHass=x;exports.useLocale=$;exports.useLocales=E;
//# sourceMappingURL=index-CUxh5LWn.cjs.map