diff --git a/webapp/.vscode/launch.json b/webapp/.vscode/launch.json index 4a63c3b..9e4ce11 100644 --- a/webapp/.vscode/launch.json +++ b/webapp/.vscode/launch.json @@ -2,24 +2,17 @@ "version": "0.2.0", "configurations": [ { - "name": "Debug CRA Tests", + "name": "Debug Jest Tests", "type": "node", "request": "launch", - "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts", - "args": [ - "test", - "--runInBand", - "--no-cache", - "--watchAll=false" + "runtimeArgs": [ + "--inspect-brk", + "${workspaceRoot}/node_modules/.bin/jest", + "--runInBand" ], - "cwd": "${workspaceRoot}", - "protocol": "inspector", "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", - "env": { - "CI": "true" - }, - "disableOptimisticBPs": true + "port": 9229 } ] } \ No newline at end of file diff --git a/webapp/src/apiService/mock.ts b/webapp/src/apiService/mock.ts index 57c52bb..e0f9077 100644 --- a/webapp/src/apiService/mock.ts +++ b/webapp/src/apiService/mock.ts @@ -15,7 +15,7 @@ export const useMockAPI = ( method = "POST", data = {} }) { - console.debug("[[mockCall]]", method, path, data); + // console.debug("[[mockCall]]", method, path, data); if (path === AUTOMTAION_LIST) { return { ok: true, diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.css b/webapp/src/components/AutomationEditor/AutoInfoBox.css index fa862a5..4869600 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.css +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.css @@ -64,24 +64,6 @@ padding: 1em 0.25em 0.2em 0.25em; } -.automation-editor--info-box--tag--error { - width: 100%; - display: flex; - justify-content: center; - padding: 0; -} - -.automation-editor--info-box--tag--error.hide { - height: 0; - opacity: 0; - display: none; -} - -.automation-editor--info-box--tag--error.show { - height: fit-content; - opacity: 1; -} - .automation-editor--info-box--tag--trash, .automation-editor--info-box--tag--add { height: 2em; width: 2em; diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx index 0123138..de5daec 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx @@ -1,12 +1,20 @@ + import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { makeTagDB } from "components/AutomationList/TagDB"; import { Page } from "components/Page"; import { useState } from "react"; import { AutoInfoBox } from "./AutoInfoBox"; +import { defaultAutomation } from 'utils/defaults'; export default { title: 'App/AutomationList/Editor/InfoBox', component: AutoInfoBox, parameters: { actions: { argTypesRegex: '^on.*' } }, + args: { + tagDB: makeTagDB([ + defaultAutomation("test") + ]) + } } as ComponentMeta; @@ -15,6 +23,7 @@ export const NoTags: ComponentStory = args => { return ( ; onUpdate: (m: AutomationMetadata, tags: Array<[string, string]>) => void; + tagDB: TagDB; } export const AutoInfoBox: FC = ({ metadata, @@ -18,6 +21,7 @@ export const AutoInfoBox: FC = ({ onUpdate, className, children, + tagDB, }) => { // state const [newTag, setNewTag] = useState<[string, string]>(["", ""]) @@ -25,6 +29,7 @@ export const AutoInfoBox: FC = ({ const errorTimeoutId = useRef(-1); // aliases + const foundNewTagNames = tagDB.getTagNames(tags.map(([n, _]) => n)); const setError = (index: number | 'new', msg: string) => { _setError([index, msg]) if (errorTimeoutId.current >= 0) { @@ -68,7 +73,9 @@ export const AutoInfoBox: FC = ({ if (error) { return setError('new', error) } - onUpdate(metadata, tags.concat([newTag])); + onUpdate(metadata, tags.concat([ + newTag.map(x => x.trim()) as [string, string] + ])); setNewTag(['', '']); } const onRemoveTag = (tagIndex: number) => onUpdate(metadata, [ @@ -96,22 +103,40 @@ export const AutoInfoBox: FC = ({

Tags

{tags.map(([tagName, tagValue], tagIndex) =>
- updateTagName(v, tagIndex)} /> - onUpdateTags(v, tagIndex)} /> + updateTagName(v, tagIndex)} + options={foundNewTagNames} + error={errorIndex === tagIndex ? error : undefined} + /> + onUpdateTags(v, tagIndex)} + options={tagDB.getTagValues(tagName)} + error={errorIndex === tagIndex ? error : undefined} + /> - - {error} -
)}

- setNewTag([v, newTag[1]])} onEnter={addNewTag} /> - setNewTag([newTag[0], v])} onEnter={addNewTag} /> + setNewTag([v, newTag[1]])} + options={foundNewTagNames} + error={errorIndex === 'new' ? error : undefined} + /> + setNewTag([newTag[0], v])} + options={tagDB.getTagValues(newTag[0])} + error={errorIndex === 'new' ? error : undefined} + /> - - {error} -
diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index 152aaa9..0a2bdde 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -4,6 +4,7 @@ import { AutomationEditor } from './index'; import { useState } from 'react'; import * as dgconst from 'components/DAGFlow/constants'; import { Page } from "components/Page"; +import { makeTagDB } from "components/AutomationList/TagDB"; export default { @@ -11,7 +12,7 @@ export default { component: AutomationEditor, parameters: { actions: { argTypesRegex: '^on.*' } }, args: { - dims: dgconst.DEFAULT_DIMS + dims: dgconst.DEFAULT_DIMS, } } as ComponentMeta @@ -20,6 +21,7 @@ const Template: ComponentStory = args => { return { window.setTimeout(() => setState(s), 3000) diff --git a/webapp/src/components/AutomationEditor/index.tsx b/webapp/src/components/AutomationEditor/index.tsx index 244d45e..7fa932e 100644 --- a/webapp/src/components/AutomationEditor/index.tsx +++ b/webapp/src/components/AutomationEditor/index.tsx @@ -10,16 +10,19 @@ import { SafeDAGAutomationFlow } from "components/DAGFlow/safe"; import { DAGAutomationFlowDims } from "components/DAGFlow/types"; import { MiniFailure } from 'types/validators/helper'; import InputYaml from "components/Inputs/InputYaml"; +import { TagDB } from "components/AutomationList/TagDB"; interface Props { automation?: AutomationData; dims: DAGAutomationFlowDims; onUpdate: (auto: AutomationData) => void; + tagDB: TagDB; } export const AutomationEditor: FC = ({ dims, automation: propsAutos, onUpdate: propsOnUpdate, + tagDB, }) => { // state const { @@ -55,6 +58,7 @@ export const AutomationEditor: FC = ({ metadata={state.data.metadata} tags={state.data.tags} onUpdate={updateMetadata} + tagDB={tagDB} > b { + color: var(--change); } .automation-list-box--body--group>.automation-list-box--body--group { diff --git a/webapp/src/components/AutomationList/AutomationListBox.test.ts b/webapp/src/components/AutomationList/AutomationListBox.test.ts index fbf196d..c964bd3 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.test.ts +++ b/webapp/src/components/AutomationList/AutomationListBox.test.ts @@ -2,62 +2,64 @@ import { groupAutomations, getTagList } from './AutomationListBox'; import { AutomationData } from 'types/automations/index'; import { createMockAuto } from 'utils/mocks'; +const makeDummy = (id: string, tags: Record) => ({ + condition: [], + metadata: { + id, + alias: '', + description: '', + mode: 'single', + }, + tags, + sequence: [], + trigger: [], +}) + test('group automations', () => { const automations: AutomationData[] = [ - { - condition: [], - metadata: { - id: '1', - alias: '', - description: '', - mode: 'single', - }, - tags: { - "Room": 'Living Room', - "Type": 'Lights' - }, - sequence: [], - trigger: [], - }, - { - condition: [], - metadata: { - id: '2', - alias: '', - description: '', - mode: 'single', - }, - tags: { - "Room": 'Living Room', - "Type": 'Climate' - }, - sequence: [], - trigger: [], - }, - { - condition: [], - metadata: { - id: '3', - alias: '', - description: '', - mode: 'single', - }, - tags: { - "Room": 'Kitchen', - "Type": 'Climate' - }, - sequence: [], - trigger: [], - }, + makeDummy('1', { + "Room": 'Living Room', + "Type": 'Lights' + }), + makeDummy('2', { + "Room": 'Living Room', + "Type": 'Climate' + }), + makeDummy('3', { + "Room": 'Kitchen', + "Type": 'Climate' + }), + makeDummy('4', { + "Type": 'Climate' + }), + makeDummy('5', { + "Room": 'Living Room' + }), + makeDummy('6', {}), ]; + expect(groupAutomations(automations, [], ['Room', "Type"])).toStrictEqual([ + 0, 1, 2, 3, 4, 5, + ]) + expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({ + "Lights": [0], + "Climate": [1, 2, 3], + "": [4, 5] + }) + expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({ + "Living Room": [0, 1, 4], + "Kitchen": [2], + "": [3, 5] + }) expect(groupAutomations(automations, [0, 1], ['Room', "Type"])).toStrictEqual({ "Living Room": { "Lights": [0], "Climate": [1], + "": [4], }, "Kitchen": { "Climate": [2], - } + }, + "": [3, 5] }) expect(groupAutomations(automations, [1, 0], ['Room', "Type"])).toStrictEqual({ "Lights": { @@ -65,22 +67,11 @@ test('group automations', () => { }, "Climate": { "Living Room": [1], - "Kitchen": [2] - } + "Kitchen": [2], + "": [3], + }, + "": [4, 5] }) - expect(groupAutomations(automations, [1], ['Room', "Type"])).toStrictEqual({ - "Lights": [0], - "Climate": [1, 2] - }) - expect(groupAutomations(automations, [0], ['Room', "Type"])).toStrictEqual({ - "Living Room": [0, 1], - "Kitchen": [2] - }) - - - expect(groupAutomations(automations, [], ['Room', "Type"])).toStrictEqual([ - 0, 1, 2 - ]) }); diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index a89cf72..fa5e104 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -5,6 +5,7 @@ import InputMultiSelect from "components/Inputs/InputMultiSelect"; import InputText from "components/Inputs/InputText"; import { Button } from "components/Inputs/Button"; import { TrashIcon } from "components/Icons"; +import { MetadataBox } from "./AutomationMetadataBox"; export type Props = { @@ -78,13 +79,6 @@ const filterAutomations = (autos: AutomationData[], searchText: string, selected return false } } - if (selected.length > 0) { - for (const i of selected) { - if (!a.tags[tags[i]]) { - return false - } - } - } return true }) @@ -105,13 +99,21 @@ export const groupAutomations = ( let lastKey: string | null = null; for (const i of selected) { tag = tags[i]; - if (lastKey !== null) { + if (lastKey) { where = where[lastKey] } - if (!where[a.tags[tag]]) { - where[a.tags[tag]] = {} + if (a.tags[tag]) { + if (!where[a.tags[tag]]) { + where[a.tags[tag]] = {} + } + lastKey = a.tags[tag] + } else { + if (!where[""]) { + where[""] = [] + } + lastKey = "" + break } - lastKey = a.tags[tag] } if (lastKey !== null) { if (!where[lastKey] || !Array.isArray(where[lastKey])) { @@ -148,7 +150,12 @@ const convertGroupsToItems = ( }) } else { - return Object.keys(groups).map(groupName => { + return Object.keys(groups).sort((a, b) => { + if (!a) { + return 1 + } + return a < b ? 1 : -1 + }).map(groupName => { return , -}> = (auto) => { - let title = {"BadAuto<>"} - if (auto.metadata) { - title = <> - {String(auto.metadata.alias ?? "").slice(0, 15)} ({String(auto.metadata.id).slice(0, 5)}) - {String(auto.metadata.description ?? "").slice(0, 25)} - - } - return
- {title} -
- {Object.keys(auto.tags).map(tagName => - {tagName}: {auto.tags[tagName]} - )} -
-
-} -const AutomationListBoxGroup: FC<{ +export const AutomationListBoxGroup: FC<{ groupName: string, groups: any, autos: Array<[AutomationData, number]>, @@ -204,8 +187,13 @@ const AutomationListBoxGroup: FC<{ selected, }) => { const [open, setOpen] = useState(false); - return
- setOpen(!open)}>{groupName} {!open ? "⊕" : "⊖"} + const isOther = !groupName; + const openIcon = !open ? "⊕" : "⊖"; + const total = isOther ? + {groups.length} item{groups.length !== 1 ? 's' : ''} (untagged) + : <> + return
+ setOpen(!open)}>{groupName} {openIcon} {total} {open && convertGroupsToItems(groups, autos, onSelectAutomation, onRemove, selected)}
} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx new file mode 100644 index 0000000..1e7f234 --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx @@ -0,0 +1,52 @@ + +import { TrashIcon } from "components/Icons"; +import { Button } from "components/Inputs/Button"; +import { MetadataBox } from "./AutomationMetadataBox"; +import { AutomationData } from "types/automations"; +import { AutomationGrouper } from "./automationGrouper"; + + + +export const convertGroupsToItems = ( + autos: Array<[AutomationData, number]>, + groups: AutomationGrouper, + currentNode: number, + onSelectAutomation: (i: number) => void, + onRemove: (i: number) => void, +) => { + if (Array.isArray(groups)) { + return groups.map(i => { + const [auto, autoIndex] = autos[i]; + let title = "BadAuto<>" + if (auto.metadata) { + title = `Name = ${auto.metadata.alias}\nID = ${auto.metadata.id}\nDescription = ${auto.metadata.description}` + } + return
onSelectAutomation(autoIndex)} + > + + +
+ }) + } else { + return Object.keys(groups).sort((a, b) => { + if (!a) { + return 1 + } + return a < b ? 1 : -1 + }).map(groupName => { + return + }) + } +} diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx new file mode 100644 index 0000000..9c628b9 --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx @@ -0,0 +1,27 @@ +import { FC } from "react" +import { AutomationMetadata } from "types/automations" + +export const MetadataBox: FC<{ + metadata: AutomationMetadata, + tags: Record, +}> = (auto) => { + let title = {"BadAuto<>"} + if (auto.metadata) { + title = <> + {String(auto.metadata.alias ?? "").slice(0, 15)} ({String(auto.metadata.id).slice(0, 5)}) + {String(auto.metadata.description ?? "").slice(0, 25)} + + } + return
+ {title} +
+ {Object.keys(auto.tags).map(tagName => + {tagName}: {auto.tags[tagName]} + )} +
+
+} diff --git a/webapp/src/components/AutomationList/TagDB.ts b/webapp/src/components/AutomationList/TagDB.ts new file mode 100644 index 0000000..8bb361a --- /dev/null +++ b/webapp/src/components/AutomationList/TagDB.ts @@ -0,0 +1,21 @@ +import { AutomationData } from "types/automations"; + +export type TagDB = ReturnType; +export const makeTagDB = (automations: AutomationData[]) => { + const allTags: Record = {}; + automations.forEach(({ tags }) => + Object.keys(tags).forEach(tagName => { + const tagValue = tags[tagName]; + allTags[tagName] = (allTags[tagName] ?? []).concat([tagValue]); + }) + ); + const allTagNames = Object.keys(allTags); + return { + getTagNames(myTags: string[]): string[] { + return allTagNames.filter(n => !myTags.includes(n)) + }, + getTagValues(tagName: string): string[] { + return allTags[tagName] ?? []; + } + } +} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/automationGrouper.test.ts b/webapp/src/components/AutomationList/automationGrouper.test.ts new file mode 100644 index 0000000..6b75b0b --- /dev/null +++ b/webapp/src/components/AutomationList/automationGrouper.test.ts @@ -0,0 +1,48 @@ +import { makeGrouping } from "./automationGrouper"; +import { AutomationData } from 'types/automations/index'; +import { createMockAuto } from 'utils/mocks'; + +const automations: AutomationData[] = [ + createMockAuto({ + "Room": 'Living Room', + "Type": 'Lights' + }), + createMockAuto({ + "Room": 'Living Room', + "Type": 'Climate' + }), + createMockAuto({ + "Room": 'Kitchen', + "Type": 'Climate' + }), + createMockAuto({ + "Type": 'Climate' + }), + createMockAuto({ + "Room": 'Living Room' + }), + createMockAuto({}), +]; + +test('no grouping', () => { + const grouped = makeGrouping(automations, [], 0); + expect(grouped.top).toStrictEqual([0]) + expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 2, 3, 4, 5]) +}); +test('1 grouping', () => { + const grouped = makeGrouping(automations, ["Room"], 0); + expect(grouped.top).toHaveLength(3); + expect(grouped.getAutomations(0)).toStrictEqual([0, 1, 4]) + expect(grouped.getAutomations(1)).toStrictEqual([2]) + expect(grouped.getAutomations(2)).toStrictEqual([3, 5]) +}); +test('2 groupings', () => { + const grouped = makeGrouping(automations, ["Room", "Type"], 0); + expect(grouped.top).toHaveLength(3); + expect(grouped.getAutomations(grouped.top[0])).toHaveLength(0) + expect(grouped.getSubGroups(grouped.top[0])).toHaveLength(3) + expect(grouped.getAutomations(grouped.top[1])).toHaveLength(0) + expect(grouped.getSubGroups(grouped.top[1])).toHaveLength(1) + expect(grouped.getAutomations(grouped.top[2])).toHaveLength(2) + expect(grouped.getSubGroups(grouped.top[2])).toHaveLength(0) +}); \ No newline at end of file diff --git a/webapp/src/components/AutomationList/automationGrouper.tsx b/webapp/src/components/AutomationList/automationGrouper.tsx new file mode 100644 index 0000000..21b504d --- /dev/null +++ b/webapp/src/components/AutomationList/automationGrouper.tsx @@ -0,0 +1,116 @@ +import { AutomationData } from "types/automations"; + +const makeGrouper = () => { + const groupIdNames: string[] = [] + const groupSubGroups: number[][] = [] + const groupAutomations: number[][] = [] + const groupData: Array<{ + name: string; + selected: boolean; + }> = []; + const topGroups: number[] = []; + + return { + groupIdNames, + groupSubGroups, + groupAutomations, + groupData, + topGroups, + addNew(groupId: string, groupName: string, theChosenOne: boolean, isTopLevel: boolean): number { + groupIdNames.push(groupId); + groupSubGroups.push([]); + groupAutomations.push([]); + groupData.push({ + name: groupName, + selected: theChosenOne, + }) + const groupIdx = groupIdNames.length - 1; + if (isTopLevel) { + topGroups.push(groupIdx); + } + return groupIdx; + }, + updateGroup( + groupIdx: number, + update: { + isChosen: boolean, + lastGroupIdx: number | null, + autoIdx: number | null, + } + ) { + if (update.isChosen) { + groupData[groupIdx].selected = true; + } + if (update.lastGroupIdx !== null) { + groupSubGroups[update.lastGroupIdx].push(groupIdx); + } + if (update.autoIdx !== null) { + groupAutomations[groupIdx].push(update.autoIdx) + } + } + } +} + +export type AutomationGrouper = ReturnType; +export const makeGrouping = ( + autos: AutomationData[], + selectedTags: string[], + selectedAutomationIdx: number, +) => { + const grouper = makeGrouper(); + + if (selectedTags.length === 0) { + grouper.addNew("$", "", selectedAutomationIdx >= 0, true) + grouper.groupAutomations = [autos.map((_, i) => i)]; + } else { + autos.forEach((auto, autoIdx) => { + const theChosenOne = selectedAutomationIdx === autoIdx; + let lastGroupIdx: number | null = null; + for (let ti = 0; ti < selectedTags.length; ti++) { + const isTopLevel = ti === 0; + const selTag = selectedTags[ti]; + if (selTag in auto.tags) { + const groupName = auto.tags[selTag]; + const groupId = `${selTag}:${groupName}`; + let groupIdx = grouper.groupIdNames.indexOf(groupId); + if (groupIdx < 0) { + groupIdx = grouper.addNew(groupId, groupName, theChosenOne, isTopLevel); + } + grouper.updateGroup(groupIdx, { + isChosen: theChosenOne, + lastGroupIdx, + autoIdx: ti === selectedTags.length - 1 ? autoIdx : null, + }) + lastGroupIdx = groupIdx; + } else { + let groupIdx = grouper.groupIdNames.indexOf(`${selTag}:$`); + if (groupIdx < 0) { + groupIdx = grouper.addNew(`${selTag}:$`, "", theChosenOne, isTopLevel); + } + grouper.updateGroup(groupIdx, { + isChosen: theChosenOne, + lastGroupIdx, + autoIdx, + }) + break + } + } + }); + } + + console.log(grouper) + return { + top: grouper.topGroups, + getSubGroups(groupIdx: number) { + return grouper.groupSubGroups[groupIdx] + }, + getAutomations(groupIdx: number) { + return grouper.groupAutomations[groupIdx] + }, + getData(groupIdx: number) { + return grouper.groupData[groupIdx] + } + } +} + + diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx index 61106f4..b3b770d 100644 --- a/webapp/src/components/AutomationList/index.tsx +++ b/webapp/src/components/AutomationList/index.tsx @@ -10,6 +10,7 @@ import { ApiService } from "apiService/core"; import { AutomationListBox } from "./AutomationListBox"; import { DAGAutomationFlowDims } from 'components/DAGFlow/types'; import useWindowSize from "utils/useWindowSize"; +import { makeTagDB } from "./TagDB"; interface AutomationListParams { @@ -34,6 +35,7 @@ export const AutomationList: FC = ({ const { isMobile } = useWindowSize(); // alias const currentAuto = automations.length > 0 ? automations[current] : null; + const tagDB = makeTagDB(automations); // render return
@@ -64,6 +66,7 @@ export const AutomationList: FC = ({ automation={currentAuto} onUpdate={a => onUpdate(current, a)} dims={dims} + tagDB={tagDB} /> : <>}
{children} diff --git a/webapp/src/components/Inputs/InputAutoText.tsx b/webapp/src/components/Inputs/InputAutoText.tsx new file mode 100644 index 0000000..be77b2c --- /dev/null +++ b/webapp/src/components/Inputs/InputAutoText.tsx @@ -0,0 +1,40 @@ +import TextField from "@mui/material/TextField"; +import Autocomplete from "@mui/material/Autocomplete"; + +export interface InputAutoTextProps { + label: string; + value: string; + options?: string[]; + error?: string; + onChange: (v: string) => void; +} +export default function InputAutoText({ + value = "", + label, + options = [], + onChange, + error, +}: InputAutoTextProps) { + + return onChange( + v === null ? "" : v + )} + renderInput={params => { + e.preventDefault(); + onChange( + e.target.value + ) + }} + />} + /> +} diff --git a/webapp/src/components/Inputs/InputTextBubble.stories.tsx b/webapp/src/components/Inputs/InputTextBubble.stories.tsx index 4a82503..a2cace8 100644 --- a/webapp/src/components/Inputs/InputTextBubble.stories.tsx +++ b/webapp/src/components/Inputs/InputTextBubble.stories.tsx @@ -42,3 +42,27 @@ export const EntityId: ComponentStory = args => { } + + +export const ManyOptions: ComponentStory = args => { + + const [value, setValue] = useState('hello') + return + + +} + +ManyOptions.args = { + ...ManyOptions.args, + options: [ + 'sensor.humidity_bathroom', + 'sensor.humidity_bedroom', + 'sensor.humidity_laundry', + 'sensor.temp_bathroom', + 'sensor.temp_bedroom', + 'switch.living_room', + 'switch.bathroom', + 'light.bedroom_main', + 'light.bedroom_second', + ] +} \ No newline at end of file diff --git a/webapp/src/components/Inputs/InputTextBubble.tsx b/webapp/src/components/Inputs/InputTextBubble.tsx index 3b4b141..d91a1d6 100644 --- a/webapp/src/components/Inputs/InputTextBubble.tsx +++ b/webapp/src/components/Inputs/InputTextBubble.tsx @@ -1,55 +1,46 @@ -import { FC, useState } from "react"; -import InputText from "./InputText"; -import { useInputBubblesStyles } from "./styles"; +import Chip from "@mui/material/Chip"; +import TextField from "@mui/material/TextField"; +import Autocomplete from "@mui/material/Autocomplete"; +import { FC } from "react"; export interface Props { label: string; - textBoxFor?: string; value: string | string[]; + options?: string[]; onChange: (v: string[]) => void; - additionalTooltipFilters?: Record; } export default function InputTextBubble({ value = "", label, - textBoxFor, + options = [], onChange, - additionalTooltipFilters = {}, }: Props) { - const { classes } = useInputBubblesStyles({}); - let selected: string[]; - if (typeof value === 'string') { - selected = [value] - } else { - selected = value - } - const [text, setText] = useState(''); - const addNew = () => { - if (text) { - onChange([text].concat(selected)) - setText("") - } - }; - return setText(t)} - onEnter={() => addNew()} - additionalTooltipFilters={additionalTooltipFilters} - > -
- {selected.map((s, i) =>
onChange([ - ...selected.slice(0, i), - ...selected.slice(i + 1), - ])}> - {s} - X -
)} -
- -
+ const selected = Array.isArray(value) ? value : [value]; + + return onChange( + v === null ? [] : + Array.isArray(v) ? v : [v] + )} + renderInput={params => } + renderTags={(tagValue, getTagProps) => + tagValue.map((option, index) => ( + + )) + } + /> } export const InputEntity: FC<{ @@ -62,5 +53,4 @@ export const InputEntity: FC<{ value={value} onChange={onChange} label="Entity ID" - textBoxFor="entity_id" /> diff --git a/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx b/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx index 0285a3f..37a3b86 100644 --- a/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/TriggerDevice.tsx @@ -1,6 +1,7 @@ import { OptionManager } from './OptionManager'; import { AutomationTriggerDevice } from 'types/automations/triggers'; import InputText from 'components/Inputs/InputText'; +import { InputEntity } from 'components/Inputs/InputTextBubble'; export const TriggerDevice: OptionManager = { @@ -34,7 +35,7 @@ export const TriggerDevice: OptionManager = { ...state, device_id })} /> - setState({ + setState({ ...state, entity_id })} /> diff --git a/webapp/src/components/VersionBox/index.tsx b/webapp/src/components/VersionBox/index.tsx index 22b9950..509fb93 100644 --- a/webapp/src/components/VersionBox/index.tsx +++ b/webapp/src/components/VersionBox/index.tsx @@ -1,7 +1,6 @@ import "./index.css"; import { FC } from "react"; -console.log(process.env) export const VersionBox: FC = () => { const version = process.env.REACT_APP_BUILD_VERSION ?? "n/a"; return
diff --git a/webapp/src/types/validators/triggers.ts b/webapp/src/types/validators/triggers.ts index 631a847..1f8198e 100644 --- a/webapp/src/types/validators/triggers.ts +++ b/webapp/src/types/validators/triggers.ts @@ -1,5 +1,5 @@ import * as st from 'superstruct' -import { AutomationTimeString, AutomationTime } from './common' +import { AutomationTimeString, AutomationTime, EntityId } from './common' const AutomationTriggerBase = { @@ -95,7 +95,7 @@ export const AutomationTriggerZone = st.type({ export const AutomationTriggerDevice = st.type({ ...AutomationTriggerBase, platform: st.literal('device'), - entity_id: st.optional(st.string()), + entity_id: st.optional(EntityId), device_id: st.string(), domain: st.string(), type: st.string(),