added delay to save

This commit is contained in:
ari committed 2022-07-29 21:42:50 -04:00
1 parent 957b6a66fe
commit 067f5fa642
4 files changed
+44 -23

No files matched your search

+6 -2
View File
@@ -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",
@@ -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<AutomationManagerProps> = ({
const snackbr = useSnackbar();
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
refreshAutomations();
// eslint-disable-next-line
}, []);
// handle bad states
if (!haEntities.ready && haEntities.error) {
return (
+9 -1
View File
@@ -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<T> =
@@ -19,9 +20,10 @@ export const useHassCollection = <T, D extends {} = {}>(
getHACollection: (c: Connection) => Collection<T>,
updateAdditionalProps?: (n: T | {}) => D
): HACollectionState<T, D> => {
const [state, setState] = useState<_HACollectionState<T>>({
const [state, _setState] = useState<_HACollectionState<T>>({
ready: false,
});
const setState = useDelayedFunction(_setState, 500);
const conn = useHAConnection();
const collection = useMemo(() => {
if (conn.status === "loaded") {
@@ -39,6 +41,12 @@ export const useHassCollection = <T, D extends {} = {}>(
}
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,
+23 -19
View File
@@ -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 = <FcnParams extends any[]>(
cb: (...p: FcnParams) => void, timeout: number
cb: (...p: FcnParams) => void,
timeout: number
) => {
const timeid = useRef<number | null>(null);
return (...args: FcnParams) => {
if (timeid.current) {
window.clearTimeout(timeid.current)
}
timeid.current = window.setTimeout(() => {
cb(...args)
}, timeout)
const timeid = useRef<number | null>(null);
return (...args: FcnParams) => {
if (timeid.current) {
window.clearTimeout(timeid.current);
}
}
timeid.current = window.setTimeout(() => {
cb(...args);
}, timeout);
};
};