From afc67a17ec534e4702995003ea6bbb141da53e03 Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 20 Apr 2022 15:40:26 -0400 Subject: [PATCH] automation list box --- .../AutomationList/AutomationList.stories.tsx | 1 + .../AutomationList/AutomationListBox.css | 55 ++++++++++- .../AutomationListBox.stories.tsx | 45 +++++++++ .../AutomationList/AutomationListBox.test.ts | 55 +++++++---- .../AutomationList/AutomationListBox.tsx | 93 ++++++++++++++++--- webapp/src/utils/mocks.ts | 5 +- 6 files changed, 220 insertions(+), 34 deletions(-) create mode 100644 webapp/src/components/AutomationList/AutomationListBox.stories.tsx diff --git a/webapp/src/components/AutomationList/AutomationList.stories.tsx b/webapp/src/components/AutomationList/AutomationList.stories.tsx index 581797f..a28b0f2 100644 --- a/webapp/src/components/AutomationList/AutomationList.stories.tsx +++ b/webapp/src/components/AutomationList/AutomationList.stories.tsx @@ -1,3 +1,4 @@ +import "styles/root.css"; import { ComponentMeta, ComponentStory } from "@storybook/react"; import { useState } from "react"; import { AutomationList } from "."; diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css index e5d56da..eafa4eb 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.css +++ b/webapp/src/components/AutomationList/AutomationListBox.css @@ -1,3 +1,56 @@ .automation-list-box { - + display: flex; + flex-direction: column; + height: 100%; +} + +.automation-list-box--body { + overflow: scroll; + height: 100%; + flex: 1; +} +.automation-list-box--body--group { + padding-left: 1em; + border-radius: 10px; + padding-bottom: 0.5em; + border-width: 1px; + border-bottom-style: solid ; + border-color: white; + user-select: none; +} +.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 { + cursor: pointer; + padding: 0.1em; +} +.automation-list-box--body--group > b:hover { + background-color: var(--secondary-accent); +} + +.automation-list-box--body--item:hover { + background-color: var(--primary-accent); +} +.automation-list-box--body--item { + padding-left: 1em; + cursor: pointer; +} +.automation-list-box--body--item::before { + content: "⊚ "; +} +.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 > button { + padding: 0.5em 2em; } \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx new file mode 100644 index 0000000..de6fe3c --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx @@ -0,0 +1,45 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; +import { AutomationListBox } from "./AutomationListBox"; +import { NODE_HEIGHT, NODE_WIDTH, ADD_HEIGHT, ADD_WIDTH, CIRCLE_SIZE, DISTANCE_FACTOR } from 'components/DAGSvgs/constants'; +import { createMockAuto } from "utils/mocks"; + +export default { + title: 'App/AutomationListBox', + component: AutomationListBox, + parameters: { actions: { argTypesRegex: '^on.*' } }, + args: { + } +} as ComponentMeta; + + +const Template: ComponentStory = args => { + const [autos, setAutos] = useState(args.automations) + console.log({ args }) + return
+ +
+} + +export const EmptyStart = Template.bind({}) +EmptyStart.args = { + ...EmptyStart.args, + automations: [], +} + +export const FewAutos = Template.bind({}) +FewAutos.args = { + ...FewAutos.args, + automations: [ + createMockAuto({ "Room": "Bathroom", "Type": "Lights" }), + 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(), + ], +} diff --git a/webapp/src/components/AutomationList/AutomationListBox.test.ts b/webapp/src/components/AutomationList/AutomationListBox.test.ts index 6570dde..826fcfc 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.test.ts +++ b/webapp/src/components/AutomationList/AutomationListBox.test.ts @@ -1,7 +1,8 @@ -import { groupAutomations } from './AutomationListBox'; +import { groupAutomations, getTagList } from './AutomationListBox'; import { AutomationData } from 'types/automations/index'; +import { createMockAuto } from 'utils/mocks'; -test('converting time to a string', () => { +test('group automations', () => { const automations: AutomationData[] = [ { metadata: { @@ -46,30 +47,52 @@ test('converting time to a string', () => { trigger: [], }, ]; - expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toBe({ + expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toStrictEqual({ "Living Room": { - "Lights": ["1"], - "Climate": ["2"], + "Lights": [0], + "Climate": [1], }, "Kitchen": { - "Climate": ["3"], + "Climate": [2], } }) - expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toBe({ + expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toStrictEqual({ "Lights": { - "Living Room": ['1'], + "Living Room": [0], }, "Climate": { - "Living Room": ['2'], - "Kitchen": ['3'] + "Living Room": [1], + "Kitchen": [2] } }) - expect(groupAutomations(automations, [1], ['Room', "Type"])).toBe({ - "Lights": ['1'], - "Climate": ['2', '3'] + expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({ + "Lights": [0], + "Climate": [1, 2] }) - expect(groupAutomations(automations, [0], ['Room', "Type"])).toBe({ - "Living Room": ['1', '2'], - "Kitchen": ['3'] + expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({ + "Living Room": [0, 1], + "Kitchen": [2] }) + + + expect(groupAutomations(automations, [], ['Room', "Type"])).toStrictEqual([ + 0, 1, 2 + ]) }); + + +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"]) +}) \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index e86b4f9..826612b 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -3,21 +3,26 @@ 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"; export type Props = { automations: AutomationData[]; + onSelectAutomation: (i: number) => void; + onAdd: () => void; } export const AutomationListBox: FC = ({ - automations + automations, + onSelectAutomation, + onAdd, }) => { const tags = getTagList(automations); const [searchText, setSearchText] = useState(''); const [selected, setSelected] = useState([]); const filteredAutomations = filterAutomations(automations, searchText, selected, tags); - + const groups = groupAutomations(filteredAutomations.map(([a, _]) => a), selected, tags); return
@@ -34,17 +39,26 @@ export const AutomationListBox: FC = ({ />
- + {convertGroupsToItems(groups, filteredAutomations, onSelectAutomation)} +
+
+ + Showing Automations: {filteredAutomations.length} out of {automations.length}
} -const getTagList = (autos: AutomationData[]): string[] => autos +export const getTagList = (autos: AutomationData[]): string[] => Object.keys(autos .map(a => Object.keys(a.metadata.tags)) - .reduce((all, keys) => all.concat(keys)) + .reduce((all: any, keys) => { + for (const k of keys) { + all[k] = 1 + } + return all; + }, {})) -const filterAutomations = (autos: AutomationData[], searchText: string, selected: number[], tags: string[]) => autos.filter(a => { +const filterAutomations = (autos: AutomationData[], searchText: string, selected: number[], tags: string[]): Array<[AutomationData, number]> => autos.map<[AutomationData, number]>((a, i) => [a, i]).filter(([a, i]) => { if (searchText.length > 0) { if ( !( @@ -65,10 +79,18 @@ const filterAutomations = (autos: AutomationData[], searchText: string, selected return true }) -export type GroupedAutos = Record; -export const groupAutomations = (autos: AutomationData[], selected: number[], tags: string[]) => { - const out: GroupedAutos = {}; - for (const a of autos) { + +export const groupAutomations = ( + autos: AutomationData[], + selected: number[], + tags: string[], +) => { + if (selected.length === 0) { + return autos.map((_, i) => i); + } + const out: any = {}; + for (let ai = 0; ai < autos.length; ai++) { + const a = autos[ai]; let where = out; let tag: string = ''; let lastKey: string | null = null; @@ -82,12 +104,53 @@ export const groupAutomations = (autos: AutomationData[], selected: number[], ta } lastKey = a.metadata.tags[tag] } - if (tag) { - if (!where[a.metadata.tags[tag]]) { - where[a.metadata.tags[tag]] = [] + if (lastKey !== null) { + if (!where[lastKey] || !Array.isArray(where[lastKey])) { + where[lastKey] = [] } - where[a.metadata.tags[tag]].push(a.metadata.id) + where[lastKey].push(ai) } } return out; -} \ No newline at end of file +} + +const convertGroupsToItems = ( + groups: any, + autos: Array<[AutomationData, number]>, + onSelectAutomation: (i: number) => void, +) => { + if (Array.isArray(groups)) { + return groups.map(i =>
onSelectAutomation(autos[i][1])}> + {autos[i][0].metadata.id} {autos[i][0].metadata.alias} +
) + } else { + return Object.keys(groups).map(groupName => { + return + }) + } +} + + +const AutomationListBoxGroup: FC<{ + groupName: string, + groups: any, + autos: Array<[AutomationData, number]>, + onSelectAutomation: (i: number) => void, +}> = ({ + groupName, + groups, + autos, + onSelectAutomation, +}) => { + const [open, setOpen] = useState(false); + return
+ setOpen(!open)}>{groupName} {!open ? "⊕" : "⊖"} + {open && convertGroupsToItems(groups, autos, onSelectAutomation)} +
+ } \ No newline at end of file diff --git a/webapp/src/utils/mocks.ts b/webapp/src/utils/mocks.ts index b4b6eb4..a4a5aa4 100644 --- a/webapp/src/utils/mocks.ts +++ b/webapp/src/utils/mocks.ts @@ -1,15 +1,16 @@ import { AutomationData } from 'types/automations'; -export const createMockAuto = (): AutomationData => ({ +export const createMockAuto = (tags: Record = {}): AutomationData => ({ metadata: { - id: "random", + id: Math.random().toString(36).slice(2, 5), alias: "Random", description: "Example Metadata", trigger_variables: { 'wowo': '!' }, mode: 'single', + tags }, trigger: [ {