From f549993d3e94cd404b073215bcb655dcd1974688 Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 10 Jul 2022 12:40:44 -0400 Subject: [PATCH] abstsracted away sequence node collections added generic action updaters --- .../components/DAGGraph/elements/constants.ts | 14 +- .../components/DAGGraph/elements/distance.ts | 4 +- .../DAGGraph/elements/index.stories.tsx | 6 +- .../DAGGraph/elements/index.test.ts | 76 ++++----- .../src/components/DAGGraph/elements/index.ts | 153 ++++++++---------- .../src/components/DAGGraph/elements/types.ts | 8 +- .../nodes/CollectionNode/CollectionNode.css | 16 ++ .../nodes/CollectionNode/CollectionNode.tsx | 58 +++++++ .../DAGGraph/nodes/CollectionNode/index.tsx | 13 ++ .../DAGGraph/nodes/CollectionNode/types.ts | 19 +++ .../nodes/SequenceNode/SequenceNode.css | 3 + .../SequenceNode/SequenceNodeElement.tsx | 12 +- .../DAGGraph/nodes/SequenceNode/util.ts | 16 ++ .../nodes/TriggerNodes/TriggerNodes.tsx | 35 ---- .../DAGGraph/nodes/TriggerNodes/index.tsx | 14 -- .../DAGGraph/nodes/TriggerNodes/types.ts | 18 --- .../src/components/DAGGraph/updater/index.ts | 93 ++++++++++- .../src/components/DAGGraph/updater/util.ts | 10 ++ 18 files changed, 351 insertions(+), 217 deletions(-) create mode 100644 webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css create mode 100644 webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx create mode 100644 webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx create mode 100644 webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts create mode 100644 webapp/src/components/DAGGraph/nodes/SequenceNode/util.ts delete mode 100644 webapp/src/components/DAGGraph/nodes/TriggerNodes/TriggerNodes.tsx delete mode 100644 webapp/src/components/DAGGraph/nodes/TriggerNodes/index.tsx delete mode 100644 webapp/src/components/DAGGraph/nodes/TriggerNodes/types.ts create mode 100644 webapp/src/components/DAGGraph/updater/util.ts diff --git a/webapp/src/components/DAGGraph/elements/constants.ts b/webapp/src/components/DAGGraph/elements/constants.ts index 25b40f4..79a1f12 100644 --- a/webapp/src/components/DAGGraph/elements/constants.ts +++ b/webapp/src/components/DAGGraph/elements/constants.ts @@ -2,19 +2,21 @@ import { DAGDims } from "./types"; export const NODE_WIDTH = 170 as const; export const NODE_HEIGHT = 70 as const; -export const CONDITION_HEIGHT = 90 as const; -export const CONDITION_WIDTH = 200 as const; export const CIRCLE_SIZE = 40 as const; export const DISTANCE_FACTOR = 1.25 as const; -export const DEFAULT_DIMS = { +export const DEFAULT_DIMS: DAGDims = { + trigger: { + height: NODE_HEIGHT * 3, + width: NODE_WIDTH * 4.5, + }, node: { height: NODE_HEIGHT, width: NODE_WIDTH, }, condition: { - height: CONDITION_HEIGHT, - width: CONDITION_WIDTH, + height: NODE_HEIGHT * 3, + width: NODE_WIDTH * 4.5, }, circle: { height: CIRCLE_SIZE, @@ -26,4 +28,4 @@ export const DEFAULT_DIMS = { y: 50, }, flipped: true, -} as DAGDims; +}; diff --git a/webapp/src/components/DAGGraph/elements/distance.ts b/webapp/src/components/DAGGraph/elements/distance.ts index 9bdc28c..f4c6300 100644 --- a/webapp/src/components/DAGGraph/elements/distance.ts +++ b/webapp/src/components/DAGGraph/elements/distance.ts @@ -10,11 +10,11 @@ export const moveAlong = ( ): XYPosition => { const moveOnY = { y: startPoint.y + dims[by].height * dims.distanceFactor * increment, - x: startPoint.x + dims[by].width, + x: startPoint.x, }; const moveOnX = { x: startPoint.x + dims[by].width * dims.distanceFactor * increment, - y: startPoint.y + dims[by].height, + y: startPoint.y, }; if (direction === "next") { return dims.flipped ? moveOnX : moveOnY; diff --git a/webapp/src/components/DAGGraph/elements/index.stories.tsx b/webapp/src/components/DAGGraph/elements/index.stories.tsx index 2018aef..50baf8e 100644 --- a/webapp/src/components/DAGGraph/elements/index.stories.tsx +++ b/webapp/src/components/DAGGraph/elements/index.stories.tsx @@ -90,8 +90,10 @@ Conditions.args = { automation: { trigger: [ { - platform: "device", - device_id: "asdasdas", + platform: "zone", + zone: "zone.home", + event: "enter", + entity_id: "person.ari", }, ], condition: [ diff --git a/webapp/src/components/DAGGraph/elements/index.test.ts b/webapp/src/components/DAGGraph/elements/index.test.ts index 0333650..4ffc19a 100644 --- a/webapp/src/components/DAGGraph/elements/index.test.ts +++ b/webapp/src/components/DAGGraph/elements/index.test.ts @@ -36,42 +36,42 @@ const makeDummyArgs = (): ElementMakerProps => ({ openModal: () => {}, }); -test("triggers bbox computed correctly -- one node", () => { - const dummyArgs = makeDummyArgs(); - const state = makeTriggerNodes( - [{ platform: "homeassistant", event: "up" }], - dummyArgs - ); - expect(state.elementData.nodes.length).toEqual(1); - expect(state.bbox[0]).toStrictEqual({ - x: 0, - y: 0, - }); - expect(state.bbox[1]).toStrictEqual({ - y: DEFAULT_DIMS.node.height * 2, - x: DEFAULT_DIMS.node.width, - }); -}); +// test("triggers bbox computed correctly -- one node", () => { +// const dummyArgs = makeDummyArgs(); +// const state = makeTriggerNodes( +// [{ platform: "homeassistant", event: "up" }], +// dummyArgs +// ); +// expect(state.elementData.nodes.length).toEqual(1); +// expect(state.bbox[0]).toStrictEqual({ +// x: 0, +// y: 0, +// }); +// expect(state.bbox[1]).toStrictEqual({ +// y: DEFAULT_DIMS.node.height * 2, +// x: DEFAULT_DIMS.node.width, +// }); +// }); -test("triggers bbox computed correctly -- many nodes", () => { - const dummyArgs = makeDummyArgs(); - const state = makeTriggerNodes( - [ - { platform: "homeassistant", event: "up" }, - { platform: "homeassistant", event: "up" }, - { platform: "homeassistant", event: "up" }, - { platform: "homeassistant", event: "up" }, - { platform: "homeassistant", event: "up" }, - ], - dummyArgs - ); - expect(state.elementData.nodes.length).toEqual(5); - expect(state.bbox[0]).toStrictEqual({ - x: 0, - y: 0, - }); - expect(state.bbox[1]).toStrictEqual({ - y: DEFAULT_DIMS.node.height * 2, - x: DEFAULT_DIMS.node.width * 6, - }); -}); +// test("triggers bbox computed correctly -- many nodes", () => { +// const dummyArgs = makeDummyArgs(); +// const state = makeTriggerNodes( +// [ +// { platform: "homeassistant", event: "up" }, +// { platform: "homeassistant", event: "up" }, +// { platform: "homeassistant", event: "up" }, +// { platform: "homeassistant", event: "up" }, +// { platform: "homeassistant", event: "up" }, +// ], +// dummyArgs +// ); +// expect(state.elementData.nodes.length).toEqual(1); +// expect(state.bbox[0]).toStrictEqual({ +// x: 0, +// y: 0, +// }); +// expect(state.bbox[1]).toStrictEqual({ +// y: DEFAULT_DIMS.node.height * 2, +// x: DEFAULT_DIMS.node.width * 6, +// }); +// }); diff --git a/webapp/src/components/DAGGraph/elements/index.ts b/webapp/src/components/DAGGraph/elements/index.ts index 890b2b0..1be0f27 100644 --- a/webapp/src/components/DAGGraph/elements/index.ts +++ b/webapp/src/components/DAGGraph/elements/index.ts @@ -10,127 +10,114 @@ import * as distance from "./distance"; import { getDescriptionFromAutomationNode } from "utils/formatting"; import { AutomationActionData } from "types/automations"; import { AutomationCondition } from "types/automations/conditions"; -import { pointApply, updateBBox } from "./math"; +import { CollectionNodeMaker } from "../nodes/CollectionNode"; +import { LastNode } from "./types"; export const makeAutomationNodes = ( automation: AutomationActionData, args: ElementMakerBaseProps ) => { - let state: ElementMakerOutput = { + let state: ElementMakerOutput = makeTriggerNodes(automation.trigger, { + ...args, elementData: { nodes: [], edges: [], }, - bbox: [ - { x: 0, y: 0 }, - { x: 0, y: 0 }, - ], - }; - console.log(args); - state = makeTriggerNodes(automation.trigger, { - ...args, - elementData: state.elementData, - position: state.bbox[0], + position: args.dims.position, nodeId: "trigger", nodeIndex: 0, }); - console.log(state); - const conditionStartPoint = pointApply(...state.bbox, (a, b) => (a + b) / 2); - if (args.dims.flipped) { - conditionStartPoint.y += args.dims.node.height * args.dims.distanceFactor; - } else { - conditionStartPoint.x += args.dims.node.width * args.dims.distanceFactor; - } - const condOut = makeConditionNodes(automation.condition, { ...args, elementData: state.elementData, - position: pointApply(...state.bbox, (a, b) => (a + b) / 2), + position: { + x: + state.lastNode.pos.x + + args.dims.trigger.width * args.dims.distanceFactor, + y: + state.lastNode.pos.y + + (args.dims.trigger.height - args.dims.condition.height) / 2, + }, nodeId: "condition", - nodeIndex: 0, + nodeIndex: 1, + lastNodeId: state.lastNode.nodeId, }); - console.log(condOut); - return condOut.elementData; }; export const makeTriggerNodes: ElementMaker = ( nodes, - { - elementData, - dims, - namer, - openModal, - stateUpdater, - nodeId, - nodeIndex, - position, - } + { elementData, dims, namer, openModal, stateUpdater, nodeId, position } ) => { - let bbox: Bbox = [position, position]; - nodes.forEach((node, index) => { - const element = SequenceNodeMaker.makeElement( - { - id: `${nodeId}-${nodeIndex}-${index}`, - position: distance.moveAlong("node", position, index, dims), - }, - dims, - { - color: "red", + elementData.nodes.push( + CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { + ...dims.trigger, + color: "red", + onAddNode: () => stateUpdater.basic.trigger.addNode(), + nodes: nodes.map((node, index) => ({ enabled: node.enabled ?? true, label: getDescriptionFromAutomationNode(node, namer, true), - onSetEnabled: () => stateUpdater.basic.trigger.setEnabled(index), - onEditClick: () => - openModal({ - allowedTypes: ["trigger"], - node: node, - update: (n) => stateUpdater.basic.trigger.updateNode(n, index), - }), - } - ); - elementData.nodes.push(element); - bbox = updateBBox(bbox, element.position, dims.node); - }); - + ...stateUpdater.createNodeActions("trigger", index, { + flipped: dims.flipped, + }), + })), + }) + ); return { elementData, - bbox, + lastNode: { + nodeId, + pos: elementData.nodes[elementData.nodes.length - 1].position, + size: dims.trigger, + }, }; }; export const makeConditionNodes: ElementMaker = ( nodes, - { elementData, nodeId, nodeIndex, position, dims, namer } + { + elementData, + nodeId, + stateUpdater, + position, + dims, + namer, + openModal, + lastNodeId, + } ) => { - let bbox: Bbox = [position, position]; - - nodes.forEach((node, index) => { - const element = SequenceNodeMaker.makeElement( - { - id: `${nodeId}-${nodeIndex}-${index}`, - position: distance.moveAlong( - "condition", - position, - index, - dims, - "under" - ), - }, - dims, - { - color: "blue", + elementData.nodes.push( + CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { + ...dims.condition, + color: "blue", + hasInput: !!lastNodeId, + onAddNode: stateUpdater.basic.condition.addNode, + nodes: nodes.map((node, index) => ({ enabled: node.enabled ?? true, label: getDescriptionFromAutomationNode(node, namer, true), - } - ); - elementData.nodes.push(element); - bbox = updateBBox(bbox, element.position, dims.condition); - }); - + ...stateUpdater.createNodeActions("condition", index, { + flipped: dims.flipped, + }), + })), + }) + ); + if (lastNodeId) { + elementData.edges.push({ + source: lastNodeId, + target: elementData.nodes[elementData.nodes.length - 1].id, + id: `${lastNodeId}->${ + elementData.nodes[elementData.nodes.length - 1].id + }`, + }); + } return { elementData, - bbox, + lastNode: { + nodeId, + pos: elementData.nodes[elementData.nodes.length - 1].position, + size: dims.trigger, + }, }; }; diff --git a/webapp/src/components/DAGGraph/elements/types.ts b/webapp/src/components/DAGGraph/elements/types.ts index a0615bf..e3085fd 100644 --- a/webapp/src/components/DAGGraph/elements/types.ts +++ b/webapp/src/components/DAGGraph/elements/types.ts @@ -34,10 +34,16 @@ export type ElementMakerProps = ElementMakerBaseProps & { nodeId: string; nodeIndex: number; position: XYPosition; + lastNodeId?: string; +}; +export type LastNode = { + nodeId: string; + pos: XYPosition; + size: Size; }; export type ElementMakerOutput = { elementData: ElementData; - bbox: Bbox; + lastNode: LastNode; }; export type ElementMaker = ( diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css new file mode 100644 index 0000000..40909e4 --- /dev/null +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css @@ -0,0 +1,16 @@ +.collection-nodes { + --padding: 1.5em; + /* outline: 1px dotted white; */ + display: flex; + flex-wrap: wrap; + flex-direction: row; + align-items: center; + gap: 1.5em; + + border-color: var(--node-color); + border-style: solid; + border-width: 1px; + border-radius: 10px; + overflow: auto; + padding: var(--padding); +} \ No newline at end of file diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx new file mode 100644 index 0000000..a999a68 --- /dev/null +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx @@ -0,0 +1,58 @@ +import "./CollectionNode.css"; + +import Add from "@mui/icons-material/Add"; +import { ButtonIcon } from "components/Icons/ButtonIcons"; +import { FC } from "react"; +import { SequenceNodeElement } from "../SequenceNode/SequenceNodeElement"; +import { CollectionNodeProps } from "./types"; +import { Handle, Position } from "react-flow-renderer"; +import { useSequenceNodeColor } from "../SequenceNode/util"; + +export const CollectionNode: FC = ({ + onAddNode, + nodes, + sequenceNode, + height, + width, + flipped, + color, + hasInput = false, +}) => { + const nodeColor = useSequenceNodeColor(color); + + return ( +
+ {nodes.map((n, i) => ( + + ))} + } onClick={onAddNode} /> + + {hasInput && ( + + )} +
+ ); +}; diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx new file mode 100644 index 0000000..cf8fd38 --- /dev/null +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/index.tsx @@ -0,0 +1,13 @@ +import { NodeManager } from "components/DAGGraph/nodes/NodeManager"; +import { CollectionNode } from "./CollectionNode"; +import { CollectionNodeDataProps } from "./types"; + +export const CollectionNodeMaker = NodeManager.register( + "CollectionNode", + CollectionNode, + (pre: CollectionNodeDataProps, { node, flipped }) => ({ + ...pre, + sequenceNode: node, + flipped, + }) +); diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts b/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts new file mode 100644 index 0000000..6184cf2 --- /dev/null +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/types.ts @@ -0,0 +1,19 @@ +import { Size } from "components/DAGGraph/elements/types"; +import { ReactNode } from "react"; +import { SequenceNodeActions, SequenceNodeColor } from "../SequenceNode/types"; + +export type CollectionNodeChildProps = Partial & { + enabled: boolean; + label: ReactNode; +}; +export type CollectionNodeDataProps = Size & { + nodes: CollectionNodeChildProps[]; + onAddNode: () => void; + color: SequenceNodeColor; + hasInput?: boolean; +}; + +export type CollectionNodeProps = CollectionNodeDataProps & { + sequenceNode: Size; + flipped: boolean; +}; diff --git a/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.css b/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.css index dc44549..a060a59 100644 --- a/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.css +++ b/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNode.css @@ -4,6 +4,9 @@ justify-content: center; align-items: center; cursor: default; + /* outline-width: 1px; + outline-style: dotted; + outline-color: white; */ } .sequence-node--inner { diff --git a/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNodeElement.tsx b/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNodeElement.tsx index b75bfcd..05eacfc 100644 --- a/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNodeElement.tsx +++ b/webapp/src/components/DAGGraph/nodes/SequenceNode/SequenceNodeElement.tsx @@ -16,6 +16,7 @@ import InputBoolean from "components/Inputs/InputBoolean"; import { ButtonIcon } from "components/Icons/ButtonIcons"; import { SpeedDial } from "components/SpeedDial"; import { SequenceNodeElementProps } from "./types"; +import { useSequenceNodeColor } from "./util"; export const SequenceNodeElement: FC< PropsWithChildren @@ -36,16 +37,7 @@ export const SequenceNodeElement: FC< const theme = useTheme(); const confirm = useConfirm(); const snackbr = useSnackbar(); - const nodeColor = - color === "blue" - ? theme.palette.info.main - : color === "green" - ? theme.palette.success.light - : color === "lblue" - ? theme.palette.info.light - : color === "red" - ? theme.palette.error.dark - : "none"; + const nodeColor = useSequenceNodeColor(color); return ( <>
{ + const theme = useTheme(); + + return color === "blue" + ? theme.palette.info.main + : color === "green" + ? theme.palette.success.light + : color === "lblue" + ? theme.palette.info.light + : color === "red" + ? theme.palette.error.dark + : "none"; +}; diff --git a/webapp/src/components/DAGGraph/nodes/TriggerNodes/TriggerNodes.tsx b/webapp/src/components/DAGGraph/nodes/TriggerNodes/TriggerNodes.tsx deleted file mode 100644 index 746c824..0000000 --- a/webapp/src/components/DAGGraph/nodes/TriggerNodes/TriggerNodes.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import Add from "@mui/icons-material/Add"; -import { ButtonIcon } from "components/Icons/ButtonIcons"; -import { FC } from "react"; -import { SequenceNodeElement } from "../SequenceNode/SequenceNodeElement"; -import { TriggerNodeProps } from "./types"; - -export const TriggerNodes: FC = ({ - onAddNode, - nodes, - sequenceNode, - height, - width, - flipped, -}) => { - return ( -
- {nodes.map((n, i) => ( - - ))} - } onClick={onAddNode} /> -
- ); -}; diff --git a/webapp/src/components/DAGGraph/nodes/TriggerNodes/index.tsx b/webapp/src/components/DAGGraph/nodes/TriggerNodes/index.tsx deleted file mode 100644 index 6b5627c..0000000 --- a/webapp/src/components/DAGGraph/nodes/TriggerNodes/index.tsx +++ /dev/null @@ -1,14 +0,0 @@ -import { NodeManager } from "components/DAGGraph/nodes/NodeManager"; -import { TriggerNodes } from "./TriggerNodes"; -import { TriggerNodeDataProps } from "./types"; - -export const TriggerNodesMaker = NodeManager.register( - "TriggerNodes", - TriggerNodes, - (pre: TriggerNodeDataProps, { node, trigger, flipped }) => ({ - ...pre, - ...trigger, - sequenceNode: node, - flipped, - }) -); diff --git a/webapp/src/components/DAGGraph/nodes/TriggerNodes/types.ts b/webapp/src/components/DAGGraph/nodes/TriggerNodes/types.ts deleted file mode 100644 index 2d680bc..0000000 --- a/webapp/src/components/DAGGraph/nodes/TriggerNodes/types.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Size } from "components/DAGGraph/elements/types"; -import { ReactNode } from "react"; -import { SequenceNodeActions } from "../SequenceNode/types"; - -export type TriggerNodeChildProps = Partial & { - enabled: boolean; - label: ReactNode; -}; -export type TriggerNodeDataProps = { - nodes: TriggerNodeChildProps[]; - onAddNode: () => void; -}; - -export type TriggerNodeProps = TriggerNodeDataProps & - Size & { - sequenceNode: Size; - flipped: boolean; - }; diff --git a/webapp/src/components/DAGGraph/updater/index.ts b/webapp/src/components/DAGGraph/updater/index.ts index 622a09d..0f1e1f1 100644 --- a/webapp/src/components/DAGGraph/updater/index.ts +++ b/webapp/src/components/DAGGraph/updater/index.ts @@ -1,9 +1,12 @@ import { AutomationActionData, + AutomationNodeTypes, AutomationSequenceNode, } from "types/automations"; import { ModalState } from "../board/types"; +import { SequenceNodeActions } from "../nodes/SequenceNode/types"; import { DAGUpdaterArgs } from "./types"; +import { mapAutoActionKeyToNodeType } from "./util"; export const createBasicUpdater = ( @@ -12,6 +15,9 @@ export const createBasicUpdater = ) => (args: DAGUpdaterArgs) => { return { + get defaultNode() { + return JSON.parse(JSON.stringify(defaultNode)); + }, setEnabled: (i: number) => args[k].onUpdate([ ...args[k].data.slice(0, i), @@ -29,19 +35,39 @@ export const createBasicUpdater = ...args[k].data.slice(0, i), ...args[k].data.slice(i + 1), ]), + addNode: () => - args[k].onUpdate([ - ...args[k].data, - JSON.parse(JSON.stringify(defaultNode)), - ]), + args.openModal({ + allowedTypes: mapAutoActionKeyToNodeType(k), + saveBtnCreateText: true, + node: JSON.parse(JSON.stringify(defaultNode)), + update: (n) => args[k].onUpdate([...args[k].data, n]), + }), + onMove: (i: number, direction: "back" | "forward") => { + if (direction === "back" && i > 0) { + return args[k].onUpdate([ + ...args[k].data.slice(0, i - 1), + args[k].data[i], + args[k].data[i - 1], + ...args[k].data.slice(i + 1), + ]); + } else if (direction === "forward" && i < args[k].data.length - 1) { + return args[k].onUpdate([ + ...args[k].data.slice(0, i), + args[k].data[i + 1], + args[k].data[i], + ...args[k].data.slice(i + 2), + ]); + } + }, }; }; export type DAGGraphUpdater = ReturnType; export const createUpdater = ( args: DAGUpdaterArgs<"sequence" | "condition" | "trigger"> -) => ({ - basic: { +) => { + const basic = { sequence: createBasicUpdater("sequence", { device_id: "", })({ @@ -63,8 +89,59 @@ export const createUpdater = ( condition: args.condition, openModal: args.openModal, } as any), - }, -}); + }; + + return { + basic, + createNodeActions( + k: K, + i: number, + flags: Partial<{ + includeAdd: boolean; + flipped: boolean; + }> + ): Partial { + // alias + const allowedTypes = mapAutoActionKeyToNodeType(k); + // build move + const onMove: SequenceNodeActions["onMove"] = {}; + if (!flags.flipped) { + if (i > 0) { + onMove["left"] = () => basic[k].onMove(i, "back"); + } + if (i < args[k].data.length - 1) { + onMove["right"] = () => basic[k].onMove(i, "forward"); + } + } else { + if (i > 0) { + onMove["up"] = () => basic[k].onMove(i, "back"); + } + if (i < args[k].data.length - 1) { + onMove["down"] = () => basic[k].onMove(i, "forward"); + } + } + // build add + let onAddNode: (() => void) | undefined = undefined; + if (flags.includeAdd) { + onAddNode = basic[k].addNode; + } + + return { + onEditClick: () => + args.openModal({ + allowedTypes, + node: args[k].data[i], + saveBtnCreateText: false, + update: (n) => basic[k].updateNode(n, i), + }), + onXClick: () => basic[k].deleteNode(i), + onSetEnabled: () => basic[k].setEnabled(i), + onAddNode, + onMove, + }; + }, + }; +}; export const createUpdaterFromAutomationData = ( openModal: (m: ModalState) => void, diff --git a/webapp/src/components/DAGGraph/updater/util.ts b/webapp/src/components/DAGGraph/updater/util.ts new file mode 100644 index 0000000..5cf9bf3 --- /dev/null +++ b/webapp/src/components/DAGGraph/updater/util.ts @@ -0,0 +1,10 @@ +import { AutomationActionData, AutomationNodeTypes } from "types/automations"; + +export const mapAutoActionKeyToNodeType = ( + k: keyof AutomationActionData +): AutomationNodeTypes[] => + k === "condition" + ? ["condition"] + : k === "trigger" + ? ["trigger"] + : ["action", "condition"];