@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.06 kB
JavaScript
import { useRef as C, useState as h, useMemo as g, useEffect as y } from "react";
import { useSubscribeEntity as O } from "../useSubscribeEntity/index.js";
import { useStore as b } from "../useStore/index.js";
import { subscribeHistory as R, computeHistory as w } from "./history.js";
import { coordinatesMinimalResponseCompressedState as T } from "./coordinates.js";
const F = (t, e) => {
const c = b((i) => i.connection), l = b((i) => i.config), o = C(!1), u = O(t), [r, d] = h({}), [m, S] = h({
loading: !e?.disable,
timeline: [],
entityHistory: [],
coordinates: []
}), s = g(() => ({
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 y(() => {
if (!c || s?.disable) return;
let i = !0, n = null;
return R({
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]), y(() => {
if (!s?.disable && o.current) {
const i = u(!0);
if (!i) return;
const n = {
[t]: i
}, f = w(l, n, r).timeline.filter(({ entity_id: p }) => p === t), H = T(
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: f.length > 0 ? f[0].data : [],
entityHistory: r[t],
coordinates: H
});
}
}, [t, l, s, u, r]), g(() => m, [m]);
};
export {
F as useHistory
};
//# sourceMappingURL=index.js.map