@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
59 lines (58 loc) • 1.93 kB
JavaScript
import { createContext as i } from "react";
import { create as u } from "zustand";
const d = (t, n) => {
const { last_changed: e, last_updated: s, context: c, ...o } = t, { last_changed: l, last_updated: a, context: C, ...r } = n;
return JSON.stringify(o) === JSON.stringify(r);
}, h = u((t) => ({
routes: [],
setRoutes: (n) => t(() => ({ routes: n })),
entities: {},
setHassUrl: (n) => t({ hassUrl: n }),
hassUrl: null,
hash: "",
locales: null,
setLocales: (n) => t({ locales: n }),
setHash: (n) => t({ hash: n }),
setPortalRoot: (n) => t({ portalRoot: n }),
windowContext: window,
setWindowContext: (n) => t({ windowContext: n }),
setEntities: (n) => t((e) => {
let s = !1;
const c = { ...e.entities };
for (const [o, l] of Object.entries(n)) {
const a = e.entities[o];
if (!a) {
c[o] = l, s = !0;
continue;
}
d(a, l) || (c[o] = l, s = !0);
}
return s ? { entities: c, lastUpdated: Date.now(), ready: !0 } : e;
}),
connectionStatus: "pending",
setConnectionStatus: (n) => t({ connectionStatus: n }),
connection: null,
setConnection: (n) => t({ connection: n }),
cannotConnect: !1,
setCannotConnect: (n) => t({ cannotConnect: n }),
ready: !1,
setReady: (n) => t({ ready: n }),
auth: null,
setAuth: (n) => t({ auth: n }),
config: null,
setConfig: (n) => t({ config: n }),
user: null,
setUser: (n) => t({ user: n }),
error: null,
setError: (n) => t({ error: n }),
globalComponentStyles: {},
setGlobalComponentStyles: (n) => t(() => ({ globalComponentStyles: n })),
disconnectCallbacks: [],
onDisconnect: (n) => t((e) => ({ disconnectCallbacks: [...e.disconnectCallbacks, n] })),
triggerOnDisconnect: () => t((n) => (n.disconnectCallbacks.forEach((e) => e()), { disconnectCallbacks: [] }))
})), p = i({});
export {
p as HassContext,
h as useInternalStore
};
//# sourceMappingURL=HassContext.js.map