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