diff --git a/webapp/src/components/DAGFlow/DAGFlowUpdater.test.ts b/webapp/src/components/DAGFlow/DAGFlowUpdater.test.ts new file mode 100644 index 0000000..8ba1cf9 --- /dev/null +++ b/webapp/src/components/DAGFlow/DAGFlowUpdater.test.ts @@ -0,0 +1,155 @@ +import { AutomationSequenceNode } from "types/automations"; +import { ModalState } from "./types"; +import { makeDagFlowUpdate } from "./DAGFlowUpdater"; +import { AutomationTrigger } from "types/automations/triggers"; +import { AutomationCondition } from "types/automations/conditions"; + +const mockDAGFlowUpdater = ( + initialSequences: AutomationSequenceNode[] = [], + initialTriggers: AutomationTrigger[] = [], + initialConditions: AutomationCondition[] = [] +) => { + let sequence = [...initialSequences]; + let trigger = [...initialTriggers]; + let condition = [...initialConditions]; + const modalStates: ModalState[] = []; + + const updater = makeDagFlowUpdate({ + sequence, + trigger, + condition, + openModal: (m) => { + modalStates.push(m); + }, + onTriggerUpdate: (t) => { + trigger = [...t]; + }, + onSequenceUpdate: (s) => { + sequence = [...s]; + }, + onConditionUpdate: (c) => { + condition = [...c]; + }, + }); + return { + get condition() { + return condition; + }, + get trigger() { + return trigger; + }, + get sequence() { + return sequence; + }, + get modalStates() { + return modalStates; + }, + updater, + }; +}; + +test("DAGFlowUpdater.onaddNode add node when we have nothing", () => { + const emptyMock = mockDAGFlowUpdater(); + emptyMock.updater.addNode(null, "sequence"); + expect(emptyMock.modalStates.length).toEqual(1); + emptyMock.modalStates[0].update({ + device_id: "testme", + }); + expect((emptyMock.sequence[0] as any).device_id).toEqual("testme"); +}); + +test("DAGFlowUpdater.onaddNode add node to end when we have 1 thing", () => { + const mock = mockDAGFlowUpdater([ + { + choose: [], + }, + ]); + mock.updater.addNode(null, "sequence"); + expect(mock.modalStates.length).toEqual(1); + mock.modalStates[0].update({ + device_id: "testme", + }); + expect(mock.sequence.length).toEqual(2); + expect((mock.sequence[1] as any).device_id).toEqual("testme"); +}); + +test("DAGFlowUpdater.onaddNode add node to start when we have 1 thing", () => { + const mock = mockDAGFlowUpdater([ + { + choose: [], + }, + ]); + mock.updater.addNode(0, "sequence"); + expect(mock.modalStates.length).toEqual(1); + mock.modalStates[0].update({ + device_id: "testme", + }); + expect(mock.sequence.length).toEqual(2); + expect((mock.sequence[0] as any).device_id).toEqual("testme"); +}); + +test("DAGFlowUpdater.onaddNode add node to start when we have many things", () => { + const mock = mockDAGFlowUpdater([ + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + ]); + mock.updater.addNode(0, "sequence"); + expect(mock.modalStates.length).toEqual(1); + mock.modalStates[0].update({ + device_id: "testme", + }); + expect(mock.sequence.length).toEqual(8); + expect((mock.sequence[0] as any).device_id).toEqual("testme"); +}); + +test("DAGFlowUpdater.onaddNode add node to middle when we have many things", () => { + const mock = mockDAGFlowUpdater([ + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + { + choose: [], + }, + ]); + mock.updater.addNode(2, "sequence"); + expect(mock.modalStates.length).toEqual(1); + mock.modalStates[0].update({ + device_id: "testme", + }); + expect(mock.sequence.length).toEqual(8); + expect((mock.sequence[2] as any).device_id).toEqual("testme"); +}); diff --git a/webapp/src/components/DAGFlow/DAGFlowUpdater.ts b/webapp/src/components/DAGFlow/DAGFlowUpdater.ts new file mode 100644 index 0000000..8ce6e7b --- /dev/null +++ b/webapp/src/components/DAGFlow/DAGFlowUpdater.ts @@ -0,0 +1,117 @@ +import { AutomationSequenceNode } from "types/automations"; +import { AutomationCondition } from "types/automations/conditions"; +import { AutomationTrigger } from "types/automations/triggers"; +import { Namer } from "utils/formatting"; +import { makeOnEditAutomationConditions } from "./helpers"; +import { ModalState, TriggerMakerOptions } from "./types"; +import { makeSequenceUpdater, SequenceUpdater } from "./updater"; + +export type DagFlowUpdateBaseArgs = { + trigger: AutomationTrigger[]; + condition: AutomationCondition[]; + sequence: AutomationSequenceNode[]; + onTriggerUpdate: (t: AutomationTrigger[]) => void; + onSequenceUpdate: (s: AutomationSequenceNode[]) => void; + onConditionUpdate: (s: AutomationCondition[]) => void; +}; +export type DagFlowUpdateArgs = DagFlowUpdateBaseArgs & { + openModal: (m: ModalState) => void; +}; + +export type DagFlowUpdate = ReturnType; +export const makeDagFlowUpdate = (args: DagFlowUpdateArgs) => { + const methods = { + addNode( + afterIndex: number | null, + nodeType: "sequence" | "trigger" | "only-conditions" + ) { + return args.openModal({ + saveBtnCreateText: true, + node: + nodeType === "sequence" + ? { + device_id: "", + } + : nodeType === "trigger" + ? { + id: "", + } + : { + condition: "and", + conditions: [], + }, + update: (n) => { + console.log({ afterIndex, n }); + if (afterIndex !== null) { + args.onSequenceUpdate([ + ...args.sequence.slice(0, afterIndex), + n, + ...args.sequence.slice(afterIndex), + ]); + } else { + args.onSequenceUpdate([...args.sequence, n]); + } + }, + allowedTypes: + nodeType === "sequence" + ? ["action", "condition"] + : nodeType === "trigger" + ? ["trigger"] + : ["condition"], + }); + }, + onEditAutomationConditions() { + return makeOnEditAutomationConditions( + args.condition, + args.onConditionUpdate, + args.openModal + )(); + }, + makeTriggerMakerOptions(namer: Namer): TriggerMakerOptions { + return { + namer, + onAdd: () => + args.openModal({ + saveBtnCreateText: true, + node: { + platform: "device", + }, + update: (n) => { + args.onTriggerUpdate([...args.trigger, n]); + }, + allowedTypes: ["trigger"], + }), + onDelete: (i) => + args.onTriggerUpdate([ + ...args.trigger.slice(0, i), + ...args.trigger.slice(i + 1), + ]), + onEdit: (i) => + args.openModal({ + node: args.trigger[i], + update: (t) => + args.onTriggerUpdate([ + ...args.trigger.slice(0, i), + t, + ...args.trigger.slice(i + 1), + ]), + allowedTypes: ["trigger"], + }), + onSetEnabled: (i) => + args.onTriggerUpdate([ + ...args.trigger.slice(0, i), + { ...args.trigger[i], enabled: !(args.trigger[i].enabled ?? true) }, + ...args.trigger.slice(i + 1), + ]), + }; + }, + get sequenceUpdater(): SequenceUpdater { + return makeSequenceUpdater( + args.sequence, + args.onSequenceUpdate, + args.openModal + ); + }, + }; + return methods; +}; diff --git a/webapp/src/components/DAGFlow/DAGNode.css b/webapp/src/components/DAGFlow/DAGNode.css index dca8908..e09530a 100644 --- a/webapp/src/components/DAGFlow/DAGNode.css +++ b/webapp/src/components/DAGFlow/DAGNode.css @@ -42,6 +42,25 @@ right: -2.5em; } + +.dagnode--midway-options { + position: absolute; + bottom: -2.5em; + left: calc(50%-0.5em); + z-index: 10; +} + +.dagnode--midway-options button { + padding: 0; + color: var(--mui-primary-main); + background-color: var(--mui-primary-contrastText); +} + +.dagnode--midway-options button:hover { + color: var(--mui-primary-dark); + background-color: var(--mui-primary-contrastText); +} + .dagnode .dagnode--inner .speed-dial { height: 2em; width: 2em; diff --git a/webapp/src/components/DAGFlow/DAGNode.tsx b/webapp/src/components/DAGFlow/DAGNode.tsx index 67f53ea..1eb3bba 100644 --- a/webapp/src/components/DAGFlow/DAGNode.tsx +++ b/webapp/src/components/DAGFlow/DAGNode.tsx @@ -15,6 +15,8 @@ import { useTheme } from "@mui/material"; import InputBoolean from "components/Inputs/InputBoolean"; import { useConfirm } from "material-ui-confirm"; import { useSnackbar } from "notistack"; +import { ButtonIcon } from "components/Icons/ButtonIcons"; +import Add from "@mui/icons-material/Add"; export type DAGNodeOnMove = Record< Direction, @@ -27,6 +29,7 @@ export interface DAGNodeDataProps { onXClick?: () => void; onEditClick?: () => void; onSetEnabled?: () => void; + onAddNode?: () => void; onMove?: DAGNodeOnMoveEvents; color: NodeColor; label: ReactNode; @@ -43,6 +46,7 @@ export const makeConditionPoint = createToNodeMakerFunction< { label: ReactNode; onEditClick: () => void; + onAddNode: () => void; }, DAGNodeProps >((pre, { conditionWidth, conditionHeight, flipped }) => ({ @@ -71,6 +75,7 @@ export const DAGNode: FC = ({ width, onXClick, onEditClick = () => {}, + onAddNode, onMove = {}, color, hasInput = false, @@ -81,6 +86,16 @@ export const DAGNode: 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"; return ( <>
= ({ borderBottomColor: theme.palette.grey[500], borderLeftColor: theme.palette.grey[700], borderRightColor: theme.palette.grey[700], - borderTopColor: - 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", + borderTopColor: nodeColor, backgroundColor: enabled ? theme.palette.background.default : theme.palette.grey[600], @@ -167,7 +173,7 @@ export const DAGNode: FC = ({ onChange={onSetEnabled} /> )} - + {/* */} {hasInput && ( = ({ {typeof label === "string" ? {label} : label}
+ {onAddNode && ( +
+ } onClick={onAddNode} /> +
+ )} ); diff --git a/webapp/src/components/DAGFlow/constants.ts b/webapp/src/components/DAGFlow/constants.ts index c1eea33..abef8b1 100644 --- a/webapp/src/components/DAGFlow/constants.ts +++ b/webapp/src/components/DAGFlow/constants.ts @@ -5,7 +5,7 @@ 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.5 as const; +export const DISTANCE_FACTOR = 1.75 as const; export const PADDING = 50 as const; export const DEFAULT_DIMS = { @@ -19,5 +19,5 @@ export const DEFAULT_DIMS = { x: PADDING, y: PADDING, }, - flipped: false, + flipped: true, } as DAGAutomationFlowDims; diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index 8f0e4c1..e4131aa 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -21,6 +21,7 @@ import { convertToFlowNode, makeOnEditAutomationConditions } from "./helpers"; import { DAGErrorNode } from "./DAGErrorNode"; import { useHA } from "haService"; import { getDescriptionFromAutomationNode } from "utils/formatting"; +import { DagFlowUpdateBaseArgs, makeDagFlowUpdate } from "./DAGFlowUpdater"; const nodeTypes = { dagnode: convertToFlowNode(DAGNode), @@ -28,30 +29,21 @@ const nodeTypes = { dagcircle: DAGCircle, }; -export interface DAGAutomationFlowProps { +export type DAGAutomationFlowProps = DagFlowUpdateBaseArgs & { className?: string; - trigger: AutomationTrigger[]; - condition: AutomationCondition[]; - sequence: AutomationSequenceNode[]; dims: DAGAutomationFlowDims; - onTriggerUpdate: (t: AutomationTrigger[]) => void; - onSequenceUpdate: (s: AutomationSequenceNode[]) => void; - onConditionUpdate: (s: AutomationCondition[]) => void; -} +}; -export const DAGAutomationFlow: FC = ({ - className, - trigger, - condition, - sequence, - onTriggerUpdate, - onSequenceUpdate, - onConditionUpdate, - dims, -}) => { +export const DAGAutomationFlow: FC = (props) => { // state const [modalState, setModalState] = useState(null); const { namer } = useHA(); + const dagUpdater = makeDagFlowUpdate({ + ...props, + openModal: setModalState, + }); + // alias + const { className, dims, sequence, condition, trigger } = props; // modal let modalBody = <>; if (modalState) { @@ -91,11 +83,8 @@ export const DAGAutomationFlow: FC = ({ namer, true ), - onEditClick: makeOnEditAutomationConditions( - condition, - onConditionUpdate, - setModalState - ), + onEditClick: dagUpdater.onEditAutomationConditions, + onAddNode: () => dagUpdater.addNode(0, "sequence"), }, dims ); @@ -103,35 +92,13 @@ export const DAGAutomationFlow: FC = ({ nodes: [condPoint], edges: [], }; - triggerToFlow(flowData, trigger, condPoint.id, dims, { - namer, - onAdd: () => - setModalState({ - saveBtnCreateText: true, - node: { - platform: "device", - }, - update: (n) => { - onTriggerUpdate([...trigger, n]); - }, - allowedTypes: ["trigger"], - }), - onDelete: (i) => - onTriggerUpdate([...trigger.slice(0, i), ...trigger.slice(i + 1)]), - onEdit: (i) => - setModalState({ - node: trigger[i], - update: (t) => - onTriggerUpdate([...trigger.slice(0, i), t, ...trigger.slice(i + 1)]), - allowedTypes: ["trigger"], - }), - onSetEnabled: (i) => - onTriggerUpdate([ - ...trigger.slice(0, i), - { ...trigger[i], enabled: !(trigger[i].enabled ?? true) }, - ...trigger.slice(i + 1), - ]), - }); + triggerToFlow( + flowData, + trigger, + condPoint.id, + dims, + dagUpdater.makeTriggerMakerOptions(namer) + ); sequenceToFlow( flowData, sequence, @@ -149,7 +116,7 @@ export const DAGAutomationFlow: FC = ({ }, }, namer, - makeSequenceUpdater(sequence, onSequenceUpdate, setModalState) + dagUpdater.sequenceUpdater ); // this fixes some strange edge drawing issue flowData.edges = flowData.edges.map((e) => ({ diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts index 3037c80..4971a67 100644 --- a/webapp/src/components/DAGFlow/sequenceConvertor.ts +++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts @@ -82,6 +82,7 @@ export const sequenceToFlow = ( onXClick: () => updater.removeNode(i), onMove: updater.makeOnMoveEvents(i, dims.flipped), namer, + onAddNode: () => updater.addNode(i + 1), onSetEnabled: () => updater.updateNode(i, { ...node, @@ -95,6 +96,7 @@ export const sequenceToFlow = ( onXClick: () => updater.removeNode(i), onMove: updater.makeOnMoveEvents(i, dims.flipped), namer, + onAddNode: () => updater.addNode(i + 1), onSetEnabled: () => updater.updateNode(i, { ...node, @@ -107,34 +109,6 @@ export const sequenceToFlow = ( lasPos = position; } - // add button - const addCircle = makeAddButton( - `${prefix}-+`, - dims.flipped - ? { - y: - (lasPos === null - ? dims.padding.y + - dims.nodeHeight * dims.distanceFactor * (sequence.length + 1) - : lasPos.y) + - dims.nodeHeight * dims.distanceFactor, - x: dims.padding.x + dims.circleSize * 1.5, - } - : { - x: - (lasPos === null - ? dims.padding.x + - dims.nodeWidth * dims.distanceFactor * (sequence.length + 1) - : lasPos.x) + - dims.nodeWidth * dims.distanceFactor, - y: dims.padding.y + dims.circleSize / 4, - }, - dims, - updater.addNode, - false - ); - flowData.nodes.push(addCircle); - addEdge(flowData, lastPointId, addCircle.id, false); return { position: lasPos, pointId: lastPointId, @@ -172,6 +146,7 @@ const addSingleNode = ( opts: { onEditClick: () => void; onXClick: () => void; + onAddNode: () => void; namer: Namer; onMove: DAGNodeOnMoveEvents; onSetEnabled: () => void; @@ -232,6 +207,7 @@ const addChooseNode = ( onXClick: () => updater.removeNode(i), namer, onMove: updater.makeOnMoveEvents(i, dims.flipped), + onAddNode: () => updater.addNode(i + 1), onSetEnabled: () => updater.updateNode(i, { ...node, @@ -245,6 +221,7 @@ const addChooseNode = ( // conditions node.choose.forEach(({ sequence, conditions }, j) => { const sequenceId = `${nodeId}.${j}`; + const childUpdater = updater.makeChildUpdaterForChooseAction(i, j); // edit/delete circle const circle = makeFlowCircle( `${sequenceId}>delete`, @@ -296,6 +273,7 @@ const addChooseNode = ( true ), onEditClick: updater.makeOnEditConditionsForChooseNode(i, j), + onAddNode: () => childUpdater.addNode(0), }, dims ); @@ -324,7 +302,7 @@ const addChooseNode = ( }, }, namer, - updater.makeChildUpdaterForChooseAction(i, j), + childUpdater, `${sequenceId}.` ); if (lastPoint.position) { diff --git a/webapp/src/components/DAGFlow/updater.ts b/webapp/src/components/DAGFlow/updater.ts index 64e8da5..cfd65fa 100644 --- a/webapp/src/components/DAGFlow/updater.ts +++ b/webapp/src/components/DAGFlow/updater.ts @@ -19,14 +19,23 @@ export const makeSequenceUpdater = ( removeNode(i: number) { return onChange([...sequence.slice(0, i), ...sequence.slice(i + 1)]); }, - addNode() { + + addNode(afterIndex: number | null = null) { return openModal({ saveBtnCreateText: true, node: { device_id: "", }, update: (n) => { - onChange([...sequence, n]); + if (afterIndex !== null) { + onChange([ + ...sequence.slice(0, afterIndex), + n, + ...sequence.slice(afterIndex), + ]); + } else { + onChange([...sequence, n]); + } }, allowedTypes: ["action", "condition"], });