import { Collection, Connection } from "home-assistant-js-websocket"; import { useState, useEffect, useMemo } from "react"; import { useDelayedFunction } from "utils/useDelay"; import { useHAConnection } from "./connection"; type _HACollectionState = | { ready: false; collection?: undefined; error?: string; } | { ready: true; collection: T; }; export type HACollectionState = _HACollectionState & { additional: Partial; }; export const useHassCollection = ( getHACollection: (c: Connection) => Collection, updateAdditionalProps?: (n: T | {}) => D ): HACollectionState => { const [state, _setState] = useState<_HACollectionState>({ ready: false, }); const setState = useDelayedFunction(_setState, 500); const conn = useHAConnection(); const collection = useMemo(() => { if (conn.status === "loaded") { return getHACollection(conn.connection); } }, [conn, getHACollection]); useEffect(() => { if (collection) { if (collection.state) { setState({ ready: true, collection: collection.state, }); } const unsub = collection.subscribe((d) => { if (d) { setState({ ready: true, collection: d, }); } }); return () => { unsub(); }; } // eslint-disable-next-line }, [collection, state.ready]); useEffect(() => { if (conn.status === "error") { if (!state.ready && conn.error !== state.error) { setState({ ready: false, error: conn.error, }); } } // eslint-disable-next-line }, [conn, state]); if (state.ready) { if (!state.collection) { throw Error("What?"); } } return { ...state, get additional(): Partial { if (state.ready && updateAdditionalProps) { return updateAdditionalProps(state.collection ?? {}); } return {}; }, }; };