From 45ace08cbe73309c9682d1d5e7371f0590dee825 Mon Sep 17 00:00:00 2001 From: Ari Date: Thu, 14 Jul 2022 17:50:37 -0400 Subject: [PATCH] parallel nodes --- .../DAGGraph/elements/index.stories.tsx | 30 ++ .../components/DAGGraph/elements/index.tsx | 454 ++++++++++-------- .../DAGGraph/elements/specialnodes.ts | 43 ++ .../DAGGraph/nodes/ButtonNode/ButtonNode.tsx | 6 + .../src/components/DAGGraph/updater/index.ts | 47 +- webapp/src/types/automations/actions.ts | 30 +- webapp/src/utils/automations.ts | 15 +- 7 files changed, 406 insertions(+), 219 deletions(-) create mode 100644 webapp/src/components/DAGGraph/elements/specialnodes.ts diff --git a/webapp/src/components/DAGGraph/elements/index.stories.tsx b/webapp/src/components/DAGGraph/elements/index.stories.tsx index 991a954..61c4543 100644 --- a/webapp/src/components/DAGGraph/elements/index.stories.tsx +++ b/webapp/src/components/DAGGraph/elements/index.stories.tsx @@ -428,3 +428,33 @@ RepeatAuto.args = { ], }, }; + +export const ParallelAuto = Template.bind({}); +ParallelAuto.args = { + ...ParallelAuto.args, + automation: { + condition: [], + trigger: [], + sequence: [ + { + parallel: [ + { + sequence: [ + { + delay: { + hours: 1, + }, + }, + { + service: "light.turn_on", + }, + ], + }, + { + service: "light.turn_off", + }, + ], + }, + ], + }, +}; diff --git a/webapp/src/components/DAGGraph/elements/index.tsx b/webapp/src/components/DAGGraph/elements/index.tsx index 9541f6e..bd89ff4 100644 --- a/webapp/src/components/DAGGraph/elements/index.tsx +++ b/webapp/src/components/DAGGraph/elements/index.tsx @@ -8,7 +8,6 @@ import { } from "types/automations"; import { AutomationCondition } from "types/automations/conditions"; import { CollectionNodeMaker } from "../nodes/CollectionNode"; -import { ChooseAction, RepeatAction } from "types/automations/actions"; import { XYPosition } from "react-flow-renderer"; import { DAGElementsOutputState } from "./outputState"; import { SequenceNodeMaker } from "../nodes/SequenceNode"; @@ -21,6 +20,10 @@ import { AddIcon } from "components/Icons"; import { useDAGElementsState } from "./state"; import AddBox from "@mui/icons-material/AddBox"; import { DAGGraphChooseUpdater, DAGGraphUpdater } from "../updater"; +import { getSpecialNodeMaker, makeSpecialNodeMaker } from "./specialnodes"; +import { getNodeSubType } from "../../../utils/automations"; +import DeleteForeverIcon from "@mui/icons-material/DeleteForever"; +import Add from "@mui/icons-material/Add"; export const useAutomationNodes = ( automation: AutomationActionData, @@ -159,20 +162,12 @@ export const makeSequenceNodes: ElementMaker = ( if (nodeIndex === 0 && lastNodeId) { outputState.addEdge(lastNodeId, element.id); } - if ("choose" in node) { + const specialNodeMaker = getSpecialNodeMaker( + getNodeSubType(node, true) as any + ); + if (specialNodeMaker) { outputState.extend( - makeChooseeNodes([node], { - ...args, - nodeId, - lastNodeId: element.id, - position: outputState.getLastNodePos(), - nodeIndex, - }), - true - ); - } else if ("repeat" in node) { - outputState.extend( - makeRepeatNodes([node], { + specialNodeMaker([node] as any, { ...args, nodeId, lastNodeId: element.id, @@ -211,208 +206,192 @@ export const makeSequenceNodes: ElementMaker = ( return outputState; }; -export const makeChooseeNodes: ElementMaker = (nodes, args) => { - const outputState = new DAGElementsOutputState(args.position, args.dims); - if (nodes.length !== 1) { - throw new Error("makeChooseNodes only access a list of 1"); - } - const node = nodes[0]; - if ( - !((node.enabled ?? true) && !args.state.get(args.lastNodeId ?? "").isClosed) - ) { - return outputState; - } - // offset position - let offsetPos: XYPosition = distance.offsetBy( - "node", - args.position, - args.dims - ); - let lastConditionState: DAGElementsOutputState | null = null; - // functions - const drawCondition = ( - { stateUpdater, onRemove }: DAGGraphChooseUpdater, - position: XYPosition, - lastNodeId: string | undefined, - nodeIndex: number, - conditions: AutomationCondition[] - ) => { - const conditionState = makeConditionNodes(conditions, { - ...args, - stateUpdater, - position, - lastNodeId, - nodeIndex, - nodeId: `${args.nodeId}-choose-${nodeIndex}-condition`, - }); - lastConditionState = conditionState; - (conditionState.data.nodes[0].data as any).title = `Option ${ - nodeIndex + 1 - }`; - - (conditionState.data.nodes[0].data as any).onDelete = onRemove; - conditionState.data.edges[0].sourceHandle = `side`; - conditionState.data.edges[0].label = `Option ${nodeIndex + 1}`; - conditionState.data.edges[0].style = { - stroke: "var(--mui-info-main)", - }; - outputState.extend(conditionState); - return conditionState; - }; - const drawSequence = ( - { stateUpdater }: DAGGraphChooseUpdater, - position: XYPosition, - lastNodeId: string | undefined, - nodeIndex: number, - sequence: AutomationSequenceNode[], - label: "else" | undefined - ) => { - const output = makeSequenceNodes(sequence, { - ...args, - stateUpdater, - position, - lastNodeId: undefined, - nodeIndex, - nodeId: `${args.nodeId}-choose-${nodeIndex}-sequence`, - }); - if (lastNodeId && output.data.nodes.length > 0) { - outputState.addEdge(lastNodeId, output.data.nodes[0].id, { - animated: true, - label, - sourceHandle: label === "else" ? "side" : undefined, - style: { - stroke: "var(--mui-error-main)", - zIndex: -1, - }, +export const makeChooseeNodes = makeSpecialNodeMaker( + "choose", + (node, outputState, args) => { + // offset position + let offsetPos: XYPosition = distance.offsetBy( + "node", + args.position, + args.dims + ); + let lastConditionState: DAGElementsOutputState | null = null; + // functions + const drawCondition = ( + { stateUpdater, onRemove }: DAGGraphChooseUpdater, + position: XYPosition, + lastNodeId: string | undefined, + nodeIndex: number, + conditions: AutomationCondition[] + ) => { + const conditionState = makeConditionNodes(conditions, { + ...args, + stateUpdater, + position, + lastNodeId, + nodeIndex, + nodeId: `${args.nodeId}-choose-${nodeIndex}-condition`, }); - } - return output; - }; + lastConditionState = conditionState; + (conditionState.data.nodes[0].data as any).title = `Option ${ + nodeIndex + 1 + }`; - // process options - for (let sIndex = 0; sIndex < node.choose.length; sIndex++) { + (conditionState.data.nodes[0].data as any).onDelete = onRemove; + conditionState.data.edges[0].sourceHandle = `side`; + conditionState.data.edges[0].label = `Option ${nodeIndex + 1}`; + conditionState.data.edges[0].style = { + stroke: "var(--mui-info-main)", + }; + outputState.extend(conditionState); + return conditionState; + }; + const drawSequence = ( + { stateUpdater }: DAGGraphChooseUpdater, + position: XYPosition, + lastNodeId: string | undefined, + nodeIndex: number, + sequence: AutomationSequenceNode[], + label: "else" | undefined + ) => { + const output = makeSequenceNodes(sequence, { + ...args, + stateUpdater, + position, + lastNodeId: undefined, + nodeIndex, + nodeId: `${args.nodeId}-choose-${nodeIndex}-sequence`, + }); + if (lastNodeId && output.data.nodes.length > 0) { + outputState.addEdge(lastNodeId, output.data.nodes[0].id, { + animated: true, + label, + sourceHandle: label === "else" ? "side" : undefined, + style: { + stroke: "var(--mui-error-main)", + zIndex: -1, + }, + }); + } + return output; + }; + + // process options + for (let sIndex = 0; sIndex < node.choose.length; sIndex++) { + const stateUpdater = args.stateUpdater.createChoosNodeUpdater( + args.nodeIndex, + sIndex + ); + let pos = offsetPos; + let lastNodeId = args.lastNodeId; + const conditionState = drawCondition( + stateUpdater, + pos, + lastNodeId, + sIndex, + node.choose[sIndex].conditions + ); + pos = distance.moveFromTo("collection", "node", pos, args.dims); + lastConditionState = conditionState; + lastNodeId = conditionState.lastNodeId; + const sequence = node.choose[sIndex].sequence; + const output = drawSequence( + stateUpdater, + pos, + lastNodeId, + sIndex, + sequence, + undefined + ); + offsetPos = distance.moveAlongRelativeTo( + offsetPos, + output.bbox[1], + args.dims, + "collection" + ); + outputState.extend(output); + } + + // else const stateUpdater = args.stateUpdater.createChoosNodeUpdater( args.nodeIndex, - sIndex + "else" ); let pos = offsetPos; let lastNodeId = args.lastNodeId; - const conditionState = drawCondition( + const elseOutput = drawSequence( stateUpdater, pos, lastNodeId, - sIndex, - node.choose[sIndex].conditions - ); - pos = distance.moveFromTo("collection", "node", pos, args.dims); - lastConditionState = conditionState; - lastNodeId = conditionState.lastNodeId; - const sequence = node.choose[sIndex].sequence; - const output = drawSequence( - stateUpdater, - pos, - lastNodeId, - sIndex, - sequence, - undefined + node.choose.length, + node.default ?? [], + "else" ); offsetPos = distance.moveAlongRelativeTo( offsetPos, - output.bbox[1], + elseOutput.bbox[1], args.dims, - "collection" + "node" ); - outputState.extend(output); - } + outputState.extend(elseOutput); - // else - const stateUpdater = args.stateUpdater.createChoosNodeUpdater( - args.nodeIndex, - "else" - ); - let pos = offsetPos; - let lastNodeId = args.lastNodeId; - const elseOutput = drawSequence( - stateUpdater, - pos, - lastNodeId, - node.choose.length, - node.default ?? [], - "else" - ); - offsetPos = distance.moveAlongRelativeTo( - offsetPos, - elseOutput.bbox[1], - args.dims, - "node" - ); - outputState.extend(elseOutput); - - // add btn - let addPos = distance.moveAlong( - "node", - elseOutput.data.nodes[0].position, - 1 / args.dims.distanceFactor.node, - args.dims, - true - ); - console.log({ addPos, lastConditionState }); - if (lastConditionState !== null) { - addPos = distance.moveFromTo( - "collection", + // add btn + let addPos = distance.moveAlong( "node", - (lastConditionState as DAGElementsOutputState).nextPos, - { - ...args.dims, - flipped: !args.dims.flipped, - } - ); - } - const addElement = outputState.addNode( - ButtonNodeMaker.makeElement( - { - id: `${args.nodeId}-choose-add-new`, - position: addPos, - }, + elseOutput.data.nodes[0].position, + 1 / args.dims.distanceFactor.node, args.dims, - { - text: "Option", - icon: , - onClick: () => - args.stateUpdater.basic.sequence.updateNode( - { - ...node, - choose: [ - ...node.choose, - { - conditions: [], - sequence: [], - }, - ], - }, - args.nodeIndex - ), - } - ) - ); - outputState.addEdge(args.nodeId, addElement.id, { - animated: true, - sourceHandle: "side", - }); - - return outputState; -}; - -export const makeRepeatNodes: ElementMaker = (nodes, args) => { - const outputState = new DAGElementsOutputState(args.position, args.dims); - if (nodes.length !== 1) { - throw new Error("makeChooseNodes only access a list of 1"); + true + ); + console.log({ addPos, lastConditionState }); + if (lastConditionState !== null) { + addPos = distance.moveFromTo( + "collection", + "node", + (lastConditionState as DAGElementsOutputState).nextPos, + { + ...args.dims, + flipped: !args.dims.flipped, + } + ); + } + const addElement = outputState.addNode( + ButtonNodeMaker.makeElement( + { + id: `${args.nodeId}-choose-add-new`, + position: addPos, + }, + args.dims, + { + text: "Option", + icon: , + onClick: () => + args.stateUpdater.basic.sequence.updateNode( + { + ...node, + choose: [ + ...node.choose, + { + conditions: [], + sequence: [], + }, + ], + }, + args.nodeIndex + ), + } + ) + ); + outputState.addEdge(args.nodeId, addElement.id, { + animated: true, + sourceHandle: "side", + }); } - const node = nodes[0]; - if ( - (node.enabled ?? true) && - !args.state.get(args.lastNodeId ?? "").isClosed - ) { +); + +export const makeRepeatNodes = makeSpecialNodeMaker( + "repeat", + (node, outputState, args) => { // offset position let offsetPos: XYPosition = distance.offsetBy( "node", @@ -491,6 +470,83 @@ export const makeRepeatNodes: ElementMaker = (nodes, args) => { } outputState.extend(sequenceState); } +); - return outputState; -}; +export const makeParallelNodes = makeSpecialNodeMaker( + "parallel", + (node, outputState, args) => { + const offsetPos = distance.moveAlong("node", args.position, 0.5, args.dims); + for (let sIndex = 0; sIndex < node.parallel.length; sIndex++) { + const { stateUpdater, onRemove } = + args.stateUpdater.createParallelNodeUpdater(args.nodeIndex, sIndex); + const element = node.parallel[sIndex]; + const deleteBtn = ButtonNodeMaker.makeElement( + { + id: `${args.nodeId}-${sIndex}-delete`, + position: distance.moveAlong( + "node", + offsetPos, + sIndex + 1, + args.dims, + true + ), + }, + args.dims, + { + icon: , + onClick: onRemove, + } + ); + outputState.addNode(deleteBtn); + outputState.addEdge(args.nodeId, deleteBtn.id, { + sourceHandle: "side", + animated: true, + }); + const sequence = "sequence" in element ? element.sequence : [element]; + const sequenceState = makeSequenceNodes(sequence, { + ...args, + stateUpdater, + lastNodeId: deleteBtn.id, + nodeIndex: sIndex, + nodeId: `${args.nodeId}-${sIndex}`, + position: distance.moveAlong("node", deleteBtn.position, 1, args.dims), + }); + sequenceState.data.edges[0].sourceHandle = "next"; + outputState.extend(sequenceState); + } + const addBtn = ButtonNodeMaker.makeElement( + { + id: `${args.nodeId}-add`, + position: distance.moveAlong( + "node", + offsetPos, + node.parallel.length + 1, + args.dims, + true + ), + }, + args.dims, + { + icon: , + onClick: () => + args.stateUpdater.basic.sequence.updateNode( + { + ...node, + parallel: [ + ...node.parallel, + { + sequence: [], + }, + ], + }, + args.nodeIndex + ), + } + ); + outputState.addNode(addBtn); + outputState.addEdge(args.nodeId, addBtn.id, { + animated: true, + sourceHandle: "side", + }); + } +); diff --git a/webapp/src/components/DAGGraph/elements/specialnodes.ts b/webapp/src/components/DAGGraph/elements/specialnodes.ts new file mode 100644 index 0000000..b320665 --- /dev/null +++ b/webapp/src/components/DAGGraph/elements/specialnodes.ts @@ -0,0 +1,43 @@ +import { ActionType, ActionTypeMapping } from "types/automations/actions"; +import { DAGElementsOutputState } from "./outputState"; +import { ElementMaker, ElementMakerProps } from "./types"; + +const SpecialNodeRegistry: Partial>> = {}; + +export const makeSpecialNodeMaker = < + AT extends ActionType, + T extends ActionTypeMapping[AT] +>( + nodeType: AT, + run: ( + node: T, + outputState: DAGElementsOutputState, + args: ElementMakerProps + ) => any +): ElementMaker => { + const elementMaker: ElementMaker = (nodes, args) => { + const outputState = new DAGElementsOutputState(args.position, args.dims); + if (nodes.length !== 1) { + throw new Error("makeChooseNodes only access a list of 1"); + } + const node = nodes[0]; + if ( + !(node.enabled ?? true) || + args.state.get(args.lastNodeId ?? "").isClosed + ) { + return outputState; + } + + run(node, outputState, args); + + return outputState; + }; + + SpecialNodeRegistry[nodeType] = elementMaker; + + return elementMaker; +}; + +export const getSpecialNodeMaker = ( + nodeType: AT +): ElementMaker | null => SpecialNodeRegistry[nodeType] ?? null; diff --git a/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx b/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx index 2449992..230c967 100644 --- a/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/ButtonNode/ButtonNode.tsx @@ -19,9 +19,15 @@ export const ButtonNode: FC = ({ {icon} + ); return ( diff --git a/webapp/src/components/DAGGraph/updater/index.ts b/webapp/src/components/DAGGraph/updater/index.ts index 4bb5b4f..ef02eaf 100644 --- a/webapp/src/components/DAGGraph/updater/index.ts +++ b/webapp/src/components/DAGGraph/updater/index.ts @@ -6,7 +6,11 @@ import { ModalState } from "../board/types"; import { SequenceNodeActions } from "../nodes/SequenceNode/types"; import { DAGUpdaterArgs, DAGUpdaterInput } from "./types"; import { mapAutoActionKeyToNodeType } from "./util"; -import { ChooseAction, RepeatAction } from "types/automations/actions"; +import { + ChooseAction, + RepeatAction, + ParallelAction, +} from "types/automations/actions"; import { ScriptConditionField } from "types/automations/common"; import { convertScriptConditionFieldToAutomationConditions } from "utils/automations"; @@ -255,7 +259,7 @@ export const createUpdater = ( } }, createRepeatNodeUpdater(i: number) { - const { repeat } = args.sequence.data[i] as RepeatAction; + const { repeat, ...rest } = args.sequence.data[i] as RepeatAction; return { sequence: createUpdater({ @@ -266,6 +270,7 @@ export const createUpdater = ( onUpdate: (upd) => basic.sequence.updateNode( { + ...rest, repeat: { ...repeat, sequence: upd, @@ -285,6 +290,7 @@ export const createUpdater = ( onUpdate: (upd: any) => basic.sequence.updateNode( { + ...rest, repeat: { ...repeat, while: upd, @@ -305,6 +311,7 @@ export const createUpdater = ( onUpdate: (upd: any) => basic.sequence.updateNode( { + ...rest, repeat: { ...repeat, until: upd, @@ -318,6 +325,42 @@ export const createUpdater = ( }), }; }, + createParallelNodeUpdater(i: number, j: number) { + const { parallel, ...rest } = args.sequence.data[i] as ParallelAction; + const child = parallel[j]; + + return { + onRemove: () => + basic.sequence.updateNode( + { + ...rest, + parallel: [...parallel.slice(0, j), ...parallel.slice(j + 1)], + }, + i + ), + stateUpdater: createUpdater({ + openModal: args.openModal, + condition: createDummyUpdater(), + trigger: createDummyUpdater(), + sequence: { + data: "sequence" in child ? child.sequence : [child], + onUpdate(d) { + basic.sequence.updateNode( + { + ...rest, + parallel: [ + ...parallel.slice(0, j), + { sequence: d }, + ...parallel.slice(j + 1), + ], + }, + i + ); + }, + }, + }), + }; + }, }; }; diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 2c6ced1..462970a 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -30,28 +30,30 @@ export type ChooseAction = AutomationActionNodeBase<{ }>; export type ParallelAction = AutomationActionNodeBase<{ parallel: Array< - | AutomationAction + | AutomationSequenceNode | { - sequence: AutomationAction[]; + sequence: AutomationSequenceNode[]; } >; }>; +export type ActionTypeMapping = { + service: ServiceAction; + repeat: RepeatAction; + wait: WaitAction | DelayAction; + event: FireEventAction; + device: DeviceAction; + stop: StopAction; + choose: ChooseAction; + parallel: ParallelAction; +}; export type AutomationAction = - | ParallelAction | ServiceAction | RepeatAction | WaitAction - | DelayAction | FireEventAction | DeviceAction | StopAction - | ChooseAction; -export type ActionType = - | "service" - | "repeat" - | "wait" - | "event" - | "device" - | "stop" - | "choose" - | "parallel"; + | ChooseAction + | ParallelAction + | DelayAction; +export type ActionType = keyof ActionTypeMapping; diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 6896ab1..cf8b075 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -110,9 +110,15 @@ export const makeDefault = ( } }; -export const getNodeSubType = ( - node: AutomationNode -): AutomationNodeSubtype => { +export const getNodeSubType = < + T extends AutomationNodeTypes, + II extends boolean = false +>( + node: AutomationNode, + ignoreInvalid: II = false as II +): II extends false + ? AutomationNodeSubtype + : AutomationNodeSubtype | "" => { if ("condition" in node) { return node.condition as any; } else if ("platform" in node) { @@ -135,9 +141,10 @@ export const getNodeSubType = ( return "stop" as any; } else if ("parallel" in node) { return "parallel" as any; - } else { + } else if (!ignoreInvalid) { throw new Error("Invalid node type!"); } + return "" as any; }; export const TriggerValidators: Record = {