From bbe6a5abf39d724083dad70fd6a6445d514fed9f Mon Sep 17 00:00:00 2001 From: Ari Date: Fri, 15 Jul 2022 11:06:52 -0400 Subject: [PATCH] better support for repeats --- .../DAGGraph/elements/index.stories.tsx | 12 +- .../components/DAGGraph/elements/index.tsx | 103 +++++++++++------- .../DAGGraph/nodes/ButtonNode/ButtonNode.tsx | 6 +- .../nodes/SequenceNode/SequenceNode.tsx | 28 +++-- .../DAGGraph/nodes/SequenceNode/types.ts | 4 +- .../src/components/DAGGraph/updater/index.ts | 60 +++++----- .../NodeEditor/OptionManager/ActionChoose.tsx | 20 +--- .../NodeEditor/OptionManager/ActionRepeat.tsx | 80 +++++++++----- webapp/src/types/automations/actions.ts | 19 +++- webapp/src/types/validators/actions.ts | 20 +++- webapp/src/utils/formatting.tsx | 13 ++- 11 files changed, 223 insertions(+), 142 deletions(-) diff --git a/webapp/src/components/DAGGraph/elements/index.stories.tsx b/webapp/src/components/DAGGraph/elements/index.stories.tsx index 61c4543..efb714b 100644 --- a/webapp/src/components/DAGGraph/elements/index.stories.tsx +++ b/webapp/src/components/DAGGraph/elements/index.stories.tsx @@ -409,7 +409,6 @@ RepeatAuto.args = { }, { repeat: { - count: 2, until: [ { condition: "state", @@ -425,6 +424,17 @@ RepeatAuto.args = { ], }, }, + { + repeat: { + count: 2, + sequence: [ + { + entity_id: "button.ping_mainboard", + service: "button.pres", + }, + ], + }, + }, ], }, }; diff --git a/webapp/src/components/DAGGraph/elements/index.tsx b/webapp/src/components/DAGGraph/elements/index.tsx index bd89ff4..2d395f9 100644 --- a/webapp/src/components/DAGGraph/elements/index.tsx +++ b/webapp/src/components/DAGGraph/elements/index.tsx @@ -148,7 +148,6 @@ export const makeSequenceNodes: ElementMaker = ( { color: getNodeType(node) === "action" ? "green" : "blue", enabled: node.enabled ?? true, - hasInput: true, label: getDescriptionFromAutomationNode(node, namer, true), ...state.getIsClosedActions(nodeId, node), ...stateUpdater.createNodeActions("sequence", nodeIndex, { @@ -403,27 +402,37 @@ export const makeRepeatNodes = makeSpecialNodeMaker( args.nodeIndex ); + let whileState: DAGElementsOutputState | null = null; + let untilState: DAGElementsOutputState | null = null; + // while - const whileState = makeConditionNodes( - convertScriptConditionFieldToAutomationConditions(node.repeat.while), - { - ...args, - stateUpdater: stateUpdater.while, - position: offsetPos, - lastNodeId, - nodeIndex: 0, - nodeId: `${args.nodeId}-repeat-while`, - } - ); - whileState.data.edges[0].sourceHandle = `side`; - whileState.data.edges[0].label = `While`; - outputState.extend(whileState); - offsetPos = distance.moveFromTo("collection", "node", offsetPos, args.dims); - lastNodeId = whileState.lastNodeId; + if ("while" in node.repeat) { + whileState = makeConditionNodes( + convertScriptConditionFieldToAutomationConditions(node.repeat.while), + { + ...args, + stateUpdater: stateUpdater, + position: offsetPos, + lastNodeId, + nodeIndex: 0, + nodeId: `${args.nodeId}-repeat-while`, + } + ); + whileState.data.edges[0].sourceHandle = `side`; + whileState.data.edges[0].label = `While`; + outputState.extend(whileState); + offsetPos = distance.moveFromTo( + "collection", + "node", + offsetPos, + args.dims + ); + lastNodeId = whileState.lastNodeId; + } // sequence const sequenceState = makeSequenceNodes(node.repeat.sequence, { ...args, - stateUpdater: stateUpdater.sequence, + stateUpdater: stateUpdater, position: offsetPos, lastNodeId: undefined, nodeIndex: 0, @@ -432,7 +441,12 @@ export const makeRepeatNodes = makeSpecialNodeMaker( if (lastNodeId && sequenceState.data.nodes.length > 0) { outputState.addEdge(lastNodeId, sequenceState.data.nodes[0].id, { animated: true, - label: "do", + label: + "count" in node.repeat + ? `repeat ${node.repeat.count} time${ + node.repeat.count !== 1 ? "s" : "" + }` + : "do", }); } lastNodeId = sequenceState.lastNodeId; @@ -443,28 +457,41 @@ export const makeRepeatNodes = makeSpecialNodeMaker( "node" ); // until - const untilState = makeConditionNodes( - convertScriptConditionFieldToAutomationConditions(node.repeat.until), - { - ...args, - stateUpdater: stateUpdater.until, - position: offsetPos, - lastNodeId, - nodeIndex: 0, - nodeId: `${args.nodeId}-repeat-until`, - } - ); - untilState.data.edges[0].label = `Until`; - untilState.data.edges[0].targetHandle = `side`; - outputState.extend(untilState); - offsetPos = distance.moveFromTo("collection", "node", offsetPos, args.dims); - lastNodeId = untilState.lastNodeId; + if ("until" in node.repeat) { + untilState = makeConditionNodes( + convertScriptConditionFieldToAutomationConditions(node.repeat.until), + { + ...args, + stateUpdater: stateUpdater, + position: offsetPos, + lastNodeId, + nodeIndex: 0, + nodeId: `${args.nodeId}-repeat-until`, + } + ); + untilState.data.edges[0].label = `Until`; + untilState.data.edges[0].targetHandle = `side`; + outputState.extend(untilState); + offsetPos = distance.moveFromTo( + "collection", + "node", + offsetPos, + args.dims + ); + lastNodeId = untilState.lastNodeId; + } // loop - if (untilState.lastNodeId && whileState.lastNodeId) { - outputState.addEdge(untilState.lastNodeId, whileState.lastNodeId, { + const firstNodeId = + whileState === null + ? sequenceState.data.nodes[0].id + : whileState.lastNodeId; + const finalNodeId = + untilState === null ? sequenceState.lastNodeId : untilState.lastNodeId; + if (finalNodeId && firstNodeId) { + outputState.addEdge(finalNodeId, firstNodeId, { animated: true, label: "repeat", - sourceHandle: "head", + sourceHandle: untilState === null ? "default" : "head", targetHandle: "return", }); } diff --git a/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx b/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx index 230c967..8d820fa 100644 --- a/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx @@ -16,7 +16,11 @@ export const ButtonNode: FC = ({ }) => { const inner = ( <> - + {icon} = ({ - hasInput = false, - ...props -}) => { +export const SequenceNode: FC = (props) => { return ( - {hasInput && ( - <> - - - )} + <> + + + + ( @@ -260,28 +261,23 @@ export const createUpdater = ( }, createRepeatNodeUpdater(i: number) { const { repeat, ...rest } = args.sequence.data[i] as RepeatAction; + const sequence = { + data: repeat.sequence, + onUpdate: (upd: AutomationNode[]) => + basic.sequence.updateNode( + { + ...rest, + repeat: { + ...repeat, + sequence: upd, + }, + }, + i + ), + }; - return { - sequence: createUpdater({ - openModal: args.openModal, - condition: createDummyUpdater(), - sequence: { - data: repeat.sequence, - onUpdate: (upd) => - basic.sequence.updateNode( - { - ...rest, - repeat: { - ...repeat, - sequence: upd, - }, - }, - i - ), - }, - trigger: createDummyUpdater(), - }), - while: createUpdater({ + if ("while" in repeat) { + return createUpdater({ openModal: args.openModal, condition: { data: convertScriptConditionFieldToAutomationConditions( @@ -299,10 +295,13 @@ export const createUpdater = ( i ), }, - sequence: createDummyUpdater(), trigger: createDummyUpdater(), - }), - until: createUpdater({ + sequence, + }); + } + + if ("until" in repeat) { + return createUpdater({ openModal: args.openModal, condition: { data: convertScriptConditionFieldToAutomationConditions( @@ -320,10 +319,17 @@ export const createUpdater = ( i ), }, - sequence: createDummyUpdater(), trigger: createDummyUpdater(), - }), - }; + sequence, + }); + } + + return createUpdater({ + openModal: args.openModal, + condition: createDummyUpdater(), + trigger: createDummyUpdater(), + sequence, + }); }, createParallelNodeUpdater(i: number, j: number) { const { parallel, ...rest } = args.sequence.data[i] as ParallelAction; diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx index 75dda9c..5205bff 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionChoose.tsx @@ -1,6 +1,5 @@ -import InputYaml from "components/Inputs/InputYaml"; import { ChooseAction } from "types/automations/actions"; -import { OptionManager, updateActionData } from "./OptionManager"; +import { OptionManager } from "./OptionManager"; export const ActionChooseState: OptionManager = { defaultState: () => ({ @@ -9,20 +8,7 @@ export const ActionChooseState: OptionManager = { choose: [], }), isReady: () => true, - Component: ({ state, setState }) => { - return ( - <> - setState({ ...state, choose })} - /> - setState({ ...state, default: d })} - /> - - ); + Component: ({}) => { + return <>; }, }; diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx index d4cc37a..c6ef956 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ActionRepeat.tsx @@ -1,7 +1,12 @@ import InputNumber from "components/Inputs/InputNumber"; import InputYaml from "components/Inputs/InputYaml"; +import { useState } from "react"; import { RepeatAction } from "types/automations/actions"; import { OptionManager, updateActionData } from "./OptionManager"; +import { InputAutoComplete } from "components/Inputs/InputAutoComplete"; + +const determineType = (repeatNode: RepeatAction["repeat"]) => + "count" in repeatNode ? "count" : "until" in repeatNode ? "until" : "while"; export const ActionRepeatState: OptionManager = { defaultState: () => ({ @@ -12,36 +17,57 @@ export const ActionRepeatState: OptionManager = { }, }), isReady: ({}) => true, - renderOptionList: (state, setState) => { - const update = updateActionData(state, setState); + Component: ({ state, setState }) => { + const repeatType = determineType(state.repeat); return ( <> - - update({ repeat: { ...state.repeat, count } }) - } - /> - - update({ repeat: { ...state.repeat, while: sequence } }) - } - /> - - update({ repeat: { ...state.repeat, sequence } }) - } - /> - update({ repeat: { ...state.repeat, until } })} + { + if (v === "while") { + setState({ + ...state, + repeat: { + sequence: state.repeat.sequence, + while: "until" in state.repeat ? state.repeat.until : [], + }, + }); + } else if (v === "until") { + setState({ + ...state, + repeat: { + sequence: state.repeat.sequence, + until: "while" in state.repeat ? state.repeat.while : [], + }, + }); + } else { + setState({ + ...state, + repeat: { + sequence: state.repeat.sequence, + count: 1, + }, + }); + } + }} /> + {"count" in state.repeat && ( + + setState({ + ...state, + repeat: { + sequence: state.repeat.sequence, + count, + }, + }) + } + /> + )} ); }, diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 462970a..d27e5fc 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -9,12 +9,19 @@ type AutomationActionNodeBase = Data & export type ServiceAction = st.Infer; export type RepeatAction = AutomationActionNodeBase<{ - repeat: { - count?: number; - while?: ScriptConditionField; - until?: ScriptConditionField; - sequence: AutomationAction[]; - }; + repeat: + | { + sequence: AutomationAction[]; + count: number; + } + | { + sequence: AutomationAction[]; + until: ScriptConditionField; + } + | { + sequence: AutomationAction[]; + while: ScriptConditionField; + }; }>; export type WaitAction = st.Infer; export type DelayAction = st.Infer; diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index 4395ef4..88a2ff2 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -25,12 +25,20 @@ export const ServiceAction = st.intersection([ export const RepeatAction = st.intersection([ AutomationActionNodeBase, st.type({ - repeat: st.object({ - count: st.optional(st.number()), - while: st.optional(ScriptConditionField), - until: st.optional(ScriptConditionField), - sequence: st.array(st.object()), - }), + repeat: st.union([ + st.object({ + count: st.optional(st.number()), + sequence: st.array(st.object()), + }), + st.object({ + until: st.optional(ScriptConditionField), + sequence: st.array(st.object()), + }), + st.object({ + while: st.optional(ScriptConditionField), + sequence: st.array(st.object()), + }), + ]), }), ]); diff --git a/webapp/src/utils/formatting.tsx b/webapp/src/utils/formatting.tsx index f74d525..d04e3f8 100644 --- a/webapp/src/utils/formatting.tsx +++ b/webapp/src/utils/formatting.tsx @@ -8,6 +8,7 @@ import { } from "types/automations/common"; import { AutomationTriggerZone } from "types/automations/triggers"; import StopIcon from "@mui/icons-material/PanToolOutlined"; +import { RepeatAction } from "types/automations/actions"; export interface Namer { getEntityName(entity_id: string | string[], maxEntities?: number): string; getDeviceName(device_id: string): string; @@ -137,10 +138,14 @@ export const getDescriptionFromAutomationNode = < return namer.getServiceName(node.service); } if ("repeat" in node) { - return ( - "Repeat " + - (Number.isInteger(node.repeat.count) ? node.repeat.count : "indefintely") - ); + const repeatNode = node.repeat; + if ("count" in repeatNode) { + return `Repeat ${repeatNode.count}`; + } + if ("while" in repeatNode) { + return `Repeat While`; + } + return "Repeat Until"; } if ("delay" in node) { return `Wait for ${convertTimeToString(node.delay)}`;