input entities

This commit is contained in:
ari committed 2022-05-30 15:05:54 -04:00
1 parent 878035e748
commit 996626f9af
27 files changed
+596 -92

No files matched your search

+18 -4
View File
@@ -9,7 +9,10 @@ export type HACollectionState<T> = {
ready: true,
collection: Collection<T>,
}
export const useHassCollection = <T>(getHACollection: (c: Connection) => Collection<T>) => {
export const useHassCollection = <T, D extends {} = {}>(
getHACollection: (c: Connection) => Collection<T>,
updateAdditionalProps?: (n: T | {}) => D,
) => {
const [state, setState] = useState<HACollectionState<T>>({
ready: false
});
@@ -22,21 +25,32 @@ export const useHassCollection = <T>(getHACollection: (c: Connection) => Collect
ready: false,
collection,
})
return collection.subscribe(d => {
const unsub = collection.subscribe(d => {
if (d && !state.ready) {
setState({
ready: true,
collection,
})
unsub();
} else if (!d && state.ready) {
setState({
ready: true,
collection,
})
unsub();
}
})
});
return unsub
}
}, [conn, getHACollection, state.ready]);
return state;
return {
...state,
get additional(): Partial<D> {
if (state.ready && updateAdditionalProps) {
return updateAdditionalProps(state.collection.state)
}
return {}
}
}
}