From a8229eb7bdc9a1cc258f3a215531e36d2e2b615b Mon Sep 17 00:00:00 2001 From: Ari Date: Fri, 1 Jul 2022 14:28:18 -0400 Subject: [PATCH] - removed multi node - updated tag updater --- .../AutomationList/AutomationListBox.css | 6 + .../AutomationListBox.stories.tsx | 79 ++++--- .../AutomationList/AutomationListBox.tsx | 4 + .../AutomationList/AutomationListBoxGroup.tsx | 27 ++- .../AutomationList/AutomationMetadataBox.css | 47 ++++ .../AutomationList/AutomationMetadataBox.tsx | 121 +++++++++-- .../AutomationMetadataBoxTag.stories.tsx | 31 +++ .../src/components/AutomationList/index.tsx | 1 + webapp/src/components/DAGFlow/helpers.tsx | 1 - webapp/src/components/DAGFlow/index.tsx | 40 ++-- webapp/src/components/DAGFlow/types.ts | 25 +-- webapp/src/components/DAGFlow/updater.test.ts | 99 ++++----- webapp/src/components/DAGFlow/updater.ts | 3 - .../components/Inputs/InputAutoComplete.tsx | 13 ++ .../src/components/Inputs/InputAutoText.tsx | 7 + .../src/components/MultiNodeEditors/index.css | 48 ----- .../MultiNodeEditors/index.stories.tsx | 70 ------ .../src/components/MultiNodeEditors/index.tsx | 55 ----- .../src/components/MultiNodeEditors/state.ts | 202 ------------------ .../src/components/MultiNodeEditors/types.ts | 8 - 20 files changed, 337 insertions(+), 550 deletions(-) create mode 100644 webapp/src/components/AutomationList/AutomationMetadataBox.css create mode 100644 webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx delete mode 100644 webapp/src/components/MultiNodeEditors/index.css delete mode 100644 webapp/src/components/MultiNodeEditors/index.stories.tsx delete mode 100644 webapp/src/components/MultiNodeEditors/index.tsx delete mode 100644 webapp/src/components/MultiNodeEditors/state.ts delete mode 100644 webapp/src/components/MultiNodeEditors/types.ts diff --git a/webapp/src/components/AutomationList/AutomationListBox.css b/webapp/src/components/AutomationList/AutomationListBox.css index fda4424..10e07d4 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.css +++ b/webapp/src/components/AutomationList/AutomationListBox.css @@ -64,6 +64,12 @@ border-color: var(--primary); } +.automation-list-box--body--item--tags--add:hover { + border-color: var(--primary); + color: var(--primary-accent); + background-color: var(--secondary); +} + .automation-list-box--body--item { padding-left: 1em; cursor: pointer; diff --git a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx index 366ddc0..7573dbd 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.stories.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.stories.tsx @@ -6,45 +6,66 @@ import { Page } from "components/Page"; import { CookiesProvider } from "react-cookie"; export default { - title: 'App/AutomationList/Box', + title: "App/AutomationList/Box", component: AutomationListBox, - parameters: { actions: { argTypesRegex: '^on.*' } }, - args: { - } + parameters: { actions: { argTypesRegex: "^on.*" } }, + args: {}, } as ComponentMeta; +const Template: ComponentStory = (args) => { + const [selected, setSelected] = useState(args.selected); + return ( + + + { + setSelected(i); + args.onSelectAutomation(i); + }} + /> + + + ); +}; -const Template: ComponentStory = args => { - const [selected, setSelected] = useState(args.selected) - return - - { - setSelected(i) - args.onSelectAutomation(i) - }} /> - - -} - -export const EmptyStart = Template.bind({}) +export const EmptyStart = Template.bind({}); EmptyStart.args = { ...EmptyStart.args, automations: [], -} +}; -export const FewAutos = Template.bind({}) +export const FewAutos = Template.bind({}); FewAutos.args = { ...FewAutos.args, automations: [ - createMockAuto({ "Room": "Bathroom", "Type": "Lights", "NewDevice": "Yes", "Remote": "No", "Floor": "1" }), - 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({ + Room: "Bathroom", + Type: "Lights", + NewDevice: "Yes", + Remote: "No", + Floor: "1", + }), + 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(), ], -} +}; + +export const OneAuto = Template.bind({}); +OneAuto.args = { + ...OneAuto.args, + automations: [ + createMockAuto({ + Type: "Climate", + }), + ], +}; diff --git a/webapp/src/components/AutomationList/AutomationListBox.tsx b/webapp/src/components/AutomationList/AutomationListBox.tsx index d90325a..d24a6e0 100644 --- a/webapp/src/components/AutomationList/AutomationListBox.tsx +++ b/webapp/src/components/AutomationList/AutomationListBox.tsx @@ -7,6 +7,7 @@ import { Button } from "components/Inputs/Button"; import { makeGrouping } from "./automationGrouper"; import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; import { useCookies } from "react-cookie"; +import { TagDB } from "components/AutomationList/TagDB"; export type Props = { automations: AutomationData[]; @@ -14,6 +15,7 @@ export type Props = { onSelectAutomation: (i: number) => void; onRemove: (i: number) => void; onAdd: () => void; + tagsDB: TagDB; }; const useAutomationListBoxState = () => { @@ -56,6 +58,7 @@ export const AutomationListBox: FC = ({ onSelectAutomation, onAdd, onRemove, + tagsDB, }) => { const tags = getTagList(automations); const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } = @@ -91,6 +94,7 @@ export const AutomationListBox: FC = ({ autos={filteredAutomations} grouping={grouping} selectedAutomationIdx={selected} + tagsDB={tagsDB} />
diff --git a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx index 976e1a1..28a1458 100644 --- a/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx +++ b/webapp/src/components/AutomationList/AutomationListBoxGroup.tsx @@ -4,6 +4,7 @@ import { AutomationData } from "types/automations"; import { AutomationGrouping } from "./automationGrouper"; import { FC, useState } from "react"; import { ButtonIcon } from "components/Icons/ButtonIcons"; +import { TagDB } from "./TagDB"; export type AutomationListBoxEvents = { onSelectAutomation: (i: number) => void; @@ -24,12 +25,14 @@ export type AutomationListBoxGroupProps = { grouping: AutomationGrouping; autos: Array<[AutomationData, number]>; selectedAutomationIdx: number; + tagsDB: TagDB; }; export const AutomationListBoxGroup: FC = ({ events, grouping, autos, selectedAutomationIdx, + tagsDB, }) => { if (grouping.top.length === 1) { return ( @@ -41,7 +44,8 @@ export const AutomationListBoxGroup: FC = ({ currentNode: grouping.top[0], }, events, - selectedAutomationIdx + selectedAutomationIdx, + tagsDB )} ); @@ -58,6 +62,7 @@ export const AutomationListBoxGroup: FC = ({ }} events={events} selectedAutomationIdx={selectedAutomationIdx} + tagsDB={tagsDB} /> ))} @@ -69,7 +74,8 @@ export const convertGroupsToItems = ( autos: Array<[AutomationData, number]>, { grouping, currentNode }: AutomationListBoxGroups, events: AutomationListBoxEvents, - selectedAutomationIdx: number + selectedAutomationIdx: number, + tagsDB: TagDB ) => { const automations = grouping.getAutomations(currentNode); if (automations.length > 0) { @@ -79,6 +85,7 @@ export const convertGroupsToItems = ( events={events} auto={autos[autoIndex]} isSelected={selectedAutomationIdx === autos[autoIndex][1]} + tagsDB={tagsDB} /> )); } @@ -94,6 +101,7 @@ export const convertGroupsToItems = ( }} events={events} selectedAutomationIdx={selectedAutomationIdx} + tagsDB={tagsDB} /> )); } @@ -105,7 +113,8 @@ export const AutomationListBoxGroupItem: FC<{ groups: AutomationListBoxGroups; autos: Array<[AutomationData, number]>; selectedAutomationIdx: number; -}> = ({ autos, groups, events, selectedAutomationIdx }) => { + tagsDB: TagDB; +}> = ({ autos, groups, events, selectedAutomationIdx, tagsDB }) => { const groupData = groups.grouping.getData(groups.currentNode); const automations = groups.grouping.getAutomations(groups.currentNode); const [open, setOpen] = useState(false); @@ -131,7 +140,13 @@ export const AutomationListBoxGroupItem: FC<{ {groupData.name} {openIcon} {total} {open && - convertGroupsToItems(autos, groups, events, selectedAutomationIdx)} + convertGroupsToItems( + autos, + groups, + events, + selectedAutomationIdx, + tagsDB + )}
); }; @@ -140,10 +155,12 @@ export const AutomationListBoxItem: FC<{ auto: [AutomationData, number]; isSelected: boolean; events: AutomationListBoxEvents; + tagsDB: TagDB; }> = ({ events: { onRemove, onSelectAutomation }, auto: [auto, autoIndex], isSelected, + tagsDB, }) => { let title = "BadAuto<>"; if (auto.metadata) { @@ -159,7 +176,7 @@ export const AutomationListBoxItem: FC<{ title={title} onClick={() => onSelectAutomation(autoIndex)} > - + onRemove(autoIndex)} icon={} /> ); diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.css b/webapp/src/components/AutomationList/AutomationMetadataBox.css new file mode 100644 index 0000000..eccc50e --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationMetadataBox.css @@ -0,0 +1,47 @@ +.automation-list-box--body--item--tags--item { + color: white; + border-color: white; + border-style: solid; + border-width: 1px; + border-radius: 0.5em; + padding: 0.1em 0.5em; + + display: flex; + flex-direction: row; + gap: 0.25em; + max-width: min-content; + align-items: center; + +} + +.automation-list-box--body--item--tags--item>.text:hover { + text-decoration: underline; + cursor: pointer; +} + +.automation-list-box--body--item--tags--item>.text { + position: relative; +} + +.automation-list-box--body--item--tags--item>.text .MuiFilledInput-root { + padding: 0; + min-width: 6em; + max-width: 10em; + background-color: var(--primary); +} + +.automation-list-box--body--item--tags--item>.text .icon { + position: absolute; + right: 0.5em; + top: 0.5em; + cursor: pointer; +} + +.automation-list-box--body--item--tags--item>.text .icon:hover { + background-color: var(--primary-accent); + border-radius: 20px; +} + +.automation-list-box--body--item--tags--item>.text .icon:hover svg { + fill: var(--primary); +} \ No newline at end of file diff --git a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx index 9c628b9..fe6e6f0 100644 --- a/webapp/src/components/AutomationList/AutomationMetadataBox.tsx +++ b/webapp/src/components/AutomationList/AutomationMetadataBox.tsx @@ -1,27 +1,102 @@ -import { FC } from "react" -import { AutomationMetadata } from "types/automations" +import "./AutomationMetadataBox.css"; +import { CheckMarkIcon } from "components/Icons"; +import { FC, useState } from "react"; +import { AutomationMetadata } from "types/automations"; +import { InputAutoComplete } from "components/Inputs/InputAutoComplete"; +import { TagDB } from "./TagDB"; +import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager"; export const MetadataBox: FC<{ - metadata: AutomationMetadata, - tags: Record, + metadata: AutomationMetadata; + tags: Record; + tagsDB: TagDB; }> = (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 [tags, setTags] = useState(auto.tags); + const tagOptions = auto.tagsDB.getTagNames(Object.keys(tags)); + 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(tags).map((tagName) => ( + + setTags( + cleanUpUndefined({ + ...tags, + [tagName]: undefined, + [newName]: newValue, + }) + ) + } + /> + ))} + + +
-} + ); +}; + +export const MetadataBoxTag: FC<{ + value: [string, string]; + onChange: (t: [string, string]) => void; + options: [string[], string[]]; +}> = (props) => { + const [[editMode1, editMode2], setEditMode] = useState([false, false]); + const toggle1 = () => setEditMode([!editMode1, editMode2]); + const toggle2 = () => setEditMode([editMode1, !editMode2]); + return ( + + {editMode1 ? ( + props.onChange([v ?? "", props.value[1]])} + onlyShowLabel + endAdornment={} + /> + ) : ( + + {props.value[0]}: + + )} + {editMode2 ? ( + props.onChange([props.value[0], v ?? ""])} + onlyShowLabel + endAdornment={} + /> + ) : ( + + {props.value[1]} + + )} + + ); +}; diff --git a/webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx b/webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx new file mode 100644 index 0000000..a07b25b --- /dev/null +++ b/webapp/src/components/AutomationList/AutomationMetadataBoxTag.stories.tsx @@ -0,0 +1,31 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { DEFAULT_DIMS } from "components/DAGFlow/constants"; +import { Page } from "components/Page"; +import { MetadataBoxTag } from "./AutomationMetadataBox"; +import { useState } from "react"; + +export default { + title: "App/AutomationList/Box", + component: MetadataBoxTag, + parameters: { actions: { argTypesRegex: "^on.*" } }, + args: { + value: ["Type", "Climate"], + options: [[], []], + }, +} as ComponentMeta; + +export const BoxTag: ComponentStory = (args) => { + const [state, setState] = useState(args.value); + return ( + + { + setState(v); + args.onChange(v); + }} + /> + + ); +}; diff --git a/webapp/src/components/AutomationList/index.tsx b/webapp/src/components/AutomationList/index.tsx index f166b72..2bd101f 100644 --- a/webapp/src/components/AutomationList/index.tsx +++ b/webapp/src/components/AutomationList/index.tsx @@ -93,6 +93,7 @@ export const AutomationList: FC = ({ }} selected={current} onRemove={onRemove} + tagsDB={tagDB} /> setHideList(!hideList)} diff --git a/webapp/src/components/DAGFlow/helpers.tsx b/webapp/src/components/DAGFlow/helpers.tsx index 25e32cf..59d78fa 100644 --- a/webapp/src/components/DAGFlow/helpers.tsx +++ b/webapp/src/components/DAGFlow/helpers.tsx @@ -36,7 +36,6 @@ export const makeOnEditAutomationConditions = ) => () => openModal({ - single: true, node: conditions.length > 1 ? { diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index 6923854..8f0e4c1 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -15,7 +15,6 @@ import { DAGCircle } from "./DAGCircle"; import { sequenceToFlow } from "./sequenceConvertor"; import { makeSequenceUpdater } from "./updater"; import { NodeEditor } from "components/NodeEditor"; -import { MultiNodeEditor } from "components/MultiNodeEditors"; import { Modal } from "components/Modal"; import { AutomationCondition } from "types/automations/conditions"; import { convertToFlowNode, makeOnEditAutomationConditions } from "./helpers"; @@ -56,30 +55,19 @@ export const DAGAutomationFlow: FC = ({ // modal let modalBody = <>; if (modalState) { - if (modalState.single) { - modalBody = ( - setModalState(null)} - onSave={(n) => { - modalState.update(n as any); - setModalState(null); - }} - allowedTypes={modalState.allowedTypes} - saveBtnCreateText={modalState.saveBtnCreateText} - /> - ); - } else { - modalBody = ( - setModalState(null)} - onSave={(n) => modalState.update(n as any)} - allowedTypes={["condition"]} - /> - ); - } + modalBody = ( + setModalState(null)} + onSave={(n) => { + modalState.update(n as any); + setModalState(null); + }} + allowedTypes={modalState.allowedTypes} + saveBtnCreateText={modalState.saveBtnCreateText} + /> + ); } // convert automation to flow data @@ -119,7 +107,6 @@ export const DAGAutomationFlow: FC = ({ namer, onAdd: () => setModalState({ - single: true, saveBtnCreateText: true, node: { platform: "device", @@ -133,7 +120,6 @@ export const DAGAutomationFlow: FC = ({ onTriggerUpdate([...trigger.slice(0, i), ...trigger.slice(i + 1)]), onEdit: (i) => setModalState({ - single: true, node: trigger[i], update: (t) => onTriggerUpdate([...trigger.slice(0, i), t, ...trigger.slice(i + 1)]), diff --git a/webapp/src/components/DAGFlow/types.ts b/webapp/src/components/DAGFlow/types.ts index 25214d0..45f0151 100644 --- a/webapp/src/components/DAGFlow/types.ts +++ b/webapp/src/components/DAGFlow/types.ts @@ -1,9 +1,8 @@ import { Edge, Node, XYPosition } from "react-flow-renderer"; -import { AutomationSequenceNode } from "types/automations"; +import { AutomationNodeTypes, AutomationSequenceNode } from "types/automations"; import { DAGCircleProps } from "./DAGCircle"; import { DAGNodeProps } from "./DAGNode"; import { AutomationTrigger } from "types/automations/triggers"; -import { MultiNodeEditorProps } from "components/MultiNodeEditors/types"; import { DAGErrorNodeProps } from "./DAGErrorNode"; import { Namer } from "utils/formatting"; @@ -40,18 +39,10 @@ export type UpdateModalState< export type ModalState< T extends AutomationSequenceNode | AutomationTrigger = any -> = - | { - single: true; - isError?: boolean; - node: T; - update: (n: T) => void; - saveBtnCreateText?: boolean; - allowedTypes: MultiNodeEditorProps["allowedTypes"]; - } - | { - single: false; - node: AutomationSequenceNode[]; - update: (n: AutomationSequenceNode[]) => void; - allowedTypes: ["condition"]; - }; +> = { + isError?: boolean; + node: T; + update: (n: T) => void; + saveBtnCreateText?: boolean; + allowedTypes: AutomationNodeTypes[]; +}; diff --git a/webapp/src/components/DAGFlow/updater.test.ts b/webapp/src/components/DAGFlow/updater.test.ts index 99d6903..8222ff1 100644 --- a/webapp/src/components/DAGFlow/updater.test.ts +++ b/webapp/src/components/DAGFlow/updater.test.ts @@ -1,70 +1,45 @@ -import { AutomationSequenceNode } from "types/automations" +import { AutomationSequenceNode } from "types/automations"; import { ChooseAction } from "types/automations/actions"; import { ModalState } from "./types"; -import { makeSequenceUpdater } from "./updater" +import { makeSequenceUpdater } from "./updater"; const mockSequenceUpdater = (defaultState: AutomationSequenceNode[] = []) => { - let state: AutomationSequenceNode[] = [...defaultState] - const modalStates: ModalState[] = []; + let state: AutomationSequenceNode[] = [...defaultState]; + const modalStates: ModalState[] = []; - const updater = makeSequenceUpdater( - state, - s => { - state = s - }, - ms => modalStates.push(ms) - ); - return { - get state() { - return state - }, - modalStates, - updater - }; -} + const updater = makeSequenceUpdater( + state, + (s) => { + state = s; + }, + (ms) => modalStates.push(ms) + ); + return { + get state() { + return state; + }, + modalStates, + updater, + }; +}; test("sequence updater updates nodes", () => { - const mock = mockSequenceUpdater([ + const mock = mockSequenceUpdater([ + { + choose: [ { - "choose": [ - { - "conditions": [], - "sequence": [], - } - ], - "default": [], - } - ]); - expect(mock.state.length).toBe(1) - expect((mock.state[0] as ChooseAction).alias).toBe(undefined) - mock.updater.updateNode(0, { - ...mock.state[0], - alias: "Hello" - } as any) - expect((mock.state[0] as any).alias).toBe("Hello") -}) -test('update conditions for node', () => { - const mock = mockSequenceUpdater([ - { - "choose": [ - { - "conditions": [], - "sequence": [], - } - ], - "default": [], - } - ]); - mock.updater.makeOnEditConditionsForChooseNode(0, 0)(); - - expect(mock.modalStates.length).toBe(1) - expect((mock.modalStates[0].node as AutomationSequenceNode[]).length).toBe(0) - mock.modalStates[0].update([ - { - "condition": "and", - "conditions": [], - } - ] as any) - expect((mock.state[0] as ChooseAction).choose[0].conditions.length).toBe(1) - expect((mock.state[0] as ChooseAction).choose[0].conditions[0].condition).toBe("and") -}) \ No newline at end of file + conditions: [], + sequence: [], + }, + ], + default: [], + }, + ]); + expect(mock.state.length).toBe(1); + expect((mock.state[0] as ChooseAction).alias).toBe(undefined); + mock.updater.updateNode(0, { + ...mock.state[0], + alias: "Hello", + } as any); + expect((mock.state[0] as any).alias).toBe("Hello"); +}); diff --git a/webapp/src/components/DAGFlow/updater.ts b/webapp/src/components/DAGFlow/updater.ts index ea98313..64e8da5 100644 --- a/webapp/src/components/DAGFlow/updater.ts +++ b/webapp/src/components/DAGFlow/updater.ts @@ -21,7 +21,6 @@ export const makeSequenceUpdater = ( }, addNode() { return openModal({ - single: true, saveBtnCreateText: true, node: { device_id: "", @@ -85,7 +84,6 @@ export const makeSequenceUpdater = ( makeOnModalOpenForNode(i: number, node: AutomationSequenceNode) { return () => openModal({ - single: true, node, allowedTypes: ["action", "condition"], update: (n) => this.updateNode(i, n), @@ -114,7 +112,6 @@ export const makeSequenceUpdater = ( const node = sequence[i]; return () => openModal({ - single: true, isError: true, node, allowedTypes: [getNodeType(node)], diff --git a/webapp/src/components/Inputs/InputAutoComplete.tsx b/webapp/src/components/Inputs/InputAutoComplete.tsx index cb24590..af28e1c 100644 --- a/webapp/src/components/Inputs/InputAutoComplete.tsx +++ b/webapp/src/components/Inputs/InputAutoComplete.tsx @@ -28,6 +28,8 @@ export type InputAutoCompletePropsBase = { onlyShowLabel?: boolean; helperText?: ReactNode; endAdornment?: ReactNode; + onEnter?: () => void; + disableChips?: boolean; } & ( | { value: string | null; @@ -203,12 +205,20 @@ export function InputAutoComplete( ...params.InputProps, endAdornment: props.endAdornment ?? params.InputProps.endAdornment, }} + onKeyDown={(e) => { + if (e.key === "Enter" && props.onEnter) { + props.onEnter(); + } + }} variant="filled" required={props.required} label={props.label ?? "Entity ID"} helperText={helperText} disabled={props.disabled} error={error} + placeholder={ + props.disableChips && !props.multiple ? props.value ?? "" : "" + } /> )} renderTags={(tagValue, getTagProps) => { @@ -241,6 +251,9 @@ export function InputAutoComplete( })}
); + if (props.disableChips) { + return <>; + } if (props.multiple) { return ( diff --git a/webapp/src/components/Inputs/InputAutoText.tsx b/webapp/src/components/Inputs/InputAutoText.tsx index ece099e..eaf445c 100644 --- a/webapp/src/components/Inputs/InputAutoText.tsx +++ b/webapp/src/components/Inputs/InputAutoText.tsx @@ -11,12 +11,14 @@ export interface InputAutoTextProps { required?: boolean; endAdornment?: ReactNode; onChange: (v: string) => void; + onEnter?: () => void; } export default function InputAutoText({ value = "", label, options = [], onChange, + onEnter = () => {}, className, required, endAdornment, @@ -41,6 +43,11 @@ export default function InputAutoText({ InputProps={{ endAdornment, }} + onKeyDown={(e) => { + if (e.key === "Enter") { + onEnter(); + } + }} onChange={(e) => { e.preventDefault(); onChange(e.target.value); diff --git a/webapp/src/components/MultiNodeEditors/index.css b/webapp/src/components/MultiNodeEditors/index.css deleted file mode 100644 index 3999992..0000000 --- a/webapp/src/components/MultiNodeEditors/index.css +++ /dev/null @@ -1,48 +0,0 @@ -.multinode-editor { - max-width: 80vw; - min-width: 60vw; -} - -.multinode-editor.mobile { - max-width: 95vw; -} - -.multinode-editor .node-editor--root { - padding: 0; -} - -.multinode-editor .node-editor--body { - padding-bottom: 1em; - padding-top: 0.25em; - padding-left: 0.25em; - padding-right: 0.25em; - max-height: 60vh; -} - -.multinode-editor--navbar { - display: flex; - flex-direction: row; - justify-content: space-evenly; - align-items: center; - min-width: 20vw; -} - -.multinode-editor.modded>.multinode-editor--navbar * { - border-color: var(--green-light); -} - -.multinode-editor.modded .multinode-editor--total { - color: var(--green-light) -} - -.multinode-editor.modded .node-editor--footer--save { - border-color: var(--green-opaque); - background-color: var(--green-light); - color: var(--primary); -} - -.multinode-editor.modded .node-editor--footer--save:hover { - border-color: var(--green-light); - background-color: var(--green-opaque); - color: var(--green-light); -} \ No newline at end of file diff --git a/webapp/src/components/MultiNodeEditors/index.stories.tsx b/webapp/src/components/MultiNodeEditors/index.stories.tsx deleted file mode 100644 index df38aa1..0000000 --- a/webapp/src/components/MultiNodeEditors/index.stories.tsx +++ /dev/null @@ -1,70 +0,0 @@ -import React, { useState } from "react"; -import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { MultiNodeEditor } from "."; -import { Button } from "components/Inputs/Button"; -import { Modal } from "components/Modal"; -import { Page } from "components/Page"; - - -export default { - title: 'MultiNodeEditor', - component: MultiNodeEditor, - parameters: { actions: { argTypesRegex: '^on.*' } }, - args: { - allowedTypes: ['action', 'condition', 'trigger'], - sequence: [ - { - "condition": "numeric_state", - "entity_id": [ - "sensor.kitchen_humidity" - ], - "conditions": [], - "above": "40" - }, - { - "condition": "template", - "value_template": "states('switch.kitchen') == 'on'" - } - ] - } -} as ComponentMeta - -export const Simple: ComponentStory = args => { - const [state, setState] = useState(args.sequence); - - return -
- { - args.onSave && args.onSave(s) - setState(s) - }} /> -
-
-} -export const InAModal: ComponentStory = props => { - - const [open, setOpen] = useState(false); - const [state, setState] = useState(props.sequence); - return - - - setOpen(!open)} - onSave={s => { - props.onSave && props.onSave(s) - setState(s) - }} - /> - - -}; - - -export const EmptyConditionEditor = Simple.bind({}) -EmptyConditionEditor.args = { - ...EmptyConditionEditor.args, - allowedTypes: ['condition'], - sequence: [], -} \ No newline at end of file diff --git a/webapp/src/components/MultiNodeEditors/index.tsx b/webapp/src/components/MultiNodeEditors/index.tsx deleted file mode 100644 index 11ab693..0000000 --- a/webapp/src/components/MultiNodeEditors/index.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import "./index.css"; -import { FC } from 'react'; -import { NodeEditor } from 'components/NodeEditor'; -import { Button } from 'components/Inputs/Button'; -import { useMultiNodeEditorState } from "./state"; -import { MultiNodeEditorProps } from "./types"; -import useWindowSize from 'utils/useWindowSize'; - - -export const MultiNodeEditor: FC = props => { - // state - const state = useMultiNodeEditorState(props); - const { isMobile } = useWindowSize(); - // components - const nextOrAddBtn = ; - // branch - if (state.isEmpty) { - return
-
- {nextOrAddBtn} -
-
- - -
-
- {state.currentSlideNumber}/{state.totalSlides} -
-
- } - return
-
- - - {nextOrAddBtn} -
- -
- {state.currentSlideNumber + 1}/{state.totalSlides} -
-
-} diff --git a/webapp/src/components/MultiNodeEditors/state.ts b/webapp/src/components/MultiNodeEditors/state.ts deleted file mode 100644 index 872c047..0000000 --- a/webapp/src/components/MultiNodeEditors/state.ts +++ /dev/null @@ -1,202 +0,0 @@ -import { MultiNodeEditorProps } from "./types"; -import { useState, useRef } from "react"; -import { AutomationNode } from "../../types/automations/index"; -import { makeDefault } from "utils/automations"; -import { useConfirm, ConfirmOptions } from "material-ui-confirm"; -import { useSnackbar } from "notistack"; - -export type MultiNodeEditorInternalState = { - slides: AutomationNode[]; - slide: number; - multiNode: { - isModified: boolean; - isSaved: boolean; - }; - internalNodeModified: boolean; -}; - -export const useMultiNodeEditorState = (props: MultiNodeEditorProps) => { - // state - const confirm = useConfirm(); - const snackbr = useSnackbar(); - const [state, _setState] = useState( - makeInitialState({ - slides: props.sequence, - }) - ); - const justSaved = useRef(false); - - // alias - const { slide, slides, multiNode, internalNodeModified } = state; - const setState = (a: MultiNodeEditorInternalState) => { - _setState(a); - }; - const askThenDo = async ( - options: ConfirmOptions, - action: () => void, - condition = () => internalNodeModified && !multiNode.isSaved - ) => { - if (condition()) { - return await confirm(options) - .then(action) - .catch(() => { - snackbr.enqueueSnackbar("Cancelling...", { - variant: "info", - }); - }); - } else { - return action(); - } - }; - - return { - get totalSlides() { - return slides.length; - }, - get currentSlideNumber() { - return slide; - }, - get currentSlide() { - return slides[slide]; - }, - get isLast() { - return slide >= slides.length - 1; - }, - get isEmpty() { - return slides.length === 0; - }, - get isModified() { - return ( - (internalNodeModified || multiNode.isModified) && !multiNode.isSaved - ); - }, - moveBack() { - if (slide <= 0) { - return; - } - return askThenDo( - { - description: - "Moving back without saving will cause you to lose the work, are you sure you want to do that?", - }, - () => - setState({ - ...state, - slide: slide - 1, - internalNodeModified: false, - }) - ); - }, - moveForward() { - const createNew = slide >= slides.length - 1; - const action = createNew ? "Creating a new new node" : "Moving forward"; - return askThenDo( - { - description: `${action} without saving will cause you to lose the work, are you sure you want to do that?`, - }, - () => { - if (createNew) { - setState({ - ...state, - slide: slides.length, - slides: slides.concat([makeDefault(props.allowedTypes[0])]), - internalNodeModified: false, - multiNode: { - isModified: true, - isSaved: false, - }, - }); - } else { - setState({ - ...state, - slide: slide + 1, - internalNodeModified: false, - }); - } - } - ); - }, - updateInternalNode(isReady: boolean, isModified: boolean) { - if (!isReady || isModified) { - if (!state.multiNode.isModified || state.multiNode.isSaved) { - if (justSaved.current) { - justSaved.current = false; - } else { - setState({ - ...state, - multiNode: { isModified: true, isSaved: false }, - internalNodeModified: true, - }); - } - } - } - }, - onRemove() { - setState({ - ...state, - slide: Math.max(slide - 1, 0), - slides: slides.slice(0, slide).concat(slides.slice(slide + 1)), - multiNode: { - isModified: true, - isSaved: false, - }, - }); - }, - onSave(node: AutomationNode) { - justSaved.current = true; - setState({ - ...state, - slides: slides - .slice(0, slide) - .concat([node]) - .concat(slides.slice(slide + 1)), - multiNode: { - isModified: multiNode.isModified, - isSaved: true, - }, - internalNodeModified: false, - }); - }, - onSaveEmpty() { - justSaved.current = true; - setState({ - ...state, - multiNode: { - isModified: multiNode.isModified, - isSaved: true, - }, - internalNodeModified: false, - }); - }, - async onClose() { - if (!multiNode.isSaved && multiNode.isModified) { - try { - await confirm({ - description: - "You haven't saved your work, are you ok with losing it?", - }); - props.onSave && props.onSave(slides); - } catch (_: any) { - return; - } - } - if (justSaved.current) { - props.onSave && props.onSave(slides); - } - props.onClose && props.onClose(); - }, - }; -}; - -const makeInitialState = ( - props: Partial -): MultiNodeEditorInternalState => ({ - slides: [], - slide: 0, - multiNode: { - isModified: false, - isSaved: false, - }, - internalNodeModified: false, - ...props, -}); diff --git a/webapp/src/components/MultiNodeEditors/types.ts b/webapp/src/components/MultiNodeEditors/types.ts deleted file mode 100644 index 3a8241b..0000000 --- a/webapp/src/components/MultiNodeEditors/types.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { AutomationNode, AutomationNodeMapping } from "types/automations"; - -export interface MultiNodeEditorProps { - sequence: AutomationNode[]; - allowedTypes: Array; - onClose?: () => void; - onSave?: (n: AutomationNode[]) => void; -}