@hakit/core
Version:
A collection of React hooks and helpers for Home Assistant to easily communicate with the Home Assistant WebSocket API.
48 lines (47 loc) • 1.32 kB
JavaScript
import { useState as f, useRef as b, useMemo as d, useEffect as h } from "react";
import "lodash";
import { X as E } from "../../../index-BDZyTvOW.js";
import "../useLocale/locales/index.js";
import "home-assistant-js-websocket";
import "../../utils/date.js";
import "@iconify/react";
import "use-debounce";
const z = (s) => {
const n = E((o) => o.connection), [i, e] = f(null), t = b(null), c = d(() => s, [s]);
return h(() => {
const o = () => {
if (t.current)
try {
t.current();
} catch {
} finally {
t.current = null;
}
}, { enabled: u = !0, ...m } = c;
if (!u || !n) {
o(), u || e(null);
return;
}
const a = (r) => {
if ("error" in r) {
console.error("Error processing template:", r.error), e(r.error);
return;
}
e((l) => l === r.result ? l : r.result);
}, p = (r) => {
console.error("Error processing template:", r), e(r?.error || "Could not process template request.");
};
return n.subscribeMessage(a, {
type: "render_template",
...m
}).then((r) => {
t.current = r;
}).catch((r) => {
console.error("Error subscribing to template:", r), p(r);
}), o;
}, [n, c]), i;
};
export {
z as useTemplate
};
//# sourceMappingURL=index.js.map