diff --git a/webapp/.vscode/launch.json b/webapp/.vscode/launch.json index e2db973..eb1f753 100644 --- a/webapp/.vscode/launch.json +++ b/webapp/.vscode/launch.json @@ -2,7 +2,25 @@ "version": "0.2.0", "configurations": [ { - "name": "Debug CRA Tests", + "name": "Debug Tests Single Run", + "type": "node", + "request": "launch", + "env": { + "CI": "true" + }, + "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts", + "args": [ + "test", + "--runInBand", + "--no-cache" + ], + "cwd": "${workspaceRoot}", + "protocol": "inspector", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + }, + { + "name": "Debug tests watch mode", "type": "node", "request": "launch", "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts", @@ -10,20 +28,29 @@ "test", "--runInBand", "--no-cache", - "--watchAll=false" + "--watchAll" ], "cwd": "${workspaceRoot}", "protocol": "inspector", "console": "integratedTerminal", - "internalConsoleOptions": "neverOpen", - "env": { - "CI": "true" - }, - "disableOptimisticBPs": true, - "jasmineExplorer.nodeArgv": [ - "-r", - "ts-node/register" - ] + "internalConsoleOptions": "neverOpen" + }, + { + "name": "Debug tests opened file", + "type": "node", + "request": "launch", + "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts", + "args": [ + "test", + "${fileBasenameNoExtension}", + "--runInBand", + "--no-cache", + "--watchAll" + ], + "cwd": "${workspaceRoot}", + "protocol": "inspector", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" } ] -} +} \ No newline at end of file diff --git a/webapp/package.json b/webapp/package.json index bd58ee3..851aa62 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -70,5 +70,9 @@ "@types/chroma-js": "^2.1.3", "@types/js-yaml": "^4.0.5", "ts-node": "^10.4.0" - } + }, + "moduleDirectories": [ + "node_modules", + "src" + ] } \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css new file mode 100644 index 0000000..e5d56da --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationListBox.css @@ -0,0 +1,3 @@ +.automation-list-box { + +} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBox.test.ts b/webapp/src/components/AutomationList/AutomationListBox.test.ts new file mode 100644 index 0000000..6570dde --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationListBox.test.ts @@ -0,0 +1,75 @@ +import { groupAutomations } from './AutomationListBox'; +import { AutomationData } from 'types/automations/index'; + +test('converting time to a string', () => { + const automations: AutomationData[] = [ + { + metadata: { + id: '1', + alias: '', + description: '', + mode: 'single', + tags: { + "Room": 'Living Room', + "Type": 'Lights' + } + }, + sequence: [], + trigger: [], + }, + { + metadata: { + id: '2', + alias: '', + description: '', + mode: 'single', + tags: { + "Room": 'Living Room', + "Type": 'Climate' + } + }, + sequence: [], + trigger: [], + }, + { + metadata: { + id: '3', + alias: '', + description: '', + mode: 'single', + tags: { + "Room": 'Kitchen', + "Type": 'Climate' + } + }, + sequence: [], + trigger: [], + }, + ]; + expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toBe({ + "Living Room": { + "Lights": ["1"], + "Climate": ["2"], + }, + "Kitchen": { + "Climate": ["3"], + } + }) + expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toBe({ + "Lights": { + "Living Room": ['1'], + }, + "Climate": { + "Living Room": ['2'], + "Kitchen": ['3'] + } + }) + expect(groupAutomations(automations, [1], ['Room', "Type"])).toBe({ + "Lights": ['1'], + "Climate": ['2', '3'] + }) + expect(groupAutomations(automations, [0], ['Room', "Type"])).toBe({ + "Living Room": ['1', '2'], + "Kitchen": ['3'] + }) +}); diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx new file mode 100644 index 0000000..e86b4f9 --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -0,0 +1,93 @@ +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"; + + +export type Props = { + automations: AutomationData[]; +} + +export const AutomationListBox: FC = ({ + automations +}) => { + + const tags = getTagList(automations); + const [searchText, setSearchText] = useState(''); + const [selected, setSelected] = useState([]); + const filteredAutomations = filterAutomations(automations, searchText, selected, tags); + + + return
+
+ + +
+
+ +
+
+} + + +const getTagList = (autos: AutomationData[]): string[] => autos + .map(a => Object.keys(a.metadata.tags)) + .reduce((all, keys) => all.concat(keys)) + +const filterAutomations = (autos: AutomationData[], searchText: string, selected: number[], tags: string[]) => autos.filter(a => { + if (searchText.length > 0) { + if ( + !( + a.metadata.description.toLocaleLowerCase().includes(searchText) || + a.metadata.alias.toLocaleLowerCase().includes(searchText) + ) + ) { + return false + } + } + if (selected.length > 0) { + for (const i of selected) { + if (!a.metadata.tags[tags[i]]) { + return false + } + } + } + return true +}) + +export type GroupedAutos = Record; +export const groupAutomations = (autos: AutomationData[], selected: number[], tags: string[]) => { + const out: GroupedAutos = {}; + for (const a of autos) { + let where = out; + let tag: string = ''; + let lastKey: string | null = null; + for (const i of selected) { + tag = tags[i]; + if (lastKey !== null) { + where = where[lastKey] + } + if (!where[a.metadata.tags[tag]]) { + where[a.metadata.tags[tag]] = {} + } + lastKey = a.metadata.tags[tag] + } + if (tag) { + if (!where[a.metadata.tags[tag]]) { + where[a.metadata.tags[tag]] = [] + } + where[a.metadata.tags[tag]].push(a.metadata.id) + } + } + return out; +} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/styles.css b/webapp/src/components/AutomationList/index.css similarity index 100% rename from webapp/src/components/AutomationList/styles.css rename to webapp/src/components/AutomationList/index.css diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx index 6dca8fd..14201df 100644 --- a/webapp/src/components/AutomationList/index.tsx +++ b/webapp/src/components/AutomationList/index.tsx @@ -1,4 +1,4 @@ -import "./styles.css"; +import "./index.css"; import { AutomationEditor } from "components/AutomationEditor"; import { Button } from "components/Inputs/Button"; import { FC, useState } from "react"; diff --git a/webapp/src/types/automations/index.ts b/webapp/src/types/automations/index.ts index ffa6dde..5aaea9b 100644 --- a/webapp/src/types/automations/index.ts +++ b/webapp/src/types/automations/index.ts @@ -4,22 +4,23 @@ import { AutomationTrigger, TriggerType } from "./triggers"; export interface AutomationMetadata { - id: string; - alias: string; - description: string; - trigger_variables?: Record; - mode: "single" | "restart" | "queued" | "parallel"; + id: string; + alias: string; + description: string; + trigger_variables?: Record; + mode: "single" | "restart" | "queued" | "parallel"; + tags: Record; } export interface AutomationData { - metadata: AutomationMetadata; - trigger: AutomationTrigger[]; - sequence: AutomationSequenceNode[]; + metadata: AutomationMetadata; + trigger: AutomationTrigger[]; + sequence: AutomationSequenceNode[]; } export type AutomationSequenceNode = AutomationAction | AutomationCondition; export type AutomationNodeMapping = { - "trigger": AutomationTrigger; - "action": AutomationAction; - "condition": AutomationCondition; + "trigger": AutomationTrigger; + "action": AutomationAction; + "condition": AutomationCondition; } export type AutomationNodeTypes = keyof AutomationNodeMapping | undefined; export type AutomationNode =