automation list box

This commit is contained in:
ari committed 2022-04-20 15:40:26 -04:00
1 parent 248660ff30
commit afc67a17ec
6 files changed
+218 -32

No files matched your search

@@ -1,3 +1,4 @@
import "styles/root.css";
import { ComponentMeta, ComponentStory } from "@storybook/react"; import { ComponentMeta, ComponentStory } from "@storybook/react";
import { useState } from "react"; import { useState } from "react";
import { AutomationList } from "."; import { AutomationList } from ".";
@@ -1,3 +1,56 @@
.automation-list-box { .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;
} }
@@ -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<typeof AutomationListBox>;
const Template: ComponentStory<typeof AutomationListBox> = args => {
const [autos, setAutos] = useState(args.automations)
console.log({ args })
return <div className="page">
<AutomationListBox {...args} automations={autos} />
</div>
}
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(),
],
}
@@ -1,7 +1,8 @@
import { groupAutomations } from './AutomationListBox'; import { groupAutomations, getTagList } from './AutomationListBox';
import { AutomationData } from 'types/automations/index'; import { AutomationData } from 'types/automations/index';
import { createMockAuto } from 'utils/mocks';
test('converting time to a string', () => { test('group automations', () => {
const automations: AutomationData[] = [ const automations: AutomationData[] = [
{ {
metadata: { metadata: {
@@ -46,30 +47,52 @@ test('converting time to a string', () => {
trigger: [], trigger: [],
}, },
]; ];
expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toBe({ expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toStrictEqual({
"Living Room": { "Living Room": {
"Lights": ["1"], "Lights": [0],
"Climate": ["2"], "Climate": [1],
}, },
"Kitchen": { "Kitchen": {
"Climate": ["3"], "Climate": [2],
} }
}) })
expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toBe({ expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toStrictEqual({
"Lights": { "Lights": {
"Living Room": ['1'], "Living Room": [0],
}, },
"Climate": { "Climate": {
"Living Room": ['2'], "Living Room": [1],
"Kitchen": ['3'] "Kitchen": [2]
} }
}) })
expect(groupAutomations(automations, [1], ['Room', "Type"])).toBe({ expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({
"Lights": ['1'], "Lights": [0],
"Climate": ['2', '3'] "Climate": [1, 2]
}) })
expect(groupAutomations(automations, [0], ['Room', "Type"])).toBe({ expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({
"Living Room": ['1', '2'], "Living Room": [0, 1],
"Kitchen": ['3'] "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"])
})
@@ -3,21 +3,26 @@ import { FC, useState } from 'react';
import { AutomationData } from "types/automations"; import { AutomationData } from "types/automations";
import InputMultiSelect from "components/Inputs/InputMultiSelect"; import InputMultiSelect from "components/Inputs/InputMultiSelect";
import InputText from "components/Inputs/InputText"; import InputText from "components/Inputs/InputText";
import { Button } from "components/Inputs/Button";
export type Props = { export type Props = {
automations: AutomationData[]; automations: AutomationData[];
onSelectAutomation: (i: number) => void;
onAdd: () => void;
} }
export const AutomationListBox: FC<Props> = ({ export const AutomationListBox: FC<Props> = ({
automations automations,
onSelectAutomation,
onAdd,
}) => { }) => {
const tags = getTagList(automations); const tags = getTagList(automations);
const [searchText, setSearchText] = useState(''); const [searchText, setSearchText] = useState('');
const [selected, setSelected] = useState<number[]>([]); const [selected, setSelected] = useState<number[]>([]);
const filteredAutomations = filterAutomations(automations, searchText, selected, tags); const filteredAutomations = filterAutomations(automations, searchText, selected, tags);
const groups = groupAutomations(filteredAutomations.map(([a, _]) => a), selected, tags);
return <div className="automation-list-box"> return <div className="automation-list-box">
<div className="automation-list-box--nav"> <div className="automation-list-box--nav">
@@ -34,17 +39,26 @@ export const AutomationListBox: FC<Props> = ({
/> />
</div> </div>
<div className="automation-list-box--body"> <div className="automation-list-box--body">
{convertGroupsToItems(groups, filteredAutomations, onSelectAutomation)}
</div>
<div className="automation-list-box--bottom">
<Button onClick={onAdd}>Add</Button>
<span>Showing Automations: {filteredAutomations.length} out of {automations.length}</span>
</div> </div>
</div> </div>
} }
const getTagList = (autos: AutomationData[]): string[] => autos export const getTagList = (autos: AutomationData[]): string[] => Object.keys(autos
.map(a => Object.keys(a.metadata.tags)) .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 (searchText.length > 0) {
if ( if (
!( !(
@@ -65,10 +79,18 @@ const filterAutomations = (autos: AutomationData[], searchText: string, selected
return true return true
}) })
export type GroupedAutos = Record<string, GroupedAutos | string[]>;
export const groupAutomations = (autos: AutomationData[], selected: number[], tags: string[]) => { export const groupAutomations = (
const out: GroupedAutos = {}; autos: AutomationData[],
for (const a of autos) { 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 where = out;
let tag: string = ''; let tag: string = '';
let lastKey: string | null = null; let lastKey: string | null = null;
@@ -82,12 +104,53 @@ export const groupAutomations = (autos: AutomationData[], selected: number[], ta
} }
lastKey = a.metadata.tags[tag] lastKey = a.metadata.tags[tag]
} }
if (tag) { if (lastKey !== null) {
if (!where[a.metadata.tags[tag]]) { if (!where[lastKey] || !Array.isArray(where[lastKey])) {
where[a.metadata.tags[tag]] = [] where[lastKey] = []
} }
where[a.metadata.tags[tag]].push(a.metadata.id) where[lastKey].push(ai)
} }
} }
return out; return out;
} }
const convertGroupsToItems = (
groups: any,
autos: Array<[AutomationData, number]>,
onSelectAutomation: (i: number) => void,
) => {
if (Array.isArray(groups)) {
return groups.map(i => <div key={i} className="automation-list-box--body--item" onClick={() => onSelectAutomation(autos[i][1])}>
{autos[i][0].metadata.id} {autos[i][0].metadata.alias}
</div>)
} else {
return Object.keys(groups).map(groupName => {
return <AutomationListBoxGroup
key={groupName}
groupName={groupName}
groups={groups[groupName]}
autos={autos}
onSelectAutomation={onSelectAutomation}
/>
})
}
}
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 <div className="automation-list-box--body--group">
<b onClick={() => setOpen(!open)}>{groupName} {!open ? "⊕" : "⊖"}</b>
{open && convertGroupsToItems(groups, autos, onSelectAutomation)}
</div>
}
+3 -2
View File
@@ -1,15 +1,16 @@
import { AutomationData } from 'types/automations'; import { AutomationData } from 'types/automations';
export const createMockAuto = (): AutomationData => ({ export const createMockAuto = (tags: Record<string, string> = {}): AutomationData => ({
metadata: { metadata: {
id: "random", id: Math.random().toString(36).slice(2, 5),
alias: "Random", alias: "Random",
description: "Example Metadata", description: "Example Metadata",
trigger_variables: { trigger_variables: {
'wowo': '!' 'wowo': '!'
}, },
mode: 'single', mode: 'single',
tags
}, },
trigger: [ trigger: [
{ {