From 57c86093a489ced2705bedbac8f21d46eb826230 Mon Sep 17 00:00:00 2001 From: Ari Date: Thu, 28 Jul 2022 21:50:23 -0400 Subject: [PATCH] clean up --- api/src/hass_config/__init__.py | 4 +- webapp/src/App.stories.tsx | 18 +- .../AutomationEditor/AutoInfoBox.stories.tsx | 53 ++-- .../AutomationEditor/AutoInfoBox.tsx | 6 +- .../AutomationEditor/index.stories.tsx | 10 +- .../src/components/AutomationEditor/index.tsx | 2 +- .../AutomationList/AutomationList.stories.tsx | 187 ------------- .../AutomationList/AutomationListBox.css | 84 ------ .../AutomationListBox.stories.tsx | 79 ------ .../AutomationList/AutomationListBox.test.ts | 22 -- .../AutomationList/AutomationListBox.tsx | 195 ------------- .../AutomationList/AutomationListBoxGroup.css | 50 ---- .../AutomationList/AutomationListBoxGroup.tsx | 257 ------------------ .../AutomationList/AutomationMetadataBox.css | 101 ------- .../AutomationList/AutomationMetadataBox.tsx | 181 ------------ .../AutomationMetadataBoxTag.stories.tsx | 30 -- .../ConnectedAutomationList.stories.tsx | 63 ----- webapp/src/components/AutomationList/TagDB.ts | 26 -- .../AutomationList/automationGrouper.test.ts | 72 ----- .../AutomationList/automationGrouper.tsx | 130 --------- .../src/components/AutomationList/index.css | 68 ----- .../AutomationList/index.mobile.css | 38 --- .../src/components/AutomationList/index.tsx | 213 --------------- .../AutomationManager/ListBoxGroup/index.tsx | 4 +- .../AutomationManager/Sidebar/index.css | 2 +- webapp/src/haService/HAEntities.ts | 2 + webapp/src/haService/useHassCollection.ts | 113 ++++---- webapp/src/routes/automation.stories.tsx | 16 ++ webapp/src/routes/automation.tsx | 20 +- 29 files changed, 145 insertions(+), 1901 deletions(-) delete mode 100644 webapp/src/components/AutomationList/AutomationList.stories.tsx delete mode 100644 webapp/src/components/AutomationList/AutomationListBox.css delete mode 100644 webapp/src/components/AutomationList/AutomationListBox.stories.tsx delete mode 100644 webapp/src/components/AutomationList/AutomationListBox.test.ts delete mode 100644 webapp/src/components/AutomationList/AutomationListBox.tsx delete mode 100644 webapp/src/components/AutomationList/AutomationListBoxGroup.css delete mode 100644 webapp/src/components/AutomationList/AutomationListBoxGroup.tsx delete mode 100644 webapp/src/components/AutomationList/AutomationMetadataBox.css delete mode 100644 webapp/src/components/AutomationList/AutomationMetadataBox.tsx delete mode 100644 webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx delete mode 100644 webapp/src/components/AutomationList/ConnectedAutomationList.stories.tsx delete mode 100644 webapp/src/components/AutomationList/TagDB.ts delete mode 100644 webapp/src/components/AutomationList/automationGrouper.test.ts delete mode 100644 webapp/src/components/AutomationList/automationGrouper.tsx delete mode 100644 webapp/src/components/AutomationList/index.css delete mode 100644 webapp/src/components/AutomationList/index.mobile.css delete mode 100644 webapp/src/components/AutomationList/index.tsx create mode 100644 webapp/src/routes/automation.stories.tsx diff --git a/api/src/hass_config/__init__.py b/api/src/hass_config/__init__.py index e903dad..231732f 100644 --- a/api/src/hass_config/__init__.py +++ b/api/src/hass_config/__init__.py @@ -11,11 +11,11 @@ def find_hass_config() -> HassConfig: if config("USE_EXAMPLE_REPO", False, cast=bool): print("using USE_EXAMPLE_REPO") from tests.utils import ( - HA_CONFIG3_EXAMPLE, + HA_CONFIG2_EXAMPLE, create_copy_of_example_config, ) - path = create_copy_of_example_config(HA_CONFIG3_EXAMPLE) + path = create_copy_of_example_config(HA_CONFIG2_EXAMPLE) print("|-> ", path) return HassConfig(path) diff --git a/webapp/src/App.stories.tsx b/webapp/src/App.stories.tsx index 8d5b8c5..ca33d91 100644 --- a/webapp/src/App.stories.tsx +++ b/webapp/src/App.stories.tsx @@ -1,12 +1,14 @@ import App from "./App"; import { withQuery } from "@storybook/addon-queryparams"; +import { makeStory } from "devUtils"; -const book = { - title: "App/App", - decorators: [withQuery], - component: App -}; +const { make, componentMeta } = makeStory({ + Component: App, + meta: { + title: "App", + decorators: [withQuery], + }, +}); +export default componentMeta; -export const Basic = () => - -export default book; +export const RootApp = make({}); diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx index ad89e27..5bcb119 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx @@ -1,25 +1,27 @@ -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { makeTagDB } from "components/AutomationList/TagDB"; -import { Page } from "components/Page"; +import { useTagDB } from "components/AutomationManager/TagDB"; import { useState } from "react"; -import { AutoInfoBox } from "./AutoInfoBox"; -import { defaultAutomation } from "utils/defaults"; +import { AutoInfoBox, AutoInfoBoxProps } from "./AutoInfoBox"; +import { makeStory } from "devUtils"; -export default { - title: "App/AutomationList/Editor/InfoBox", - component: AutoInfoBox, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - tagDB: makeTagDB([defaultAutomation("test")]), +const { componentMeta, make } = makeStory({ + meta: { + title: "App/AutomationEditor/InfoBox", }, -} as ComponentMeta; + Component: (args: { + metadata: AutoInfoBoxProps["metadata"]; + tags: AutoInfoBoxProps["tags"]; + onUpdate: AutoInfoBoxProps["onUpdate"]; + }) => { + const [[metadata, tags], setState] = useState([args.metadata, args.tags]); + const tagDB = useTagDB( + [{ id: metadata.id, tags: Object.fromEntries(tags) }], + (_, t) => setState([metadata, Object.entries(t)]) + ); -export const NoTags: ComponentStory = (args) => { - const [[metadata, tags], setState] = useState([args.metadata, args.tags]); - return ( - + return ( { @@ -27,11 +29,12 @@ export const NoTags: ComponentStory = (args) => { setState([m, t]); }} /> - - ); -}; -NoTags.args = { - ...NoTags.args, + ); + }, +}); + +export default componentMeta; +export const NoTags = make({ tags: [], metadata: { id: "random", @@ -42,11 +45,9 @@ NoTags.args = { }, mode: "single", }, -}; +}); -export const SomeTags: ComponentStory = NoTags.bind({}); -SomeTags.args = { - ...SomeTags.args, +export const SomeTags = make({ tags: [ ["Room", "Bathroom"], ["Type", "Climate"], @@ -60,4 +61,4 @@ SomeTags.args = { }, mode: "single", }, -}; +}); diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx index 942b362..a580c89 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx @@ -5,17 +5,17 @@ import InputText from "components/Inputs/InputText"; import { FC, ReactNode, useRef, useState } from "react"; import { AddIcon, TrashIcon } from "components/Icons"; import InputAutoText from "components/Inputs/InputAutoText"; -import { TagDB } from "components/AutomationList/TagDB"; +import { TagDB } from "components/AutomationManager/TagDB"; import { ButtonIcon } from "components/Icons/ButtonIcons"; -interface Props { +export interface AutoInfoBoxProps { metadata: AutomationMetadata; tags: Array<[string, string]>; onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void; tagDB: TagDB; children?: ReactNode; } -export const AutoInfoBox: FC = ({ +export const AutoInfoBox: FC = ({ metadata, tags, onUpdate, diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index 8f1eefc..5b7bffc 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -1,13 +1,12 @@ -import React from "react"; import { ComponentMeta, ComponentStory } from "@storybook/react"; import { AutomationEditor } from "./index"; import { useState } from "react"; import { Page } from "components/Page"; -import { makeTagDB } from "components/AutomationList/TagDB"; +import { useTagDB } from "components/AutomationManager/TagDB"; import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; export default { - title: "App/AutomationList/Editor", + title: "App/AutomationEditor", component: AutomationEditor, parameters: { actions: { argTypesRegex: "^on.*" } }, args: { @@ -21,7 +20,10 @@ const Template: ComponentStory = (args) => { {} + )} automation={state} onUpdate={(s) => { window.setTimeout(() => setState(s), 3000); diff --git a/webapp/src/components/AutomationEditor/index.tsx b/webapp/src/components/AutomationEditor/index.tsx index 6bfb803..a4a4186 100644 --- a/webapp/src/components/AutomationEditor/index.tsx +++ b/webapp/src/components/AutomationEditor/index.tsx @@ -10,7 +10,7 @@ import { Button } from "components/Inputs/Button"; import { useAutomatioEditorState, EditorData } from "./state"; import { MiniFailure } from "types/validators/helper"; import InputYaml from "components/Inputs/InputYaml"; -import { TagDB } from "components/AutomationList/TagDB"; +import { TagDB } from "components/AutomationManager/TagDB"; import Skeleton from "@mui/material/Skeleton"; import LinearProgress from "@mui/material/LinearProgress"; import { useCookies } from "react-cookie"; diff --git a/webapp/src/components/AutomationList/AutomationList.stories.tsx b/webapp/src/components/AutomationList/AutomationList.stories.tsx deleted file mode 100644 index 8909500..0000000 --- a/webapp/src/components/AutomationList/AutomationList.stories.tsx +++ /dev/null @@ -1,187 +0,0 @@ -import "styles/root.css"; -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { useState } from "react"; -import { AutomationList } from "."; -import { bigMockAutoList } from "utils/mocks"; -import { Page } from "components/Page"; -import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; -import { AutomationData } from "types/automations"; - -export default { - title: "App/AutomationList", - component: AutomationList, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - dims: DEFAULT_DIMS, - }, -} as ComponentMeta; - -const Template: ComponentStory = (args) => { - const [autos, setAutos] = useState(args.automations.data); - return ( - - { - args.onAdd(a); - setAutos([...autos, a]); - }} - onRemove={(i) => { - args.onRemove(i); - setAutos([...autos.slice(0, i), ...autos.slice(i + 1)]); - }} - onUpdate={(i, a) => { - args.onUpdate(i, a); - setAutos([...autos.slice(0, i), a, ...autos.slice(i + 1)]); - }} - /> - - ); -}; - -const make = (autos: AutomationData[]) => { - const NewTemp = Template.bind({}); - NewTemp.args = { - ...NewTemp.args, - automations: { - data: autos, - params: { - offset: 0, - limit: 100, - }, - totalItems: autos.length, - }, - }; - - return NewTemp; -}; - -export const EmptyStart = make([]); - -export const FewAutos = make(bigMockAutoList); - -export const BadAutmations = make([ - { - metadata: { - alias: "Set Thermostat to 76 at 8am", - description: "", - mode: "single", - } as any, - trigger: [ - { - at: "08:00:00", - platform: "time", - }, - ], - condition: [ - { - condition: "state", - entity_id: "climate.thermostat", - state: "cool", - }, - ], - sequence: [ - { - data: { - temperature: 76, - }, - service: "climate.set_temperature", - target: { - entity_id: "climate.thermostat", - }, - }, - ], - tags: {}, - }, - { - condition: [], - tags: {}, - metadata: { - id: "Bad Choose Example", - alias: "Bad Choose", - description: "Example Metadata", - trigger_variables: { - wowo: "!", - }, - mode: "single", - }, - trigger: [], - sequence: [ - { - choose: [], - }, - ] as any, - }, - { - condition: [], - tags: {}, - metadata: { - id: "random", - alias: "Bad Trigger", - description: "Example Metadata", - trigger_variables: { - wowo: "!", - }, - mode: "single", - }, - trigger: ["haha I am a string"] as any, - sequence: [], - }, - { - condition: [], - tags: {}, - metadata: {} as any, - trigger: [], - sequence: [], - }, -]); - -export const FewAutosWithSlowLoader: ComponentStory = ( - args -) => { - const [autos, setAutos] = useState(args.automations.data); - return ( - - { - window.setTimeout(() => { - args.onAdd(a); - setAutos([...autos, a]); - }, 10000); - }} - onRemove={(i) => { - window.setTimeout(() => { - args.onRemove(i); - setAutos([...autos.slice(0, i), ...autos.slice(i + 1)]); - }, 10000); - }} - onUpdate={(i, a) => { - window.setTimeout(() => { - args.onUpdate(i, a); - setAutos([...autos.slice(0, i), a, ...autos.slice(i + 1)]); - }, 10000); - }} - /> - - ); -}; -FewAutosWithSlowLoader.args = { - ...FewAutosWithSlowLoader.args, - automations: { - data: bigMockAutoList, - params: { - limit: 100, - offset: 0, - }, - totalItems: 100, - }, -}; diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css deleted file mode 100644 index 9859faf..0000000 --- a/webapp/src/components/AutomationList/AutomationListBox.css +++ /dev/null @@ -1,84 +0,0 @@ -.automation-list-box { - display: flex; - flex-direction: column; - height: 100%; -} - -.automation-list-box--body { - overflow-y: auto; - overflow-x: hidden; - height: 100%; - flex: 1; - user-select: none; -} - -.automation-list-box--body>* { - padding-top: 0.5em; - padding-bottom: 0.5em; -} - -.automation-list-box--body--group { - display: flex; - flex-direction: column; - padding-left: 1em; - border-radius: 10px; - padding-bottom: 0.5em; - border-width: 1px; - border-bottom-style: solid; - border-color: var(--mui-grey-400); -} - -.automation-list-box--body--group.other { - border-color: var(--mui-success-main); -} - -.automation-list-box--body--group.other>b { - color: var(--mui-success-light); -} - -.automation-list-box--body--group>.automation-list-box--body--group { - border-left-style: solid; - border-top-left-radius: 0; - margin-left: 0.75em; - padding-left: 0.5em; -} - -.automation-list-box--body--group>b { - width: 100%; - cursor: pointer; - padding: 0.1em; -} - -.automation-list-box--body--group>b:hover, -.automation-list-box--body--group.selected>b { - background-color: var(--mui-grey-700); - color: var(--mui-secondary-contrastText); -} - - -.automation-list-box--bottom { - margin-top: 0.5em; - margin-bottom: 0.5em; - display: flex; - flex-direction: column; - justify-content: space-around; - align-items: center; -} - -.automation-list-box--bottom>.automation-list-box--bottom--add { - background-color: var(--mui-primary-main); - color: var(--mui-primary-contrastText); - padding: 0.5em 2em; -} - -.automation-list-box--bottom>.automation-list-box--bottom--add:hover { - color: var(--mui-primary-light); -} - -.automation-list-box--bottom--load svg { - color: var(--mui-primary-main); -} - -.automation-list-box--bottom--load.less svg { - transform: rotate(180deg); -} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx deleted file mode 100644 index f326e0d..0000000 --- a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx +++ /dev/null @@ -1,79 +0,0 @@ -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { useState } from "react"; -import { AutomationListBox } from "./AutomationListBox"; -import { createMockAuto } from "utils/mocks"; -import { Page } from "components/Page"; -import { CookiesProvider } from "react-cookie"; -import { makeTagDB } from "./TagDB"; -import { AutomationData } from "types/automations"; - -export default { - title: "App/AutomationList/Box", - component: AutomationListBox, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: {}, -} as ComponentMeta; - -const Template: ComponentStory = (args) => { - const [selected, setSelected] = useState(args.selected); - return ( - - - { - setSelected(i); - args.onSelectAutomation(i); - }} - /> - - - ); -}; - -const make = (autos: AutomationData[]) => { - const NewTemp = Template.bind({}); - NewTemp.args = { - ...NewTemp.args, - automations: { - data: autos, - params: { - offset: 0, - limit: 100, - }, - totalItems: autos.length, - }, - }; - - return NewTemp; -}; - -export const EmptyStart = make([]); - -export const FewAutos = make([ - createMockAuto({ - Room: "Bathroom", - Type: "Lights", - NewDevice: "Yes", - Remote: "No", - Floor: "1", - }), - createMockAuto({ Room: "Bathroom", Type: "Climate" }), - createMockAuto({ Room: "Living Room", Type: "Climate" }), - createMockAuto({ Type: "Climate" }), - createMockAuto({ Type: "Lights", Routine: "Myself" }), - createMockAuto({ Routine: "DNS", Type: "SSL" }), - createMockAuto({ Room: "Bedroom", Type: "Lights", Routine: "Baby" }), - createMockAuto({ Room: "Office", Type: "Lights" }), - createMockAuto({ Room: "Office" }), - createMockAuto(), -]); - -export const OneAuto = make([ - createMockAuto({ - Type: "Climate", - }), -]); diff --git a/webapp/src/components/AutomationList/AutomationListBox.test.ts b/webapp/src/components/AutomationList/AutomationListBox.test.ts deleted file mode 100644 index 5463d19..0000000 --- a/webapp/src/components/AutomationList/AutomationListBox.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { getTagList } from "./AutomationListBox"; -import { createMockAuto } from "utils/mocks"; - -test("getTagList", () => { - expect( - getTagList([ - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - ]) - ).toStrictEqual([]); - - expect( - getTagList([ - createMockAuto({ Room: "Kitchen", Area: "Oven" }), - createMockAuto({ Room: "Attic" }), - createMockAuto({ Room: "Office" }), - createMockAuto({ Area: "Balcony" }), - ]) - ).toStrictEqual(["Room", "Area"]); -}); diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx deleted file mode 100644 index 66d2ccb..0000000 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ /dev/null @@ -1,195 +0,0 @@ -import "./AutomationListBox.css"; -import { FC, useState } from "react"; -import { AutomationData } from "types/automations"; -import InputMultiSelect from "components/Inputs/InputMultiSelect"; -import InputText from "components/Inputs/InputText"; -import { Button } from "components/Inputs/Button"; -import { makeGrouping } from "./automationGrouper"; -import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; -import { useCookies } from "react-cookie"; -import { TagDB } from "components/AutomationList/TagDB"; -// import { ButtonIcon } from "components/Icons/ButtonIcons"; -// import ReadMoreOutlinedIcon from "@mui/icons-material/ReadMoreOutlined"; -import { ListData, ListParams } from "apiService/types"; -import Chip from "@mui/material/Chip"; -import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; - -export type AutomationListBoxProps = { - automations: ListData; - selected: number; - onSelectAutomation: (i: number) => void; - onRemove: (i: number) => void; - onAdd: () => void; - onUpdateTags: (id: string, tags: Record) => void; - onUpdate: (a: AutomationData, i: number) => void; - onLoadMore: (p: ListParams) => void; - tagsDB: TagDB; -}; - -const useAutomationListBoxState = () => { - const [ - cookies, - setCookies, - // eslint-disable-next-line - _, - ] = useCookies(["albSearchText", "albSelected"]); - let initialSelected = []; - try { - initialSelected = JSON.parse(cookies.albSelected); - if (!Array.isArray(initialSelected)) { - initialSelected = [initialSelected]; - } - initialSelected = initialSelected.map(Number); - // eslint-disable-next-line - } catch (_) {} - const [searchText, setSearchText] = useState(cookies.albSearchText ?? ""); - const [selectedTagIdx, setSelectedTagIdx] = - useState(initialSelected); - - return { - searchText, - setSearchText(v: string) { - setSearchText(v); - setCookies("albSearchText", v); - }, - setSelectedTagIdx(n: number[]) { - setSelectedTagIdx(n); - setCookies("albSelected", JSON.stringify(n)); - }, - selectedTagIdx, - }; -}; - -export const AutomationListBox: FC = ({ - automations: { - data: automations, - // params: autoParams, - totalItems: totalAutomations, - }, - selected, - onSelectAutomation, - onAdd, - onRemove, - // onLoadMore, - onUpdateTags, - tagsDB, - onUpdate, -}) => { - const tags = getTagList(automations); - const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } = - useAutomationListBoxState(); - const filteredAutomations = filterAutomations( - automations, - searchText.toLowerCase() - ); - const grouping = makeGrouping( - filteredAutomations.map(([a, _]) => a), - selectedTagIdx.map((i) => tags[i]), - selected - ); - - return ( -
-
- - -
-
- -
-
- - {automations.length < totalAutomations ? ( - <> - } - onClick={() => - window.open( - "https://github.com/asosnovsky/Shortumation/issues/78", - "_blank" - ) - } - /> - support for more is not yet implemented - - ) : ( - "" - )} - {/* {autoParams.offset > 0 && ( - } - onClick={() => onLoadMore()} - /> - )} */} - {/* {filteredAutomations.length} out of {totalAutomations} */} - {/* {autoParams.offset + automations.length < totalAutomations && ( - } - /> - )} */} -
-
- ); -}; - -export const getTagList = (autos: AutomationData[]): string[] => - Object.keys( - autos - .map((a) => Object.keys(a.tags)) - .reduce((all: any, keys) => { - for (const k of keys) { - all[k] = 1; - } - return all; - }, {}) - ); - -const filterAutomations = ( - autos: AutomationData[], - searchText: string -): Array<[AutomationData, number]> => - autos - .map<[AutomationData, number]>((a, i) => [a, i]) - .filter(([a, i]) => { - if (!a.metadata) { - return true; - } - if (searchText.length > 0) { - if ( - !( - (a.metadata.description ?? "") - .toLocaleLowerCase() - .includes(searchText) || - (a.metadata.alias ?? "").toLocaleLowerCase().includes(searchText) - ) - ) { - return false; - } - } - return true; - }); diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.css b/webapp/src/components/AutomationList/AutomationListBoxGroup.css deleted file mode 100644 index 2764080..0000000 --- a/webapp/src/components/AutomationList/AutomationListBoxGroup.css +++ /dev/null @@ -1,50 +0,0 @@ -.automation-list-box--body--item:hover, .automation-list-box--body--item.selected { - outline-color: var(--mui-grey-400); - outline-style: solid; - outline-width: 1px; - background-color: var(--mui-secondary-contrastText); -} - -.automation-list-box--body--item:hover>.automation-list-box--body--item--tags>span { - border-color: var(--mui-common-black); -} - -.automation-list-box--body--item--tags--add:hover { - border-color: var(--mui-common-black); - color: var(--mui-grey-400); - background-color: var(--mui-secondary-contrastText); -} - -.automation-list-box--body--item { - padding-left: 1em; - cursor: pointer; - display: flex; - gap: 1em; -} - -.automation-list-box--body--item::before { - content: "⊚ "; -} - -.automation-list-box--body--item--buttons { - display: flex; - flex-direction: row; - justify-content: center; - align-items: center; - height: 100%; - gap: 0.25em; - margin: 0.5em -} - -.automation-list-box--body--item--buttons>button { - padding: 0.25em; - margin: 0; -} - -.automation-list-box--body--item--buttons .save { - color: var(--mui-success-light) -} - -.automation-list-box--body--item--buttons .save:hover { - color: var(--mui-success-main) -} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx deleted file mode 100644 index f145f4d..0000000 --- a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx +++ /dev/null @@ -1,257 +0,0 @@ -import "./AutomationListBoxGroup.css"; - -import { TrashIcon } from "components/Icons"; -import { MetadataBox } from "./AutomationMetadataBox"; -import { AutomationData } from "types/automations"; -import { AutomationGrouping } from "./automationGrouper"; -import { FC, useEffect, useState } from "react"; -import { ButtonIcon } from "components/Icons/ButtonIcons"; -import { TagDB } from "./TagDB"; -import Save from "@mui/icons-material/Save"; - -export type AutomationListBoxEvents = { - onSelectAutomation: (i: number) => void; - onRemove: (i: number) => void; -}; -export type AutomationListBoxGroups = { - grouping: AutomationGrouping; - currentNode: number; -}; -export type AutomationListBoxProps = { - events: AutomationListBoxEvents; - groups: AutomationListBoxGroups; - autos: Array<[AutomationData, number]>; - selectedAutomationIdx: number; -}; -export type AutomationListBoxGroupProps = { - events: AutomationListBoxEvents; - grouping: AutomationGrouping; - autos: Array<[AutomationData, number]>; - selectedAutomationIdx: number; - tagsDB: TagDB; - onSave: (a: AutomationData, i: number) => void; -}; -export const AutomationListBoxGroup: FC = ({ - events, - grouping, - autos, - selectedAutomationIdx, - tagsDB, - onSave, -}) => { - if (grouping.top.length === 1) { - return ( - <> - {convertGroupsToItems( - autos, - { - grouping, - currentNode: grouping.top[0], - }, - events, - selectedAutomationIdx, - tagsDB, - onSave - )} - - ); - } else { - return ( - <> - {grouping.top.map((groupIdx) => ( - - ))} - - ); - } -}; - -export const convertGroupsToItems = ( - autos: Array<[AutomationData, number]>, - { grouping, currentNode }: AutomationListBoxGroups, - events: AutomationListBoxEvents, - selectedAutomationIdx: number, - tagsDB: TagDB, - onSave: (a: AutomationData, i: number) => void -) => { - const automations = grouping.getAutomations(currentNode); - if (automations.length > 0) { - return automations.map((autoIndex) => ( - onSave(a, autoIndex)} - /> - )); - } - const subgroups = grouping.getSubGroups(currentNode); - if (subgroups.length > 0) { - return subgroups.map((groupIdx) => ( - - )); - } - return <>; -}; - -export const AutomationListBoxGroupItem: FC<{ - events: AutomationListBoxEvents; - groups: AutomationListBoxGroups; - autos: Array<[AutomationData, number]>; - selectedAutomationIdx: number; - tagsDB: TagDB; - onSave: (a: AutomationData, i: number) => void; -}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB, onSave }) => { - const groupData = groups.grouping.getData(groups.currentNode); - const automations = groups.grouping.getAutomations(groups.currentNode); - const [open, setOpen] = useState(false); - const isOther = groupData.isOther; - const openIcon = !open ? "⊕" : "⊖"; - const total = isOther ? ( - - {automations.length} automation{automations.length !== 1 ? "s" : ""}{" "} - {groupData.isOther ? "" : "(untagged)"} - - ) : ( - <> - ); - return ( -
- setOpen(!open)}> - {groupData.name} {openIcon} {total} - - {open && - convertGroupsToItems( - autos, - groups, - events, - selectedAutomationIdx, - tagsDB, - onSave - )} -
- ); -}; - -export const AutomationListBoxItem: FC<{ - auto: [AutomationData, number]; - isSelected: boolean; - events: AutomationListBoxEvents; - tagsDB: TagDB; - onSave: (a: AutomationData) => void; -}> = ({ - events: { onRemove, onSelectAutomation }, - auto: [auto, autoIndex], - isSelected, - tagsDB, - onSave, -}) => { - // state - const [{ tags, isModified, alias }, setState] = useState({ - isModified: false, - tags: Object.entries(auto.tags), - alias: auto.metadata.alias ?? "", - }); - - // effects - useEffect(() => { - setState({ - isModified: false, - tags: Object.entries(auto.tags), - alias: auto.metadata.alias ?? "", - }); - }, [auto]); - - // alias - const setAlias = (a: string) => - setState({ - isModified: true, - tags, - alias: a, - }); - const setTags = (t: [string, string][]) => - setState({ - isModified: true, - tags: t, - alias, - }); - // sub elements - let title = "BadAuto<>"; - if (auto.metadata) { - title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}`; - } - - // render - return ( -
onSelectAutomation(autoIndex)} - > - -
- {isModified && ( - } - onClick={() => - onSave({ - ...auto, - tags: Object.fromEntries(tags), - metadata: { - ...auto.metadata, - alias, - }, - }) - } - /> - )} - onRemove(autoIndex)} icon={} /> -
-
- ); -}; diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.css b/webapp/src/components/AutomationList/AutomationMetadataBox.css deleted file mode 100644 index 43b9c2d..0000000 --- a/webapp/src/components/AutomationList/AutomationMetadataBox.css +++ /dev/null @@ -1,101 +0,0 @@ -.automation-list-box--body--item--title { - display: flex; - flex-direction: column; - padding-left: 0.25em; - flex: 1; -} - -.automation-list-box--body--item--title>b { - font-size: 1em; - display: flex; - align-items: center; - gap: 0.25em; -} - -.automation-list-box--body--item--title .input-text-view span { - max-height: 1.5em; -} - -.automation-list-box--body--item--title>span, .automation-list-box--body--item--title>b>span { - font-size: 0.75em; -} - - -.automation-list-box--body--item--tags { - flex: 1; - display: flex; - flex-wrap: wrap; - font-size: 0.75em; - justify-content: left; - align-items: flex-start; - overflow: auto; - max-height: 100%; - gap: 0.25em; -} - -.automation-list-box--body--item--tags>span { - border-color: var(--mui-grey-400); - border-style: solid; - border-width: 1px; - border-radius: 0.5em; - padding: 0.1em 0.5em; -} - -.automation-list-box--body--item--tags--item { - color: white; - border-color: white; - border-style: solid; - border-width: 1px; - border-radius: 0.5em; - padding: 0.1em 0.5em; - - display: flex; - flex-direction: row; - gap: 0.25em; - max-width: min-content; - align-items: center; - -} - -.automation-list-box--body--item--tags--item>.text:hover { - text-decoration: underline; - cursor: pointer; -} - -.automation-list-box--body--item--tags--item>.text { - position: relative; -} - -.automation-list-box--body--item--tags--item>.text .MuiFilledInput-root { - padding: 0; - min-width: 6em; - max-width: 10em; - background-color: var(--mui-common-black); -} - -.automation-list-box--body--item--tags--item>.text .icon { - position: absolute; - right: 0.5em; - top: 0.5em; - cursor: pointer; -} - -.automation-list-box--body--item--tags--item>.text .icon:hover { - background-color: var(--mui-grey-400); - border-radius: 20px; -} - -.automation-list-box--body--item--tags--item>.text .icon:hover svg { - fill: var(--mui-common-black); -} - -.automation-list-box--body--item--tags--item .delete { - font-size: 1em; - color: var(--mui-error-light); -} - -.automation-list-box--body--item--tags--item .delete:hover { - color: var(--mui-error-dark); - outline: 1px solid var(--mui-error-dark); - border-radius: 20px; -} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx deleted file mode 100644 index 5982997..0000000 --- a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx +++ /dev/null @@ -1,181 +0,0 @@ -import "./AutomationMetadataBox.css"; -import { CheckMarkIcon } from "components/Icons"; -import { FC, useState } from "react"; -import { AutomationMetadata } from "types/automations"; -import { InputAutoComplete } from "components/Inputs/InputAutoComplete"; -import { TagDB } from "./TagDB"; -import DeleteIcon from "@mui/icons-material/ClearTwoTone"; -import { InputTextView } from "components/Inputs/InputTextView"; - -export const MetadataBox: FC<{ - metadata: AutomationMetadata; - onAliasUpdate: (t: string) => void; - onTagUpdate: (t: [string, string][]) => void; - tags: [string, string][]; - tagsDB: TagDB; -}> = ({ tags, onTagUpdate, onAliasUpdate, tagsDB, metadata }) => { - // state - const [newTag, setNewTag] = useState< - | { - on: false; - } - | { - on: true; - name: string; - value: string; - } - >({ on: false }); - // alias - const tagOptions = tagsDB.getTagNames(tags.map((t) => t[0])); - - // render - let title = {"BadAuto<>"}; - if (metadata) { - title = ( - <> - - - ({String(metadata.id).slice(0, 10)}) - - {String(metadata.description ?? "").slice(0, 25)} - - ); - } - return ( -
- {title} -
- {tags.map(([tagName, tagValue], tagIndex) => ( - - onTagUpdate([ - ...tags.slice(0, tagIndex), - ...tags.slice(tagIndex + 1), - ]) - } - onChange={([newName, newValue]) => - onTagUpdate([ - ...tags.slice(0, tagIndex), - [newName, newValue], - ...tags.slice(tagIndex + 1), - ]) - } - /> - ))} - {newTag.on ? ( - { - setNewTag({ - on: true, - name, - value, - }); - }} - onToggle={(em) => { - if (!em) { - if (newTag.name.trim() !== "" && newTag.value.trim() !== "") { - onTagUpdate(tags.concat([[newTag.name, newTag.value]])); - setNewTag({ - on: false, - }); - } - } - setNewTag({ - on: false, - }); - }} - /> - ) : ( - - setNewTag({ - on: true, - name: "", - value: "", - }) - } - > - + - - )} -
-
- ); -}; - -export const MetadataBoxTag: FC<{ - value: [string, string]; - onChange: (t: [string, string]) => void; - options: [string[], string[]]; - initialState?: boolean; - onToggle?: (em: boolean) => void; - onDelete?: () => void; -}> = (props) => { - const [editMode, setEditMode] = useState( - props.initialState ? props.initialState : false - ); - const toggle = () => { - if (editMode) { - if (props.value[0].trim() === "" || props.value[1].trim() === "") { - props.onToggle && props.onToggle(true); - return setEditMode(true); - } - } - props.onToggle && props.onToggle(!editMode); - setEditMode(!editMode); - }; - return ( - - {props.onDelete && ( - - )} - {editMode ? ( - props.onChange([v ?? "", props.value[1]])} - onlyShowLabel - endAdornment={} - /> - ) : ( - - {props.value[0]}: - - )} - {editMode ? ( - props.onChange([props.value[0], v ?? ""])} - onlyShowLabel - endAdornment={} - /> - ) : ( - - {props.value[1]} - - )} - - ); -}; diff --git a/webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx b/webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx deleted file mode 100644 index a9676e8..0000000 --- a/webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { Page } from "components/Page"; -import { MetadataBoxTag } from "./AutomationMetadataBox"; -import { useState } from "react"; - -export default { - title: "App/AutomationList/Box", - component: MetadataBoxTag, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - value: ["Type", "Climate"], - options: [[], []], - }, -} as ComponentMeta; - -export const BoxTag: ComponentStory = (args) => { - const [state, setState] = useState(args.value); - return ( - - { - setState(v); - args.onChange(v); - }} - /> - - ); -}; diff --git a/webapp/src/components/AutomationList/ConnectedAutomationList.stories.tsx b/webapp/src/components/AutomationList/ConnectedAutomationList.stories.tsx deleted file mode 100644 index 4d882d4..0000000 --- a/webapp/src/components/AutomationList/ConnectedAutomationList.stories.tsx +++ /dev/null @@ -1,63 +0,0 @@ -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { ConnectedAutomationList } from "."; -import { createMockAuto } from "utils/mocks"; -import { useMockApiService } from "apiService"; -import { Page } from "components/Page"; -import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; - -export default { - title: "App/AutomationList/MockConnected", - component: ConnectedAutomationList, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - dims: DEFAULT_DIMS, - initialAutomations: [], - }, -} as ComponentMeta; - -const Template: ComponentStory = (args) => { - const api = useMockApiService(args.initialAutomations); - return ( - - - - ); -}; - -export const EmptyStart = Template.bind({}); - -export const FewAutos = Template.bind({}); -FewAutos.args = { - ...FewAutos.args, - initialAutomations: [ - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - createMockAuto(), - ], -}; - -export const Loading: ComponentStory = (args) => { - const api = useMockApiService(args.initialAutomations); - return ( - - - - ); -}; diff --git a/webapp/src/components/AutomationList/TagDB.ts b/webapp/src/components/AutomationList/TagDB.ts deleted file mode 100644 index ccd96b0..0000000 --- a/webapp/src/components/AutomationList/TagDB.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { AutomationData } from "types/automations"; - -export type TagDB = ReturnType; -export const makeTagDB = (automations: AutomationData[]) => { - const allTags: Record> = {}; - automations.forEach(({ tags }) => - Object.keys(tags).forEach((tagName) => { - const tagValue = tags[tagName]; - if (!allTags[tagName]) { - allTags[tagName] = new Set(); - } - allTags[tagName].add(tagValue); - }) - ); - const allTagNames = Object.keys(allTags); - return { - getTagNames(myTags: string[]): string[] { - return allTagNames.filter((n) => !myTags.includes(n)).sort(); - }, - getTagValues(tagName: string): string[] { - return allTags[tagName] - ? Array.from(allTags[tagName].values()).sort() - : []; - }, - }; -}; diff --git a/webapp/src/components/AutomationList/automationGrouper.test.ts b/webapp/src/components/AutomationList/automationGrouper.test.ts deleted file mode 100644 index ed76066..0000000 --- a/webapp/src/components/AutomationList/automationGrouper.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { makeGrouping } from "./automationGrouper"; -import { AutomationData } from 'types/automations/index'; -import { bigMockAutoList, createMockAuto } from 'utils/mocks'; - -const automations: AutomationData[] = [ - createMockAuto({ - "Room": 'Living Room', - "Type": 'Lights' - }), - createMockAuto({ - "Room": 'Living Room', - "Type": 'Climate' - }), - createMockAuto({ - "Room": 'Kitchen', - "Type": 'Climate' - }), - createMockAuto({ - "Type": 'Climate' - }), - createMockAuto({ - "Room": 'Living Room' - }), - createMockAuto({}), -]; - -test('no grouping', () => { - const grouped = makeGrouping(automations, [], 0); - expect(grouped.top).toStrictEqual([0]) - expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 2, 3, 4, 5]) -}); -test('1 grouping', () => { - const grouped = makeGrouping(automations, ["Room"], 0); - expect(grouped.top).toHaveLength(3); - expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 4]) - expect(grouped.getAutomations(1)).toStrictEqual([2]) - expect(grouped.getAutomations(2)).toStrictEqual([3, 5]) -}); -test('2 groupings', () => { - const grouped = makeGrouping(automations, ["Room", "Type"], 0); - expect(grouped.top).toHaveLength(3); - expect(grouped.getAutomations(grouped.top[0])).toHaveLength(0) - expect(grouped.getSubGroups(grouped.top[0])).toHaveLength(3) - expect(grouped.getAutomations(grouped.top[1])).toHaveLength(0) - expect(grouped.getSubGroups(grouped.top[1])).toHaveLength(1) - expect(grouped.getAutomations(grouped.top[2])).toHaveLength(2) - expect(grouped.getSubGroups(grouped.top[2])).toHaveLength(0) -}); -test('bigAutoList groupings has not duplicate ids', () => { - const grouped = makeGrouping(bigMockAutoList, ['Room', "Remote"], 0); - const flattenAndCount = (gid: number): Record => { - const subgroups = grouped.getSubGroups(gid); - if (subgroups.length > 0) { - return flattenAndConsolidate(subgroups) - } else { - return grouped.getAutomations(gid).reduce((sum: Record, b) => { - sum[b] = (sum[b] ?? []).concat(gid) - return sum - }, {}) - } - } - const flattenAndConsolidate = (gids: number[]): Record => gids.map(flattenAndCount).reduce((a, b) => { - for (const k of Object.keys(b).map(Number)) { - a[k] = (a[k] ?? []).concat(b[k]) - } - return a - }, {}) - const countedAutos = flattenAndConsolidate(grouped.top) - for (const key of Object.keys(countedAutos).map(Number)) { - expect(countedAutos[key]).toHaveLength(1) - } -}) \ No newline at end of file diff --git a/webapp/src/components/AutomationList/automationGrouper.tsx b/webapp/src/components/AutomationList/automationGrouper.tsx deleted file mode 100644 index 8bb5b9d..0000000 --- a/webapp/src/components/AutomationList/automationGrouper.tsx +++ /dev/null @@ -1,130 +0,0 @@ -import { AutomationData } from "types/automations"; - -const makeGrouper = () => { - const groupIdNames: string[] = [] - const groupSubGroups: Array> = [] - const groupAutomations: number[][] = [] - const groupData: Array<{ - name: string; - selected: boolean; - isOther: boolean; - isTop: boolean; - }> = []; - const topGroups: number[] = []; - - return { - groupIdNames, - groupSubGroups, - groupAutomations, - groupData, - topGroups, - addNew(groupId: string, groupName: string, theChosenOne: boolean, isTopLevel: boolean, isOther: boolean = false): number { - groupIdNames.push(groupId); - groupSubGroups.push(new Set()); - groupAutomations.push([]); - groupData.push({ - name: groupName, - selected: theChosenOne, - isTop: isTopLevel, - isOther - }) - const groupIdx = groupIdNames.length - 1; - if (isTopLevel) { - topGroups.push(groupIdx); - } - return groupIdx; - }, - updateGroup( - groupIdx: number, - update: { - isChosen: boolean, - lastGroupIdx: number | null, - autoIdx: number | null, - } - ) { - if (update.isChosen) { - groupData[groupIdx].selected = true; - } - if (update.lastGroupIdx !== null) { - groupSubGroups[update.lastGroupIdx].add(groupIdx); - } - if (update.autoIdx !== null) { - groupAutomations[groupIdx].push(update.autoIdx) - } - } - } -} - -export type AutomationGrouping = ReturnType; -export const makeGrouping = ( - autos: AutomationData[], - selectedTags: string[], - selectedAutomationIdx: number, -) => { - const grouper = makeGrouper(); - - if (selectedTags.length === 0) { - grouper.addNew("$", "", selectedAutomationIdx >= 0, true, true) - grouper.groupAutomations = [autos.map((_, i) => i)]; - } else { - autos.forEach((auto, autoIdx) => { - const theChosenOne = selectedAutomationIdx === autoIdx; - let lastGroupIdx: number | null = null; - let lastGroupId: string = "$"; - for (let ti = 0; ti < selectedTags.length; ti++) { - const isTopLevel = ti === 0; - const selTag = selectedTags[ti]; - if (selTag in auto.tags) { - const groupName = auto.tags[selTag]; - const groupId = `${lastGroupId}>${selTag}:${groupName}`; - let groupIdx = grouper.groupIdNames.indexOf(groupId); - if (groupIdx < 0) { - groupIdx = grouper.addNew(groupId, groupName, theChosenOne, isTopLevel); - } - grouper.updateGroup(groupIdx, { - isChosen: theChosenOne, - lastGroupIdx, - autoIdx: ti === selectedTags.length - 1 ? autoIdx : null, - }) - lastGroupIdx = groupIdx; - lastGroupId = groupId; - } else { - const groupId = `${lastGroupId}>${selTag}:$`; - let groupIdx = grouper.groupIdNames.indexOf(groupId); - if (groupIdx < 0) { - groupIdx = grouper.addNew(groupId, "", theChosenOne, isTopLevel, true); - } - grouper.updateGroup(groupIdx, { - isChosen: theChosenOne, - lastGroupIdx, - autoIdx, - }) - break - } - } - }); - } - const sortGroupIds = (groupIds: number[]) => groupIds.sort((a, b) => { - const adata = grouper.groupData[a]; - const bdata = grouper.groupData[b]; - if (adata.isOther) { - return 1 - } else { - return adata.name > bdata.name ? -1 : 1 - } - }) - return { - top: sortGroupIds(grouper.topGroups), - getSubGroups(groupIdx: number) { - return sortGroupIds(Array.from(grouper.groupSubGroups[groupIdx].values())) - }, - getAutomations(groupIdx: number) { - return grouper.groupAutomations[groupIdx] - }, - getData(groupIdx: number) { - return grouper.groupData[groupIdx] - } - } -} - - diff --git a/webapp/src/components/AutomationList/index.css b/webapp/src/components/AutomationList/index.css deleted file mode 100644 index 57ee8a6..0000000 --- a/webapp/src/components/AutomationList/index.css +++ /dev/null @@ -1,68 +0,0 @@ -/* root elements */ -.automation-list--root { - width: 100%; - height: 100%; - display: flex; - flex-direction: row; - overflow: hidden; -} - -.automation-list--list { - flex: 1; - max-width: 400px; - position: relative; -} - -.automation-list--viewer { - overflow: hidden; - flex: 1; -} - -/* Loading state */ -.automation-list--root.loading { - width: 100%; - height: 100%; - flex-direction: column; -} - -.automation-list--root.loading>* { - width: 100%; -} - -.automation-list--root.loading .automation-list--circle-loader { - width: 100%; - height: 100%; - justify-content: center; - align-items: center; - display: flex; -} - -/* list children: on hide */ -.automation-list--list.hide>.automation-list-box { - width: 0; - opacity: 0; -} - -.automation-list--list.hide { - min-width: 35px; - max-width: 35px; -} - -.automation-list--list.hide>.automation-list--list-hide { - transform: rotate(180deg); - left: 0; -} - -/* list children: normal */ -.automation-list--list { - border-right: solid; - border-width: 1px; -} - -.automation-list--list .automation-list--list-hide { - position: absolute; - bottom: 0em; - left: 0.5em; - max-width: 1.5em; - max-height: 1.5em; -} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/index.mobile.css b/webapp/src/components/AutomationList/index.mobile.css deleted file mode 100644 index 2ba13e0..0000000 --- a/webapp/src/components/AutomationList/index.mobile.css +++ /dev/null @@ -1,38 +0,0 @@ -/* none-hidden */ -.automation-list--root.mobile { - flex-direction: column; -} - -.automation-list--root.mobile>.automation-list--list { - max-width: 100vw; - min-height: 100vh; -} - -.automation-list--root.mobile .automation-editor { - flex-direction: column; -} - -.automation-list--root.mobile .automation-list--list-hide { - transform: rotate(90deg); - left: 0.25em; -} - - -/* list hide */ -.automation-list--root.mobile .automation-list--list.hide { - width: 100vw; - min-height: 0; - max-height: 0; -} - -.automation-list--root.mobile .automation-list--list.hide .automation-list--list-hide { - transform: rotate(270deg); - position: fixed; - top: 0.5em; - z-index: 5; -} - -.automation-list--root.mobile .automation-editor--flow-wrapper>.automation-editor--flow-wrapper--toolbar .input-text-view, -.automation-list--root.mobile .automation-editor--flow-wrapper>.automation-editor--flow-wrapper--toolbar .id { - display: none; -} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx deleted file mode 100644 index 5a37315..0000000 --- a/webapp/src/components/AutomationList/index.tsx +++ /dev/null @@ -1,213 +0,0 @@ -import "./index.css"; -import "./index.mobile.css"; -import { AutomationEditor } from "components/AutomationEditor"; -import { FC, ReactNode, useState, useRef, useEffect } from "react"; -import { AutomationData } from "types/automations"; -import { defaultAutomation } from "utils/defaults"; -import { ButtonIcon } from "components/Icons/ButtonIcons"; -import { ApiService } from "apiService/core"; -import { AutomationListBox } from "./AutomationListBox"; -import useWindowSize from "utils/useWindowSize"; -import { makeTagDB } from "./TagDB"; -import LinearProgress from "@mui/material/LinearProgress"; -import CircularProgress from "@mui/material/CircularProgress"; -import { useCookies } from "react-cookie"; -import { useHA } from "haService"; -import { ArrowBack } from "@mui/icons-material"; -import { useSnackbar } from "notistack"; -import { DAGDims } from "components/DAGGraph/elements/types"; -import { ListData, ListParams } from "apiService/types"; - -interface AutomationListParams { - automations: ListData; - onUpdate: (i: number, auto: AutomationData) => void; - onUpdateTags: (id: string, tags: Record) => void; - onAdd: (auto: AutomationData) => void; - onRemove: (i: number) => void; - onLoadMore: (p: ListParams) => void; - dims: DAGDims; - children?: ReactNode; -} - -export const useAutomationListState = () => { - const [ - cookies, - setCookies, - // eslint-disable-next-line - _, - ] = useCookies(["alCurrent"]); - let initialCurrent = 0; - try { - initialCurrent = Number(cookies.alCurrent ?? "0"); - } catch (_) {} - - const [hideList, setHideList] = useState(false); - const [current, setCurrent] = useState(initialCurrent); - - return { - hideList, - setHideList, - current, - setCurrent(i: number) { - setCurrent(i); - setCookies("alCurrent", String(i)); - }, - }; -}; - -export const AutomationList: FC = ({ - automations, - onLoadMore, - onUpdate, - onUpdateTags, - onAdd, - onRemove, - dims, - children, -}) => { - // state - const snackbr = useSnackbar(); - const { hideList, setHideList, current, setCurrent } = - useAutomationListState(); - const { isMobile } = useWindowSize(); - const firstRender = useRef(true); - // alias - const currentAuto = - automations.data.length > 0 - ? automations.data[current - automations.params.offset] - : null; - const tagDB = makeTagDB(automations.data); - // effect - useEffect(() => { - if (firstRender.current) { - snackbr.enqueueSnackbar("Saved.", { variant: "info" }); - } - firstRender.current = true; - // eslint-disable-next-line - }, [automations]); - // render - return ( -
-
- onUpdate(i, a)} - onUpdateTags={onUpdateTags} - onAdd={() => { - onAdd(defaultAutomation(String(Date.now()))); - setCurrent(automations.data.length); - if (isMobile) { - setHideList(true); - } - }} - onSelectAutomation={(i) => { - setCurrent(i); - if (isMobile) { - setHideList(true); - } - }} - onLoadMore={onLoadMore} - selected={current} - onRemove={onRemove} - tagsDB={tagDB} - /> - setHideList(!hideList)} - className="automation-list--list-hide" - icon={} - /> -
-
- {currentAuto ? ( - onUpdate(current, a)} - dims={{ - ...dims, - flipped: isMobile, - }} - tagDB={tagDB} - /> - ) : ( - <> - )} -
- {children} -
- ); -}; - -interface ConnectedAutomationListParams { - dims: DAGDims; - api: ApiService; -} -export const ConnectedAutomationList: FC = ({ - api: { - state: { automations }, - ...methods - }, - dims, -}) => { - const { reloadAutomations } = useHA(); - const [saving, setSaving] = useState(false); - const updateTags = async (id: string, tags: Record) => { - const resp = await methods.updateTags({ - automation_id: id, - tags, - }); - setSaving(true); - await reloadAutomations(); - setSaving(false); - return resp; - }; - const removeAutomation = async (args: any) => { - const resp = await methods.removeAutomation(args); - setSaving(true); - await reloadAutomations(); - setSaving(false); - return resp; - }; - const updateAutomation = async (args: any) => { - const resp = await methods.updateAutomation(args); - setSaving(true); - await reloadAutomations(); - setSaving(false); - return resp; - }; - - if (!automations.ready) { - return ( -
- -
- -
-
- ); - } - if (!automations.ok) { - return Error {automations.error}; - } - return ( - <> - {saving && } - {}} - onAdd={(auto) => - updateAutomation({ auto, index: automations.data.totalItems + 1 }) - } - onRemove={(index) => removeAutomation({ index })} - onUpdate={(index, auto) => updateAutomation({ index, auto })} - /> - - ); -}; diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx b/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx index 9f8b017..337200d 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.tsx @@ -59,9 +59,9 @@ export const ListBoxGroup: FC = ({ ].join(" ")} > {type === "items" - ? data.map((auto) => ( + ? data.map((auto, i) => ( events.onAutomationDelete(auto.id)} diff --git a/webapp/src/components/AutomationManager/Sidebar/index.css b/webapp/src/components/AutomationManager/Sidebar/index.css index dd5060f..a1dc223 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.css +++ b/webapp/src/components/AutomationManager/Sidebar/index.css @@ -5,7 +5,7 @@ --bottom-height: 3em; position: relative; height: 100%; - min-width: 25vw; + min-width: calc(min(max(25vw, 35em), 50vw)); } .automation-manager--sidebar-box--nav { diff --git a/webapp/src/haService/HAEntities.ts b/webapp/src/haService/HAEntities.ts index 0f13e5d..5fb8ff0 100644 --- a/webapp/src/haService/HAEntities.ts +++ b/webapp/src/haService/HAEntities.ts @@ -12,6 +12,8 @@ export const useHAEntities = (): HAEntitiesState => { new Set(Object.keys(state).map((x) => x.split(".")[0])).keys() ).sort(), })); + entities.collection?.subscribe(console.log); + console.log("ha entities refreshing..."); const entitySource = useHassCollection(entitySourceColl); return createHAEntitiesState(entities, entitySource); }; diff --git a/webapp/src/haService/useHassCollection.ts b/webapp/src/haService/useHassCollection.ts index cf341fe..d3fe64a 100644 --- a/webapp/src/haService/useHassCollection.ts +++ b/webapp/src/haService/useHassCollection.ts @@ -1,59 +1,62 @@ import { Collection, Connection } from "home-assistant-js-websocket"; -import { useState, useEffect } from 'react'; +import { useState, useEffect } from "react"; import { useHAConnection } from "./connection"; -type _HACollectionState = { - ready: false, - collection?: Collection, -} | { - ready: true, - collection: Collection, -} -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(); - - useEffect(() => { - if (conn.status === 'loaded' && !state.ready) { - const collection = getHACollection(conn.connection); - setState({ - ready: false, - collection, - }) - 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, - get additional(): Partial { - if (state.ready && updateAdditionalProps) { - return updateAdditionalProps(state.collection.state) - } - return {} - } +type _HACollectionState = + | { + ready: false; + collection?: Collection; } -} \ No newline at end of file + | { + ready: true; + collection: Collection; + }; +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(); + + useEffect(() => { + if (conn.status === "loaded" && !state.ready) { + const collection = getHACollection(conn.connection); + setState({ + ready: false, + collection, + }); + const unsub = collection.subscribe((d) => { + console.log("updating collection", 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, + get additional(): Partial { + if (state.ready && updateAdditionalProps) { + return updateAdditionalProps(state.collection.state); + } + return {}; + }, + }; +}; diff --git a/webapp/src/routes/automation.stories.tsx b/webapp/src/routes/automation.stories.tsx new file mode 100644 index 0000000..0471433 --- /dev/null +++ b/webapp/src/routes/automation.stories.tsx @@ -0,0 +1,16 @@ +import { useConnectedApiService } from "apiService"; +import { makeStory } from "devUtils"; +import { AutomationRoute } from "./automation"; + +const { make, componentMeta } = makeStory({ + Component: () => { + return ; + }, + meta: { + title: "App/Routes", + }, +}); + +export default componentMeta; + +export const Automations = make({}); diff --git a/webapp/src/routes/automation.tsx b/webapp/src/routes/automation.tsx index 825c69c..82ac37e 100644 --- a/webapp/src/routes/automation.tsx +++ b/webapp/src/routes/automation.tsx @@ -1,13 +1,27 @@ import { ApiService } from "apiService/core"; -import { ConnectedAutomationList } from "components/AutomationList"; -import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; import { Page } from "components/Page"; import { FC } from "react"; +import { useHA } from "haService"; +import { AutomationManager } from "components/AutomationManager"; export const AutomationRoute: FC<{ api: ApiService }> = ({ api }) => { + const ha = useHA(); return ( - + { + ha.callService( + "automation", + on ? "turn_on" : "turn_off", + {}, + { + entity_id: eid, + } + ); + }} + haEntities={ha.entities} + /> ); };