UNPKG

@hakit/core

Version:

A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.

150 lines (149 loc) 4.7 kB
import { j as s, F as y, a as H, H as E } from "./Provider-fbkyrDDA.js"; import { useContext as S, useState as L, useEffect as w, useMemo as C, useRef as m, memo as x } from "react"; import $ from "@emotion/styled"; import { keyframes as z } from "@emotion/react"; import { isEmpty as j } from "lodash"; import "./es/utils/light/index.js"; import g from "./es/hooks/useLocale/locales/index.js"; import { HassContext as k, useInternalStore as b } from "./es/HassConnect/HassContext.js"; import "@iconify/react"; import "use-debounce"; import { subscribeConfig as A } from "home-assistant-js-websocket"; import { useStore as F } from "./es/hooks/useStore/index.js"; function U() { const t = S(k); if (t === void 0 || j(t)) throw new Error("useHass must be used within a HassProvider, have you wrapped your application in <HassConnect hassUrl={HASS_URL} />?"); return t; } const h = {}; function P(t) { Object.assign(h, t); } function R(t, i) { const { search: e, replace: n, fallback: o } = i ?? {}; return h[t] ? typeof e == "string" && typeof n == "string" ? h[t].replace(`${e}`, n).trim() : h[t] : o || t; } function Y() { return h; } const Z = (t, i) => { const { fallback: e = R("unknown") } = i ?? {}, [n, o] = L(e), { getConfig: r } = U(); return w(() => { (async () => { const l = (await r())?.language, c = g.find((a) => a.code === l); if (c) { const a = await c.fetch(); o(a[t] ?? e); } })(); }, [t, e, r]), n; }; function _() { const t = F((n) => n.connection), [i, e] = L(null); return w(() => { if (!t) return; const n = A(t, (o) => { e(o); }); return () => { n(); }; }, [t]), C(() => i, [i]); } function O({ locale: t, children: i }) { const e = _(), [n, o] = L(!1), r = m(!1), f = m(null), l = b((a) => a.setError), c = b((a) => a.setLocales); return w(() => { if (!(t ?? e?.language)) return; const p = g.find(({ code: u }) => u === (t ?? e?.language)); if (f.current !== p?.code && (o(!1), r.current = !1, l(null)), !p) r.current = !1, l( `Locale "${t ?? e?.language}" not found, available options are "${g.map(({ code: u }) => `${u}`).join(", ")}"` ); else { if (r.current) return; r.current = !0, f.current = p.code, p.fetch().then((u) => { r.current = !1, o(!0), P(u), c(u); }).catch((u) => { r.current = !1, o(!0), l(`Error retrieving translations from Home Assistant: ${u?.message ?? u}`); }); } }, [e, n, c, l, t]), n ? i : null; } const d = z` 0% {stroke-width:0; opacity:0;} 50% {stroke-width:5; opacity:1;} 100% {stroke-width:0; opacity:0;} `; function B({ className: t }) { return /* @__PURE__ */ s("div", { className: t, children: /* @__PURE__ */ H("svg", { children: [ /* @__PURE__ */ s("path", { d: "m 12.5,20 15,0 0,0 -15,0 z" }), /* @__PURE__ */ s("path", { d: "m 32.5,20 15,0 0,0 -15,0 z" }), /* @__PURE__ */ s("path", { d: "m 52.5,20 15,0 0,0 -15,0 z" }), /* @__PURE__ */ s("path", { d: "m 72.5,20 15,0 0,0 -15,0 z" }) ] }) }); } const D = $(B)` 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: ${d} 1s ease-in-out 0s infinite alternate; } path:nth-of-type(2) { animation: ${d} 1s ease-in-out 0.1s infinite alternate; } path:nth-of-type(3) { animation: ${d} 1s ease-in-out 0.2s infinite alternate; } path:nth-of-type(4) { animation: ${d} 1s ease-in-out 0.3s infinite alternate; } } `, v = $.div` width: 100%; height: 100%; `, tt = x(function({ children: i, hassUrl: e, hassToken: n, loading: o = /* @__PURE__ */ s(D, {}), onReady: r, options: f = {} }) { const l = m(!1), c = C(() => { try { return new URL(e).origin; } catch (a) { return console.log("Error:", a), null; } }, [e]); return !c || c === "null" || c === null ? /* @__PURE__ */ s(y, { children: "Provide the hassUrl prop with a valid url to your home assistant instance." }) : /* @__PURE__ */ s(E, { hassUrl: c, hassToken: n, ...f, children: (a) => /* @__PURE__ */ s(y, { children: a ? /* @__PURE__ */ s(v, { children: /* @__PURE__ */ H(O, { locale: f.locale, children: [ r && !l.current && (r(), l.current = !0, null), i ] }) }) : /* @__PURE__ */ s(v, { children: o }) }) }); }); export { O as F, tt as H, _ as a, Z as b, Y as c, P as d, R as l, U as u }; //# sourceMappingURL=index-BfdcdUoC.js.map