diff --git a/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx b/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx index 24c58d9..44cc6c6 100644 --- a/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx +++ b/webapp/src/components/AutomationManager/ListBoxGroup/index.stories.tsx @@ -1,53 +1,51 @@ -import "styles/root.css"; -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { Page } from "components/Page"; import { ListBoxGroup } from "."; -export default { - title: "App/AutomationList/ListBoxGroup", - component: ListBoxGroup, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - title: "Example", - type: "items", - isSelected: false, - data: [ - { - id: "1", - title: "Example", - entityId: "automation.1", - description: "Amazing automation!", - tags: {}, - state: "on", - isSelected: false, - }, - { - id: "2", - title: "Example 2", - entityId: "automation.2", - description: "Cool!", - state: "on", - isSelected: false, - tags: { - room: "main bedroom", - routine: "night time", - }, - }, - ], +import { makeStory } from "devUtils"; +import { useTagDB } from "../TagDB"; + +const { make, componentMeta } = makeStory({ + Component: ListBoxGroup, + BaseTemplate: (args) => { + const tagsDB = useTagDB([], () => {}); + return ; }, -} as ComponentMeta; + meta: { + title: "App/AutomationManager/ListBoxGroup", + args: { + title: "Example", + type: "items", + isSelected: false, + data: [ + { + id: "1", + title: "Example", + entityId: "automation.1", + description: "Amazing automation!", + tags: {}, + state: "on", + isSelected: false, + }, + { + id: "2", + title: "Example 2", + entityId: "automation.2", + description: "Cool!", + state: "on", + isSelected: false, + tags: { + room: "main bedroom", + routine: "night time", + }, + }, + ], + }, + }, +}); -const Template: ComponentStory = (args) => { - return ( - - - - ); -}; +export default componentMeta; -export const Basic = Template.bind({}); -export const Nested = Template.bind({}); -Nested.args = { +export const Basic = make({}); +export const Nested = make({ title: "Top Group", type: "group", isSelected: false, @@ -120,4 +118,4 @@ Nested.args = { ], }, ], -}; +}); diff --git a/webapp/src/components/AutomationManager/Loaded/index.tsx b/webapp/src/components/AutomationManager/Loaded/index.tsx new file mode 100644 index 0000000..7cb3d2b --- /dev/null +++ b/webapp/src/components/AutomationManager/Loaded/index.tsx @@ -0,0 +1,34 @@ +import { HassEntities } from "home-assistant-js-websocket"; +import { FC } from "react"; +import { AutomationData } from "types/automations"; +import { consolidateAutomations } from "../helpers"; +import { useTagDB } from "../TagDB"; +import { useAutomationManagerState } from "./state"; + +export type AutomationManagerLoadedProps = { + configAutomations: AutomationData[]; + hassEntities: HassEntities; + onUpdateTags: (aid: string, tags: Record) => void; +}; +export const AutomationManagerLoaded: FC = ({ + configAutomations, + onUpdateTags, + hassEntities, +}) => { + const state = useAutomationManagerState(); + const tagsDB = useTagDB( + configAutomations.map(({ metadata: { id }, tags }) => ({ + id, + tags, + })), + onUpdateTags + ); + + const automations = consolidateAutomations( + hassEntities, + configAutomations.map(({ metadata }) => metadata), + tagsDB + ); + + return <>; +}; diff --git a/webapp/src/components/AutomationManager/state.ts b/webapp/src/components/AutomationManager/Loaded/state.ts similarity index 100% rename from webapp/src/components/AutomationManager/state.ts rename to webapp/src/components/AutomationManager/Loaded/state.ts diff --git a/webapp/src/components/AutomationManager/MetadataBox/index.stories.tsx b/webapp/src/components/AutomationManager/MetadataBox/index.stories.tsx index 1328f5f..6a61d78 100644 --- a/webapp/src/components/AutomationManager/MetadataBox/index.stories.tsx +++ b/webapp/src/components/AutomationManager/MetadataBox/index.stories.tsx @@ -1,59 +1,44 @@ -import "styles/root.css"; -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { Page } from "components/Page"; import { MetadataBox } from "."; import { useTagDB } from "../TagDB"; import { useState } from "react"; -export default { - title: "App/AutomationList/MetadataBox", - component: MetadataBox, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - id: "dummy", - title: "Example", - description: "Cool!", - state: "on", - isSelected: false, - tags: { - room: "main bedroom", - routine: "night time", +import { makeStory } from "devUtils"; + +const { make, componentMeta } = makeStory({ + Component: MetadataBox, + meta: { + title: "App/AutomationManager/MetadataBox", + args: { + id: "dummy", + title: "Example", + description: "Cool!", + state: "on", + isSelected: false, + tags: { + room: "main bedroom", + routine: "night time", + }, }, }, -} as ComponentMeta; + BaseTemplate: (args) => { + const [autoTags, setTags] = useState([{ id: args.id, tags: args.tags }]); + const tagsDB = useTagDB(autoTags, (aid, t) => + setTags( + autoTags + .filter(({ id }) => id !== aid) + .concat({ + id: aid, + tags: t, + }) + ) + ); + return ; + }, +}); -const Template: ComponentStory = (args) => { - const [autoTags, setTags] = useState([{ id: args.id, tags: args.tags }]); - const tagsDB = useTagDB(autoTags, (aid, t) => - setTags( - autoTags - .filter(({ id }) => id !== aid) - .concat({ - id: aid, - tags: t, - }) - ) - ); - return ( - - - - ); -}; +export default componentMeta; -export const Basic = Template.bind({}); -export const IsSelected = Template.bind({}); -IsSelected.args = { - ...IsSelected.args, - isSelected: true, -}; -export const InvalidState = Template.bind({}); -InvalidState.args = { - ...InvalidState.args, - state: "unavailable", -}; -export const WithIssue = Template.bind({}); -WithIssue.args = { - ...WithIssue.args, - issue: "look at me", -}; +export const Basic = make({}); +export const IsSelected = make({ isSelected: true }); +export const InvalidState = make({ state: "unavailable" }); +export const WithIssue = make({ issue: "look at me" }); diff --git a/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx b/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx index b7b49b3..5800243 100644 --- a/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx +++ b/webapp/src/components/AutomationManager/Sidebar/index.stories.tsx @@ -1,38 +1,34 @@ -import "styles/root.css"; -import { ComponentMeta, ComponentStory } from "@storybook/react"; -import { Page } from "components/Page"; import { AutomationListSidebar } from "."; import { useTagDB } from "../TagDB"; import { useState } from "react"; import { AutomationListAuto } from "../types"; -export default { - title: "App/AutomationList/Sidebar", - component: AutomationListSidebar, - parameters: { actions: { argTypesRegex: "^on.*" } }, - args: {}, -} as ComponentMeta; +import { makeStory } from "devUtils"; -const Template: ComponentStory = (args) => { - const [automations, setAutomations] = useState(args.automations); - const [selectedAutomationId, setSelAutoId] = useState( - args.selectedAutomationId - ); - const onTagUpdate = (tags: Record, aid: string) => { - const index = automations.findIndex(({ id }) => id === aid); - if (index >= 0) { - setAutomations([ - ...automations.slice(0, index), - { - ...automations[index], - tags, - }, - ...automations.slice(index + 1), - ]); - } - }; - return ( - +const story = makeStory({ + meta: { + title: "App/AutomationManager/Sidebar", + }, + Component: AutomationListSidebar, + BaseTemplate: (args) => { + const [automations, setAutomations] = useState(args.automations); + const [selectedAutomationId, setSelAutoId] = useState( + args.selectedAutomationId + ); + const onTagUpdate = (tags: Record, aid: string) => { + const index = automations.findIndex(({ id }) => id === aid); + if (index >= 0) { + setAutomations([ + ...automations.slice(0, index), + { + ...automations[index], + tags, + }, + ...automations.slice(index + 1), + ]); + } + }; + return ( onTagUpdate(t, a))} @@ -76,18 +72,16 @@ const Template: ComponentStory = (args) => { setSelAutoId(id); }} /> - - ); -}; + ); + }, +}); -const make = (automations: AutomationListAuto[]) => { - const Basic = Template.bind({}); - Basic.args = { - ...Basic.args, +export default story.componentMeta; + +const make = (automations: AutomationListAuto[]) => + story.make({ automations, - }; - return Basic; -}; + }); const makeAuto = (data: Partial): AutomationListAuto => { const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`; return { diff --git a/webapp/src/components/AutomationManager/index.stories.tsx b/webapp/src/components/AutomationManager/index.stories.tsx new file mode 100644 index 0000000..f4d37e0 --- /dev/null +++ b/webapp/src/components/AutomationManager/index.stories.tsx @@ -0,0 +1,12 @@ +import { makeStory } from "devUtils"; +import { AutomationManager } from "."; + +const { make, componentMeta } = makeStory({ + Component: AutomationManager, + meta: { + title: "App/AutomationManager", + }, +}); + +export default componentMeta; +export const Basic = make({}); diff --git a/webapp/src/components/AutomationManager/index.tsx b/webapp/src/components/AutomationManager/index.tsx index 03e588d..5de8b8c 100644 --- a/webapp/src/components/AutomationManager/index.tsx +++ b/webapp/src/components/AutomationManager/index.tsx @@ -1,50 +1,52 @@ -import { ListData } from "apiService/types"; +import { ApiService } from "apiService/core"; import { HAEntitiesState } from "haService/HAEntities"; -import { FC } from "react"; -import { AutomationData } from "types/automations"; -import { consolidateAutomations } from "./helpers"; -import { AutomationListSidebar } from "./Sidebar"; -import { useAutomationManagerState } from "./state"; -import { useTagDB } from "./TagDB"; +import { FC, useState } from "react"; +import { AutomationManagerLoaded } from "./Loaded"; +import LinearProgress from "@mui/material/LinearProgress"; -export type AutomationListProps = { +export type AutomationManagerProps = { haEntites: HAEntitiesState; - configAutomations: ListData; - onUpdate: (i: number, auto: AutomationData) => void; - onUpdateTags: (id: string, tags: Record) => void; - onUpdateState: (id: string, state: string) => void; - onTrigger: (id: string, state: string) => void; - onAdd: () => void; - onRemove: (i: number) => void; + api: ApiService; }; -export const AutomationList: FC = (props) => { - const state = useAutomationManagerState(); +export const AutomationManager: FC = ({ + api, + haEntites, +}) => { + const [isSaving, setIsSaving] = useState(false); - if (!props.haEntites.ready) { + // handle bad states + if (!haEntites.ready || !api.state.automations.ready) { return
...
; } - const tagsDB = useTagDB( - props.configAutomations.data.map(({ metadata: { id }, tags }) => ({ - id, - tags, - })), - props.onUpdateTags - ); - const automations = consolidateAutomations( - props.haEntites.collection.state, - props.configAutomations.data.map(({ metadata }) => metadata), - tagsDB - ); + if (!api.state.automations.ok) { + return ( +
+

Failed to load

+ {api.state.automations.error} +
+ ); + } + + // alias + const configAutomations = api.state.automations.data.data; + const hassEntities = haEntites.collection.state; return (
- {/* */} + {isSaving && } + { + setIsSaving(true); + await api.updateTags({ + automation_id: aid, + tags: t, + }); + setIsSaving(false); + }} + />
); }; diff --git a/webapp/src/devUtils/example.stories.tsx b/webapp/src/devUtils/example.stories.tsx new file mode 100644 index 0000000..dda96e5 --- /dev/null +++ b/webapp/src/devUtils/example.stories.tsx @@ -0,0 +1,11 @@ +import { makeStory } from "devUtils"; + +const { make, componentMeta } = makeStory({ + Component: () =>
Example
, + meta: { + title: "DevUtil", + }, +}); + +export default componentMeta; +export const Example = make({}); diff --git a/webapp/src/devUtils/index.tsx b/webapp/src/devUtils/index.tsx new file mode 100644 index 0000000..9bcb442 --- /dev/null +++ b/webapp/src/devUtils/index.tsx @@ -0,0 +1,47 @@ +import "styles/root.css"; +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { Page } from "components/Page"; +import { ComponentProps, JSXElementConstructor } from "react"; + +export type MakeStoryArgs< + T extends keyof JSX.IntrinsicElements | JSXElementConstructor +> = { + Component: T; + BaseTemplate?: ComponentStory; + meta: Partial> & { title: string }; +}; +export const makeStory = < + T extends keyof JSX.IntrinsicElements | JSXElementConstructor +>({ + BaseTemplate, + Component, + meta, +}: MakeStoryArgs) => { + const componentMeta: ComponentMeta = { + component: Component as any, + parameters: { actions: { argTypesRegex: "^on.*" } }, + args: {}, + ...meta, + }; + const Template: ComponentStory = (args) => { + return ( + + {BaseTemplate ? : } + + ); + }; + + const make = (args: Partial>) => { + const Basic = Template.bind({}); + Basic.args = { + ...Basic.args, + ...args, + }; + return Basic; + }; + + return { + make, + componentMeta, + }; +};