diff --git a/webapp/.vscode/tasks.json b/webapp/.vscode/tasks.json index 230d879..ed71309 100644 --- a/webapp/.vscode/tasks.json +++ b/webapp/.vscode/tasks.json @@ -2,13 +2,17 @@ "version": "2.0.0", "tasks": [ { + "icon": { + "id": "server-environment" + }, "type": "shell", - "label": "Docker: Server", + "label": "Docker: Start Server", "group": "none", "command": [".vscode/bin/start-docker-server.sh"], "runOptions": { "runOn": "folderOpen" - } + }, + "problemMatcher": [] }, { "type": "npm", diff --git a/webapp/src/components/AutomationManager/index.tsx b/webapp/src/components/AutomationManager/index.tsx index 8312b2c..5a3a755 100644 --- a/webapp/src/components/AutomationManager/index.tsx +++ b/webapp/src/components/AutomationManager/index.tsx @@ -1,6 +1,6 @@ import "./index.css"; -import { FC, useState } from "react"; +import { FC, useEffect, useState } from "react"; import LinearProgress from "@mui/material/LinearProgress"; import Skeleton from "@mui/material/Skeleton"; @@ -28,6 +28,11 @@ export const AutomationManager: FC = ({ const snackbr = useSnackbar(); const [isSaving, setIsSaving] = useState(false); + useEffect(() => { + refreshAutomations(); + // eslint-disable-next-line + }, []); + // handle bad states if (!haEntities.ready && haEntities.error) { return ( diff --git a/webapp/src/haService/useHassCollection.ts b/webapp/src/haService/useHassCollection.ts index f6f06fd..411f55e 100644 --- a/webapp/src/haService/useHassCollection.ts +++ b/webapp/src/haService/useHassCollection.ts @@ -1,5 +1,6 @@ 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 = @@ -19,9 +20,10 @@ export const useHassCollection = ( getHACollection: (c: Connection) => Collection, updateAdditionalProps?: (n: T | {}) => D ): HACollectionState => { - const [state, setState] = useState<_HACollectionState>({ + const [state, _setState] = useState<_HACollectionState>({ ready: false, }); + const setState = useDelayedFunction(_setState, 500); const conn = useHAConnection(); const collection = useMemo(() => { if (conn.status === "loaded") { @@ -39,6 +41,12 @@ export const useHassCollection = ( } const unsub = collection.subscribe((d) => { if (d) { + console.log( + "updating with", + Object.keys(d).length, + Object.fromEntries(Object.entries(d).map(([k, v]) => [k, v.state])), + getHACollection + ); setState({ ready: true, collection: d, diff --git a/webapp/src/utils/useDelay.ts b/webapp/src/utils/useDelay.ts index ee131f3..d978b21 100644 --- a/webapp/src/utils/useDelay.ts +++ b/webapp/src/utils/useDelay.ts @@ -1,24 +1,28 @@ -import { useRef, useEffect } from 'react'; +import { useRef, useEffect } from "react"; - -export const useDelayEffect = (cb: () => void, deps: any[], timeout: number) => { - const fcn = useDelayedFunction(cb, timeout); - useEffect(() => { - fcn() - // eslint-disable-next-line - }, deps) -} +export const useDelayEffect = ( + cb: () => void, + deps: any[], + timeout: number +) => { + const fcn = useDelayedFunction(cb, timeout); + useEffect(() => { + fcn(); + // eslint-disable-next-line + }, deps); +}; export const useDelayedFunction = ( - cb: (...p: FcnParams) => void, timeout: number + cb: (...p: FcnParams) => void, + timeout: number ) => { - const timeid = useRef(null); - return (...args: FcnParams) => { - if (timeid.current) { - window.clearTimeout(timeid.current) - } - timeid.current = window.setTimeout(() => { - cb(...args) - }, timeout) + const timeid = useRef(null); + return (...args: FcnParams) => { + if (timeid.current) { + window.clearTimeout(timeid.current); } -} \ No newline at end of file + timeid.current = window.setTimeout(() => { + cb(...args); + }, timeout); + }; +};