From 96617e7b0cc2300abb470fca205e0397aae2f37c Mon Sep 17 00:00:00 2001 From: Ari Date: Thu, 28 Jul 2022 15:43:00 -0400 Subject: [PATCH] v1 of loaded manager --- webapp/src/apiService/index.ts | 34 +++++----- webapp/src/apiService/mock.ts | 9 ++- .../AutomationManager/ListBoxGroup/index.tsx | 11 ++-- .../AutomationManager/Loaded/index.css | 4 ++ .../AutomationManager/Loaded/index.tsx | 57 ++++++++++++++--- .../AutomationManager/MetadataBox/index.tsx | 4 +- .../AutomationManager/Sidebar/index.css | 21 +++++++ .../Sidebar/index.stories.tsx | 14 +++-- .../AutomationManager/Sidebar/index.tsx | 32 +++++++--- .../src/components/AutomationManager/TagDB.ts | 18 ++++-- .../automationGrouper.test.ts | 8 +-- .../AutomationManager/automationGrouper.tsx | 8 +-- .../components/AutomationManager/helpers.ts | 10 +-- .../components/AutomationManager/index.css | 26 ++++++++ .../AutomationManager/index.stories.tsx | 63 ++++++++++++++++++- .../components/AutomationManager/index.tsx | 54 ++++++++++------ .../src/components/AutomationManager/types.ts | 10 +-- 17 files changed, 295 insertions(+), 88 deletions(-) create mode 100644 webapp/src/components/AutomationManager/Loaded/index.css create mode 100644 webapp/src/components/AutomationManager/index.css diff --git a/webapp/src/apiService/index.ts b/webapp/src/apiService/index.ts index 2330997..2414db4 100644 --- a/webapp/src/apiService/index.ts +++ b/webapp/src/apiService/index.ts @@ -1,22 +1,24 @@ -import { AutomationData } from 'types/automations'; -import { makeAutomationAPI } from './automations'; +import { AutomationData } from "types/automations"; +import { makeAutomationAPI } from "./automations"; import { makeRemoteAPI } from "./base"; -import { useAPIService } from './core'; +import { useAPIService } from "./core"; import { useMockAPI } from "./mock"; +import { useRef } from "react"; const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i); const baseURL = new URL( - process.env.NODE_ENV === 'development' ? - `http://${window.location.hostname}:8000` : - window.location.origin + (!locationPrefixWeb ? '' : locationPrefixWeb[1]) -); -export const wsURL = baseURL + "socket" -export const remoteAutoAPI = makeRemoteAPI( - baseURL + "automations" -); -export const useConnectedApiService = () => useAPIService( - makeAutomationAPI(remoteAutoAPI) -); -export const useMockApiService = (initialAutos: AutomationData[]) => useAPIService( - makeAutomationAPI(useMockAPI(initialAutos)) + process.env.NODE_ENV === "development" + ? `http://${window.location.hostname}:8000` + : window.location.origin + (!locationPrefixWeb ? "" : locationPrefixWeb[1]) ); +export const wsURL = baseURL + "socket"; +export const remoteAutoAPI = makeRemoteAPI(baseURL + "automations"); +export const useConnectedApiService = () => + useAPIService(makeAutomationAPI(remoteAutoAPI)); +export const useMockApiService = ( + initialAutos: AutomationData[], + returnErrors: boolean = false +) => + useAPIService( + makeAutomationAPI(useMockAPI(initialAutos, useRef, returnErrors)) + ); diff --git a/webapp/src/apiService/mock.ts b/webapp/src/apiService/mock.ts index eba7e95..d1f88f0 100644 --- a/webapp/src/apiService/mock.ts +++ b/webapp/src/apiService/mock.ts @@ -9,11 +9,18 @@ import { export const useMockAPI = ( initialAutos: AutomationData[] = [], - makeRef = useRef + makeRef = useRef, + returnErrors: boolean = false ): API => { const automationsRef = makeRef(initialAutos); return { async makeCall({ path, method = "POST", data = {} }) { + if (returnErrors) { + return { + ok: false, + error: "Test Error", + }; + } // console.debug("[[mockCall]]", method, path, data); if (path === AUTOMTAION_LIST) { return { diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx b/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx index ae518e7..9f8b017 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx @@ -2,12 +2,15 @@ import "./index.css"; import { FC, useState } from "react"; import { MetadataBox } from "../MetadataBox"; -import { AutomationListAutoUpdatable, AutomationListItem } from "../types"; +import { + AutomationManagerAutoUpdatable, + AutomationManagerItem, +} from "../types"; import { TagDB } from "../TagDB"; -export type ListBoxGroupProps = AutomationListItem & { +export type ListBoxGroupProps = AutomationManagerItem & { onAutomationUpdate: ( - a: AutomationListAutoUpdatable, + a: AutomationManagerAutoUpdatable, aid: string, eid: string ) => void; @@ -103,7 +106,7 @@ export const ListBoxGroup: FC = ({ ); }; -const countAutomations = (item: AutomationListItem): number => { +const countAutomations = (item: AutomationManagerItem): number => { if (item.type === "items") { return item.data.length; } else { diff --git a/webapp/src/components/AutomationManager/Loaded/index.css b/webapp/src/components/AutomationManager/Loaded/index.css new file mode 100644 index 0000000..7034749 --- /dev/null +++ b/webapp/src/components/AutomationManager/Loaded/index.css @@ -0,0 +1,4 @@ +.automation-manager--editor { + height: 100%; + width: 100%; +} \ No newline at end of file diff --git a/webapp/src/components/AutomationManager/Loaded/index.tsx b/webapp/src/components/AutomationManager/Loaded/index.tsx index 7cb3d2b..977c51a 100644 --- a/webapp/src/components/AutomationManager/Loaded/index.tsx +++ b/webapp/src/components/AutomationManager/Loaded/index.tsx @@ -1,20 +1,24 @@ +import "./index.css"; + +import { AutomationEditor } from "components/AutomationEditor"; +import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; import { HassEntities } from "home-assistant-js-websocket"; -import { FC } from "react"; +import { FC, PropsWithChildren } from "react"; import { AutomationData } from "types/automations"; import { consolidateAutomations } from "../helpers"; +import { AutomationManagerSidebar } from "../Sidebar"; import { useTagDB } from "../TagDB"; import { useAutomationManagerState } from "./state"; +import Alert from "@mui/material/Alert"; export type AutomationManagerLoadedProps = { configAutomations: AutomationData[]; hassEntities: HassEntities; onUpdateTags: (aid: string, tags: Record) => void; }; -export const AutomationManagerLoaded: FC = ({ - configAutomations, - onUpdateTags, - hassEntities, -}) => { +export const AutomationManagerLoaded: FC< + PropsWithChildren +> = ({ configAutomations, onUpdateTags, hassEntities, children }) => { const state = useAutomationManagerState(); const tagsDB = useTagDB( configAutomations.map(({ metadata: { id }, tags }) => ({ @@ -29,6 +33,45 @@ export const AutomationManagerLoaded: FC = ({ configAutomations.map(({ metadata }) => metadata), tagsDB ); + const currentAutomation = configAutomations.filter( + ({ metadata: { id } }) => state.current === id + ); - return <>; + return ( +
+ {children} + {}} + onAutomationDelete={() => {}} + onAutomationUpdate={() => {}} + /> +
+ {state.current !== null ? ( + currentAutomation.length > 0 ? ( + {}} + tagDB={tagsDB} + /> + ) : ( +
+ + Failed to find an automation on disk with id {state.current}. + +
+ ) + ) : ( + <> + )} +
+
+ ); }; diff --git a/webapp/src/components/AutomationManager/MetadataBox/index.tsx b/webapp/src/components/AutomationManager/MetadataBox/index.tsx index d508bd0..94e78b1 100644 --- a/webapp/src/components/AutomationManager/MetadataBox/index.tsx +++ b/webapp/src/components/AutomationManager/MetadataBox/index.tsx @@ -10,12 +10,12 @@ import SelectIcon from "@mui/icons-material/HighlightAltSharp"; import { InputTextView } from "components/Inputs/InputTextView"; import { ButtonIcon } from "components/Icons/ButtonIcons"; -import { AutomationListAuto } from "../types"; +import { AutomationManagerAuto } from "../types"; import { Tags } from "./Tags"; import { TagDB } from "../TagDB"; import useWindowSize from "utils/useWindowSize"; -export type MetadataBoxProps = AutomationListAuto & { +export type MetadataBoxProps = AutomationManagerAuto & { onDelete: () => void; onSelect: () => void; onStateUpdate: (s: "on" | "off") => void; diff --git a/webapp/src/components/AutomationManager/Sidebar/index.css b/webapp/src/components/AutomationManager/Sidebar/index.css index 84cf780..d7f7769 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.css +++ b/webapp/src/components/AutomationManager/Sidebar/index.css @@ -25,4 +25,25 @@ flex-direction: row; justify-content: center; align-items: center; + position: relative; +} + +.automation-manager--sidebar-box--bottom-buttons .hide-btn { + position: absolute; + right: 0; +} + +.automation-manager--sidebar.hidden { + width: 2.5em; +} + +.automation-manager--sidebar.hidden .automation-manager--sidebar-box--nav, +.automation-manager--sidebar.hidden .automation-manager--sidebar-box--list, +.automation-manager--sidebar.hidden .automation-manager--sidebar-box--bottom-buttons .add-btn { + width: 0; + opacity: 0; +} + +.automation-manager--sidebar.hidden .automation-manager--sidebar-box--bottom-buttons .hide-btn { + transform: rotate(180deg); } \ No newline at end of file diff --git a/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx b/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx index 5800243..009de10 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx +++ b/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx @@ -1,7 +1,7 @@ -import { AutomationListSidebar } from "."; +import { AutomationManagerSidebar } from "."; import { useTagDB } from "../TagDB"; import { useState } from "react"; -import { AutomationListAuto } from "../types"; +import { AutomationManagerAuto } from "../types"; import { makeStory } from "devUtils"; @@ -9,7 +9,7 @@ const story = makeStory({ meta: { title: "App/AutomationManager/Sidebar", }, - Component: AutomationListSidebar, + Component: AutomationManagerSidebar, BaseTemplate: (args) => { const [automations, setAutomations] = useState(args.automations); const [selectedAutomationId, setSelAutoId] = useState( @@ -29,7 +29,7 @@ const story = makeStory({ } }; return ( - onTagUpdate(t, a))} automations={automations} @@ -78,11 +78,13 @@ const story = makeStory({ export default story.componentMeta; -const make = (automations: AutomationListAuto[]) => +const make = (automations: AutomationManagerAuto[]) => story.make({ automations, }); -const makeAuto = (data: Partial): AutomationListAuto => { +const makeAuto = ( + data: Partial +): AutomationManagerAuto => { const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`; return { id, diff --git a/webapp/src/components/AutomationManager/Sidebar/index.tsx b/webapp/src/components/AutomationManager/Sidebar/index.tsx index 3f73524..17513c0 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.tsx +++ b/webapp/src/components/AutomationManager/Sidebar/index.tsx @@ -2,6 +2,8 @@ import "./index.css"; import { FC, useState } from "react"; +import ArrowIcon from "@mui/icons-material/ArrowBackIosNewOutlined"; + import InputText from "components/Inputs/InputText"; import InputMultiSelect from "components/Inputs/InputMultiSelect"; @@ -9,15 +11,19 @@ import { ListBoxGroup } from "../ListBoxGroup"; import { filterAutomations } from "../helpers"; import { TagDB } from "../TagDB"; import { convertGroupingToItems, makeGrouping } from "../automationGrouper"; -import { AutomationListAuto, AutomationListAutoUpdatable } from "../types"; +import { + AutomationManagerAuto, + AutomationManagerAutoUpdatable, +} from "../types"; import { Button } from "components/Inputs/Button"; +import { ButtonIcon } from "components/Icons/ButtonIcons"; -export type AutomationListSidebarProps = { +export type AutomationManagerSidebarProps = { tagsDB: TagDB; - automations: AutomationListAuto[]; + automations: AutomationManagerAuto[]; selectedAutomationId: string | null; onAutomationUpdate: ( - a: AutomationListAutoUpdatable, + a: AutomationManagerAutoUpdatable, aid: string, eid: string ) => void; @@ -25,7 +31,7 @@ export type AutomationListSidebarProps = { onAutomationAdd: () => void; onSelectedAutomationId: (aid: string) => void; }; -export const AutomationListSidebar: FC = ({ +export const AutomationManagerSidebar: FC = ({ automations, tagsDB, onAutomationAdd, @@ -35,6 +41,7 @@ export const AutomationListSidebar: FC = ({ }) => { const [searchText, setSearchText] = useState(""); const [selectedTagIdx, setSelectedTagIdx] = useState([]); + const [isHidden, setHidden] = useState(false); const tags = tagsDB.getTagNames([]); @@ -56,7 +63,12 @@ export const AutomationListSidebar: FC = ({ ); return ( -
+
= ({ ))}
- + } + onClick={() => setHidden(!isHidden)} + />
); diff --git a/webapp/src/components/AutomationManager/TagDB.ts b/webapp/src/components/AutomationManager/TagDB.ts index 39a971b..63c00f7 100644 --- a/webapp/src/components/AutomationManager/TagDB.ts +++ b/webapp/src/components/AutomationManager/TagDB.ts @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; export type TagDB = ReturnType; export const useTagDB = ( automations: Array<{ @@ -7,6 +7,7 @@ export const useTagDB = ( }>, onUpdate: (aid: string, tags: Record) => void ) => { + const lastAuto = useRef(JSON.stringify(automations)); const [{ automationTags, changed }, setState] = useState({ automationTags: convertAutoListToMap(automations), changed: new Set(), @@ -16,11 +17,16 @@ export const useTagDB = ( const isModified = changed.size > 0; useEffect(() => { - console.log("updating..."); - setState({ - automationTags: convertAutoListToMap(automations), - changed: new Set(), - }); + const c = JSON.stringify(automations); + if (lastAuto.current !== c) { + lastAuto.current = c; + console.log("updating..."); + + setState({ + automationTags: convertAutoListToMap(automations), + changed: new Set(), + }); + } }, [automations, setState]); return { diff --git a/webapp/src/components/AutomationManager/automationGrouper.test.ts b/webapp/src/components/AutomationManager/automationGrouper.test.ts index 994bbd5..bffe0bf 100644 --- a/webapp/src/components/AutomationManager/automationGrouper.test.ts +++ b/webapp/src/components/AutomationManager/automationGrouper.test.ts @@ -1,8 +1,8 @@ import { convertGroupingToItems, makeGrouping } from "./automationGrouper"; import { bigMockAutoList, createMockAuto } from "utils/mocks"; -import { AutomationListAuto } from "./types"; +import { AutomationManagerAuto } from "./types"; -const automations: AutomationListAuto[] = [ +const automations: AutomationManagerAuto[] = [ createMockAuto({ Room: "Living Room", Type: "Lights", @@ -22,7 +22,7 @@ const automations: AutomationListAuto[] = [ Room: "Living Room", }), createMockAuto({}), -].map((a) => ({ +].map((a) => ({ title: a.metadata.alias ?? "", entityId: "automation." + a.metadata.id, description: a.metadata.description ?? "", @@ -32,7 +32,7 @@ const automations: AutomationListAuto[] = [ isSelected: false, })); -const bigList = bigMockAutoList.map((a) => ({ +const bigList = bigMockAutoList.map((a) => ({ entityId: "automation." + a.metadata.id, title: a.metadata.alias ?? "", description: a.metadata.description ?? "", diff --git a/webapp/src/components/AutomationManager/automationGrouper.tsx b/webapp/src/components/AutomationManager/automationGrouper.tsx index 98aa699..3d8de4d 100644 --- a/webapp/src/components/AutomationManager/automationGrouper.tsx +++ b/webapp/src/components/AutomationManager/automationGrouper.tsx @@ -1,4 +1,4 @@ -import { AutomationListAuto, AutomationListItem } from "./types"; +import { AutomationManagerAuto, AutomationManagerItem } from "./types"; const makeGrouper = () => { const groupIdNames: string[] = []; @@ -63,7 +63,7 @@ const makeGrouper = () => { export type AutomationGrouping = ReturnType; export const makeGrouping = ( - autos: AutomationListAuto[], + autos: AutomationManagerAuto[], selectedTags: string[], selectedAutomationId: string | null ) => { @@ -150,9 +150,9 @@ export const makeGrouping = ( export const convertGroupingToItems = ( currentGroupIdx: number, selectedAutomationId: string | null, - autos: AutomationListAuto[], + autos: AutomationManagerAuto[], grouping: AutomationGrouping -): AutomationListItem => { +): AutomationManagerItem => { const groupData = grouping.getData(currentGroupIdx); const automationIds = grouping.getAutomations(currentGroupIdx); diff --git a/webapp/src/components/AutomationManager/helpers.ts b/webapp/src/components/AutomationManager/helpers.ts index cd562e7..7c4d5b6 100644 --- a/webapp/src/components/AutomationManager/helpers.ts +++ b/webapp/src/components/AutomationManager/helpers.ts @@ -1,13 +1,13 @@ import { HassEntities } from "home-assistant-js-websocket"; import { AutomationMetadata } from "types/automations"; import { TagDB } from "./TagDB"; -import { AutomationListAuto } from "./types"; +import { AutomationManagerAuto } from "./types"; export const consolidateAutomations = ( haEntities: HassEntities, configAutomationMetadatas: AutomationMetadata[], tagsDB: TagDB -): AutomationListAuto[] => { +): AutomationManagerAuto[] => { const configData: Record = configAutomationMetadatas.reduce( (all, n) => ({ @@ -19,7 +19,7 @@ export const consolidateAutomations = ( const out = Object.entries(haEntities) .filter(([entityId, _]) => entityId.toLowerCase().startsWith("automation.")) - .map(([entityId, entityData]) => { + .map(([entityId, entityData]) => { const autoId = entityData.attributes.id; let title = entityData.attributes.friendly_name ?? ""; let description = ""; @@ -64,9 +64,9 @@ export const consolidateAutomations = ( }; export const filterAutomations = ( - autos: AutomationListAuto[], + autos: AutomationManagerAuto[], searchText: string -): AutomationListAuto[] => +): AutomationManagerAuto[] => autos.filter((a) => { if (searchText.length > 0) { if ( diff --git a/webapp/src/components/AutomationManager/index.css b/webapp/src/components/AutomationManager/index.css new file mode 100644 index 0000000..19058c3 --- /dev/null +++ b/webapp/src/components/AutomationManager/index.css @@ -0,0 +1,26 @@ +.automation-manager { + display: flex; + flex-direction: row; + height: 100%; + width: 100%; + gap: 0.25em; +} + +.automation-manager.mobile, .automation-manager.error { + flex-direction: column; +} + +.automation-manager.loading .automation-manager--sidebar.loading { + width: 25vh; + height: 100%; +} + +.automation-manager.loading .MuiSkeleton-root { + height: 100%; + width: 100%; +} + +.automation-manager.loading .automation-manager--editor.loading { + flex: 1; + height: 100%; +} \ No newline at end of file diff --git a/webapp/src/components/AutomationManager/index.stories.tsx b/webapp/src/components/AutomationManager/index.stories.tsx index f4d37e0..06dd73f 100644 --- a/webapp/src/components/AutomationManager/index.stories.tsx +++ b/webapp/src/components/AutomationManager/index.stories.tsx @@ -1,12 +1,71 @@ import { makeStory } from "devUtils"; +import { useMockApiService } from "apiService"; import { AutomationManager } from "."; +import { + MockHAEntitiesProps, + useMockHAEntities, +} from "haService/HAEntities.mock"; +import { AutomationData } from "types/automations"; +import { createMockAuto } from "utils/mocks"; const { make, componentMeta } = makeStory({ - Component: AutomationManager, + Component: (args: { + automations?: AutomationData[]; + haProps: MockHAEntitiesProps; + }) => ( + + ), meta: { title: "App/AutomationManager", + args: { + automations: [], + haProps: { + loading: true, + }, + }, }, }); export default componentMeta; -export const Basic = make({}); +export const Loading = make({}); +export const Errored = make({ + automations: undefined, + haProps: { + loading: false, + entities: {}, + entitySource: {}, + }, +}); +export const Empty = make({ + automations: [], + haProps: { + loading: false, + entities: {}, + entitySource: {}, + }, +}); +const auto1 = createMockAuto({ + type: "button", +}); +export const One = make({ + automations: [auto1], + haProps: { + loading: false, + entities: { + [`automation.${auto1.metadata.id}`]: { + entity_id: `automation.${auto1.metadata.id}`, + attributes: { + id: auto1.metadata.id, + }, + context: { id: "", parent_id: null, user_id: null }, + last_changed: "", + last_updated: "", + state: "on", + }, + }, + entitySource: {}, + }, +}); diff --git a/webapp/src/components/AutomationManager/index.tsx b/webapp/src/components/AutomationManager/index.tsx index 5de8b8c..e20e856 100644 --- a/webapp/src/components/AutomationManager/index.tsx +++ b/webapp/src/components/AutomationManager/index.tsx @@ -1,8 +1,13 @@ +import "./index.css"; + +import { FC, useState } from "react"; +import LinearProgress from "@mui/material/LinearProgress"; +import Skeleton from "@mui/material/Skeleton"; + import { ApiService } from "apiService/core"; import { HAEntitiesState } from "haService/HAEntities"; -import { FC, useState } from "react"; import { AutomationManagerLoaded } from "./Loaded"; -import LinearProgress from "@mui/material/LinearProgress"; +import { Alert } from "@mui/material"; export type AutomationManagerProps = { haEntites: HAEntitiesState; @@ -17,14 +22,26 @@ export const AutomationManager: FC = ({ // handle bad states if (!haEntites.ready || !api.state.automations.ready) { - return
...
; + return ( +
+
+ +
+
+ +
+
+ ); } if (!api.state.automations.ok) { return (
-

Failed to load

- {api.state.automations.error} + + Failed to load automations from /config + {" "} +
+ {api.state.automations.error}
); } @@ -33,20 +50,19 @@ export const AutomationManager: FC = ({ const configAutomations = api.state.automations.data.data; const hassEntities = haEntites.collection.state; return ( -
+ { + setIsSaving(true); + await api.updateTags({ + automation_id: aid, + tags: t, + }); + setIsSaving(false); + }} + > {isSaving && } - { - setIsSaving(true); - await api.updateTags({ - automation_id: aid, - tags: t, - }); - setIsSaving(false); - }} - /> -
+ ); }; diff --git a/webapp/src/components/AutomationManager/types.ts b/webapp/src/components/AutomationManager/types.ts index fcd45ab..92b4299 100644 --- a/webapp/src/components/AutomationManager/types.ts +++ b/webapp/src/components/AutomationManager/types.ts @@ -1,25 +1,25 @@ -export type AutomationListAutoUpdatable = { +export type AutomationManagerAutoUpdatable = { title: string; description: string; state: "on" | "off" | string; }; -export type AutomationListAuto = AutomationListAutoUpdatable & { +export type AutomationManagerAuto = AutomationManagerAutoUpdatable & { id: string; entityId: string; tags: Record; issue?: string; }; -export type AutomationListItem = { +export type AutomationManagerItem = { title: string; isSelected: boolean; } & ( | { type: "group"; - data: Array; + data: Array; } | { type: "items"; - data: Array; + data: Array; } );