import { Collection, Connection } from "home-assistant-js-websocket"; import { useState, useEffect, useMemo } from "react"; import { useHAConnection } from "./connection"; type _HACollectionState = | { ready: false; collection?: undefined; } | { 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 conn = useHAConnection(); const collection = useMemo(() => { if (conn.status === "loaded") { return getHACollection(conn.connection); } }, [conn.status]); useEffect(() => { if (collection) { setState({ ready: true, collection: collection.state, }); const unsub = collection.subscribe((d) => { setState({ ready: true, collection: d, }); }); return () => { unsub(); }; } }, [collection, state.ready]); return { ...state, get additional(): Partial { if (state.ready && updateAdditionalProps) { return updateAdditionalProps(state.collection ?? {}); } return {}; }, }; };