From 05d09830bb1cf6a03ac2830540a4518d670cdaad Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 27 Jul 2022 21:32:31 -0400 Subject: [PATCH] minor clean up --- .../AutomationListV2/Sidebar/index.css | 2 +- .../Sidebar/index.stories.tsx | 170 ++++++++---------- .../AutomationListV2/Sidebar/index.tsx | 53 +++--- .../src/components/AutomationListV2/TagDB.ts | 11 +- .../src/components/AutomationListV2/index.tsx | 17 ++ 5 files changed, 122 insertions(+), 131 deletions(-) create mode 100644 webapp/src/components/AutomationListV2/index.tsx diff --git a/webapp/src/components/AutomationListV2/Sidebar/index.css b/webapp/src/components/AutomationListV2/Sidebar/index.css index 76dee87..d14bc5b 100644 --- a/webapp/src/components/AutomationListV2/Sidebar/index.css +++ b/webapp/src/components/AutomationListV2/Sidebar/index.css @@ -6,7 +6,7 @@ margin-right: 5em; } -.automation-list { +.automation-list .automation-list-box--list { max-height: 100vh; overflow: auto; } \ No newline at end of file diff --git a/webapp/src/components/AutomationListV2/Sidebar/index.stories.tsx b/webapp/src/components/AutomationListV2/Sidebar/index.stories.tsx index 09bc1c7..6632b3a 100644 --- a/webapp/src/components/AutomationListV2/Sidebar/index.stories.tsx +++ b/webapp/src/components/AutomationListV2/Sidebar/index.stories.tsx @@ -3,135 +3,111 @@ import { ComponentMeta, ComponentStory } from "@storybook/react"; import { Page } from "components/Page"; import { AutomationListSidebar } from "."; import { makeTagDB } from "../TagDB"; -import { useMockHAEntities } from "haService/HAEntities.mock"; -import { createMockAuto } from "utils/mocks"; import { useState } from "react"; +import { AutomationListAuto } from "../types"; export default { title: "App/AutomationList/Sidebar", component: AutomationListSidebar, parameters: { actions: { argTypesRegex: "^on.*" } }, - args: { - title: "Example", - description: "Cool!", - state: "on", - tags: { - room: "main bedroom", - routine: "night time", - }, - }, + args: {}, } as ComponentMeta; -export const Loading: ComponentStory = (args) => { +const Template: ComponentStory = (args) => { + const [automations, setAutomations] = useState(args.automations); return ( - - ); -}; - -export const Mocked: ComponentStory = (args) => { - const [mockAutos, setAutos] = useState([ - createMockAuto({ room: "bedroom" }), - createMockAuto({ room: "bathroom", type: "routine" }), - createMockAuto({ type: "routine" }), - createMockAuto(), - createMockAuto({ type: "missing in ws" }), - ]); - const [mockEntitiesStates, setMES] = useState>({ - ...mockAutos.slice(0, 4).reduce((all, auto, i) => { - const eid = `automation.number_${i}`; - return { - ...all, - [eid]: i % 2 == 0 ? "on" : "off", - }; - }, {}), - "automation.number_not_in_config": "on", - }); - const mockEntities = mockAutos.slice(0, 4).reduce((all, auto, i) => { - const eid = `automation.number_${i}`; - return { - ...all, - [eid]: { - entity_id: eid, - attributes: { - id: auto.metadata.id, - }, - state: mockEntitiesStates[eid] ?? "n/a", - }, - }; - }, {}); - const haEntities = useMockHAEntities({ - loading: false, - entities: { - ...mockEntities, - "automation.number_not_in_config": { - entity_id: "automation.number_not_in_config", - state: mockEntitiesStates["automation.number_not_in_config"] ?? "n/a", - attributes: { - id: "1235", - friendly_name: "What am I?", - }, - } as any, - }, - entitySource: {}, - }); - return ( - - metadata)} + tagsDB={makeTagDB(automations)} + automations={automations} onAutomationDelete={(aid) => { args.onAutomationDelete(aid); - setAutos(mockAutos.filter(({ metadata }) => metadata.id !== aid)); + setAutomations(automations.filter(({ id }) => id !== aid)); }} onAutomationUpdate={(a, aid, eid) => { args.onAutomationUpdate(a, aid, eid); - const index = mockAutos.findIndex( - ({ metadata }) => metadata.id === aid - ); + const index = automations.findIndex(({ id }) => id === aid); if (index >= 0) { - setAutos([ - ...mockAutos.slice(0, index), + setAutomations([ + ...automations.slice(0, index), { - ...mockAutos[index], - metadata: { - ...mockAutos[index].metadata, - alias: a.title, - description: a.description, - }, + ...automations[index], + ...a, }, - ...mockAutos.slice(index + 1), + ...automations.slice(index + 1), ]); } - setMES({ - ...mockEntitiesStates, - [eid]: a.state, - }); }} onTagUpdate={(tags, aid) => { args.onTagUpdate(tags, aid); - const index = mockAutos.findIndex( - ({ metadata }) => metadata.id === aid - ); + const index = automations.findIndex(({ id }) => id === aid); if (index >= 0) { - setAutos([ - ...mockAutos.slice(0, index), + setAutomations([ + ...automations.slice(0, index), { - ...mockAutos[index], + ...automations[index], tags, }, - ...mockAutos.slice(index + 1), + ...automations.slice(index + 1), ]); } }} + onAutomationAdd={() => { + args.onAutomationAdd(); + const id = String(Date.now()); + setAutomations([ + ...automations, + { + id, + description: "", + title: "new", + tags: {}, + entityId: "automation." + id, + isSelected: true, + state: "on", + }, + ]); + }} /> ); }; + +const make = (automations: AutomationListAuto[]) => { + const Basic = Template.bind({}); + Basic.args = { + ...Basic.args, + automations, + }; + return Basic; +}; +const makeAuto = (data: Partial): AutomationListAuto => ({ + id: String(Date.now()), + entityId: `automation.${Date.now()}`, + title: "", + description: "", + state: "on", + isSelected: false, + tags: {}, + ...data, +}); + +export const Empty = make([]); +export const Basic = make([ + makeAuto({ title: "example 1" }), + makeAuto({ title: "example 2", state: "off" }), + makeAuto({ title: "example 3", state: "invalid" }), + makeAuto({ title: "example 4", issue: "I have an issue!" }), +]); +export const Tagged = make([ + makeAuto({ title: "example 1", tags: { room: "bathroom", floor: "1" } }), + makeAuto({ title: "example 2", tags: { type: "routine" } }), + makeAuto({ + title: "example 3", + tags: { room: "living room", type: "climate" }, + }), + makeAuto({ + title: "example 4", + tags: { room: "bedroom", floor: "2", type: "button" }, + }), +]); diff --git a/webapp/src/components/AutomationListV2/Sidebar/index.tsx b/webapp/src/components/AutomationListV2/Sidebar/index.tsx index 389e3a0..1b02b44 100644 --- a/webapp/src/components/AutomationListV2/Sidebar/index.tsx +++ b/webapp/src/components/AutomationListV2/Sidebar/index.tsx @@ -2,23 +2,19 @@ import "./index.css"; import { FC, useState } from "react"; -import Skeleton from "@mui/material/Skeleton"; - -import { HAEntitiesState } from "haService/HAEntities"; -import { AutomationMetadata } from "types/automations"; import InputText from "components/Inputs/InputText"; import InputMultiSelect from "components/Inputs/InputMultiSelect"; import { ListBoxGroup } from "../ListBoxGroup"; -import { consolidateAutomations, filterAutomations } from "../helpers"; +import { filterAutomations } from "../helpers"; import { TagDB } from "../TagDB"; import { convertGroupingToItems, makeGrouping } from "../automationGrouper"; -import { AutomationListAutoUpdatable } from "../types"; +import { AutomationListAuto, AutomationListAutoUpdatable } from "../types"; +import { Button } from "components/Inputs/Button"; export type AutomationListSidebarProps = { - configAutomationMetadatas: AutomationMetadata[]; tagsDB: TagDB; - haEntites: HAEntitiesState; + automations: AutomationListAuto[]; onTagUpdate: (t: Record, aid: string) => void; onAutomationUpdate: ( a: AutomationListAutoUpdatable, @@ -26,11 +22,12 @@ export type AutomationListSidebarProps = { eid: string ) => void; onAutomationDelete: (aid: string) => void; + onAutomationAdd: () => void; }; export const AutomationListSidebar: FC = ({ - configAutomationMetadatas, + automations, tagsDB, - haEntites, + onAutomationAdd, ...events }) => { const [searchText, setSearchText] = useState(""); @@ -38,16 +35,7 @@ export const AutomationListSidebar: FC = ({ const [selectedAutomationId, setSelectedAutomationId] = useState(null); - if (!haEntites.ready) { - return ; - } const tags = tagsDB.getTagNames([]); - let automations = consolidateAutomations( - haEntites.collection.state, - configAutomationMetadatas, - selectedAutomationId ?? "", - tagsDB - ); if (searchText.trim().length > 0) { automations = filterAutomations( @@ -78,16 +66,23 @@ export const AutomationListSidebar: FC = ({ max={3} /> - {items.map((item, i) => ( - - ))} +
+ {items.map((item, i) => ( + + ))} +
+
+ +
); }; diff --git a/webapp/src/components/AutomationListV2/TagDB.ts b/webapp/src/components/AutomationListV2/TagDB.ts index 84bf3bb..d4089c3 100644 --- a/webapp/src/components/AutomationListV2/TagDB.ts +++ b/webapp/src/components/AutomationListV2/TagDB.ts @@ -1,10 +1,13 @@ -import { AutomationData } from "types/automations"; - export type TagDB = ReturnType; -export const makeTagDB = (automations: AutomationData[]) => { +export const makeTagDB = ( + automations: Array<{ + id: string; + tags: Record; + }> +) => { const allTags: Record> = {}; const automationTags: Record> = {}; - automations.forEach(({ metadata: { id }, tags }) => { + automations.forEach(({ id, tags }) => { automationTags[id] = tags; Object.keys(tags).forEach((tagName) => { const tagValue = tags[tagName]; diff --git a/webapp/src/components/AutomationListV2/index.tsx b/webapp/src/components/AutomationListV2/index.tsx new file mode 100644 index 0000000..5598b25 --- /dev/null +++ b/webapp/src/components/AutomationListV2/index.tsx @@ -0,0 +1,17 @@ +import { ListData } from "apiService/types"; +import { HAEntitiesState } from "haService/HAEntities"; +import { FC } from "react"; +import { AutomationData } from "types/automations"; + +export type AutomationListProps = { + haEntites: HAEntitiesState; + configAutomations: ListData; + onUpdate: (i: number, auto: AutomationData) => void; + onUpdateTags: (id: string, tags: Record) => void; + onAdd: (auto: AutomationData) => void; + onRemove: (i: number) => void; +}; + +export const AutomationList: FC = (props) => { + return <>; +};