@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
65 lines (64 loc) • 2.09 kB
JavaScript
import { useRef as C, useState as g, useMemo as y, useEffect as b } from "react";
import { useSubscribeEntity as O } from "../useSubscribeEntity/index.js";
import { u as R } from "../../../index-DnHwXFlq.js";
import { subscribeHistory as w, computeHistory as T } from "./history.js";
import { coordinatesMinimalResponseCompressedState as E } from "./coordinates.js";
const U = (t, e) => {
const { useStore: l } = R(), c = l((i) => i.connection), u = l((i) => i.config), o = C(!1), m = O(t), [r, d] = g({}), [f, S] = g({
loading: !e?.disable,
timeline: [],
entityHistory: [],
coordinates: []
}), s = y(() => ({
disable: e?.disable,
significantChangesOnly: e?.significantChangesOnly,
minimalResponse: e?.minimalResponse,
hoursToShow: e?.hoursToShow,
limits: e?.limits
}), [e?.disable, e?.significantChangesOnly, e?.minimalResponse, e?.hoursToShow, e?.limits]);
return b(() => {
if (!c || s?.disable) return;
let i = !0, n = null;
return w({
connection: c,
entityIds: [t],
significantChangesOnly: s?.significantChangesOnly,
minimalResponse: s?.minimalResponse,
hoursToShow: s?.hoursToShow,
callbackFunction: (a) => {
i && (o.current = !0, d(a));
}
}).then((a) => {
n = a;
}).catch(() => {
n?.(), o.current = !1;
}), () => {
i = !1, n?.(), o.current = !1;
};
}, [t, s, c]), b(() => {
if (!s?.disable && o.current) {
const i = m(!0);
if (!i) return;
const n = {
[t]: i
}, h = T(u, n, r).timeline.filter(({ entity_id: p }) => p === t), H = E(
r[t],
s?.hoursToShow ?? 24,
500,
// viewbox of the svgGraph
typeof s?.significantChangesOnly > "u" || s?.significantChangesOnly === !0 ? 1 : 2,
s?.limits
) ?? [];
S({
loading: !1,
timeline: h.length > 0 ? h[0].data : [],
entityHistory: r[t],
coordinates: H
});
}
}, [t, u, s, m, r]), y(() => f, [f]);
};
export {
U as useHistory
};
//# sourceMappingURL=index.js.map