diff --git a/webapp/src/components/ConditionEditor/viewRender.tsx b/webapp/src/components/ConditionEditor/viewRender.tsx index dcc5d6d..3c9a17d 100644 --- a/webapp/src/components/ConditionEditor/viewRender.tsx +++ b/webapp/src/components/ConditionEditor/viewRender.tsx @@ -1,45 +1,53 @@ import { FC } from "react"; -import { AutomationCondition, LogicCondition } from "types/automations/conditions"; +import { + AutomationCondition, + LogicCondition, +} from "types/automations/conditions"; import { getDescriptionFromAutomationNode, Namer } from "utils/formatting"; import { ConditionNode } from "./ConditionNode"; import { genUpdateMethods } from "./nestedUpdater"; - -interface Viewer extends FC<{ - condition: C; - namer: Namer; - onChange: (condition: C) => void; -}> { } +interface Viewer + extends FC<{ + condition: C; + namer: Namer; + onChange: (condition: C) => void; + }> {} export const getViewer = (condition: AutomationCondition): Viewer => { if ( - (condition.condition === 'and') || - (condition.condition === 'or') || - (condition.condition === 'not') + condition.condition === "and" || + condition.condition === "or" || + condition.condition === "not" ) { - return LogicViewer + return LogicViewer; } - return GenericViewer -} + return GenericViewer; +}; export const GenericViewer: Viewer = ({ condition, namer, }) => { - return <> - {getDescriptionFromAutomationNode(condition, namer)} - -} + return <>{getDescriptionFromAutomationNode(condition, namer, true)}; +}; export const LogicViewer: Viewer = ({ condition, onChange, }) => { - return <> - {condition.conditions.map((c, i) => { - return - })} - -} + return ( + <> + {condition.conditions.map((c, i) => { + return ( + + ); + })} + + ); +}; diff --git a/webapp/src/components/DAGFlow/sequenceConvertor.ts b/webapp/src/components/DAGFlow/sequenceConvertor.ts index 335bbef..1c17db0 100644 --- a/webapp/src/components/DAGFlow/sequenceConvertor.ts +++ b/webapp/src/components/DAGFlow/sequenceConvertor.ts @@ -1,312 +1,380 @@ -import { AutomationSequenceNode } from 'types/automations'; -import { DAGAutomationFlowDims, FlowData } from './types'; -import { getDescriptionFromAutomationNode, Namer } from 'utils/formatting'; -import { XYPosition } from 'react-flow-renderer'; -import { ChooseAction } from 'types/automations/actions'; -import { makeFlowCircle } from './DAGCircle'; -import { createOnRemoveForChooseNode, SequenceUpdater } from './updater'; -import { getNodeType } from 'utils/automations'; -import { makeAddButton, makeConditionPoint } from './flowDataMods'; - +import { AutomationSequenceNode } from "types/automations"; +import { DAGAutomationFlowDims, FlowData } from "./types"; +import { getDescriptionFromAutomationNode, Namer } from "utils/formatting"; +import { XYPosition } from "react-flow-renderer"; +import { ChooseAction } from "types/automations/actions"; +import { makeFlowCircle } from "./DAGCircle"; +import { createOnRemoveForChooseNode, SequenceUpdater } from "./updater"; +import { getNodeType } from "utils/automations"; +import { makeAddButton, makeConditionPoint } from "./flowDataMods"; export const sequenceToFlow = ( - flowData: FlowData, - sequence: AutomationSequenceNode[], - fromPoint: string, - dims: DAGAutomationFlowDims, - namer: Namer, - updater: SequenceUpdater, - prefix: string = "", + flowData: FlowData, + sequence: AutomationSequenceNode[], + fromPoint: string, + dims: DAGAutomationFlowDims, + namer: Namer, + updater: SequenceUpdater, + prefix: string = "" ) => { - let lastPointId = fromPoint; - let lasPos: XYPosition | null = null; - // sequence - for (let i = 0; i < sequence.length; i++) { - const node = sequence[i]; - const nodeId = `${prefix}n-${i}`; - let position: XYPosition; - if (lasPos) { - position = dims.flipped ? { - y: lasPos.y + dims.nodeHeight * dims.distanceFactor, - x: dims.padding.x - } : { - x: lasPos.x + dims.nodeWidth * dims.distanceFactor, - y: dims.padding.y - } - } else { - position = dims.flipped ? { - y: dims.padding.y + dims.nodeHeight * dims.distanceFactor * (i + 2), - x: dims.padding.x - } : { - x: dims.padding.x + dims.nodeWidth * dims.distanceFactor * (i + 2), - y: dims.padding.y - }; - } - // draw node - if (getNodeType(node) === 'action') { - if ('choose' in node) { - try { - const out = addChooseNode(node, position, nodeId, dims, namer, updater, i); - position = out.lastPos - flowData = mergeFlowDatas(flowData, out.flowData); - } catch (err) { - console.error(err) - addErrorNode(flowData, nodeId, position, dims, err, updater.makeOnEditForBadNode(i)); - } - } else { - addSingleNode(flowData, node, position, nodeId, dims, { - onEditClick: updater.makeOnModalOpenForNode(i, node), - onXClick: () => updater.removeNode(i), - namer, - }); - } - } else { - addSingleNode(flowData, node, position, nodeId, dims, { - onEditClick: updater.makeOnModalOpenForNode(i, node), - onXClick: () => updater.removeNode(i), - namer, - }); - - } - addEdge(flowData, lastPointId, nodeId, false) - lastPointId = nodeId; - lasPos = position; + let lastPointId = fromPoint; + let lasPos: XYPosition | null = null; + // sequence + for (let i = 0; i < sequence.length; i++) { + const node = sequence[i]; + const nodeId = `${prefix}n-${i}`; + let position: XYPosition; + if (lasPos) { + position = dims.flipped + ? { + y: lasPos.y + dims.nodeHeight * dims.distanceFactor, + x: dims.padding.x, + } + : { + x: lasPos.x + dims.nodeWidth * dims.distanceFactor, + y: dims.padding.y, + }; + } else { + position = dims.flipped + ? { + y: dims.padding.y + dims.nodeHeight * dims.distanceFactor * (i + 2), + x: dims.padding.x, + } + : { + x: dims.padding.x + dims.nodeWidth * dims.distanceFactor * (i + 2), + y: dims.padding.y, + }; } + // draw node + if (getNodeType(node) === "action") { + if ("choose" in node) { + try { + const out = addChooseNode( + node, + position, + nodeId, + dims, + namer, + updater, + i + ); + position = out.lastPos; + flowData = mergeFlowDatas(flowData, out.flowData); + } catch (err) { + console.error(err); + addErrorNode( + flowData, + nodeId, + position, + dims, + err, + updater.makeOnEditForBadNode(i) + ); + } + } else { + addSingleNode(flowData, node, position, nodeId, dims, { + onEditClick: updater.makeOnModalOpenForNode(i, node), + onXClick: () => updater.removeNode(i), + namer, + }); + } + } else { + addSingleNode(flowData, node, position, nodeId, dims, { + onEditClick: updater.makeOnModalOpenForNode(i, node), + onXClick: () => updater.removeNode(i), + namer, + }); + } + addEdge(flowData, lastPointId, nodeId, false); + lastPointId = nodeId; + 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 + // 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, - }; -} + dims, + updater.addNode, + false + ); + flowData.nodes.push(addCircle); + addEdge(flowData, lastPointId, addCircle.id, false); + return { + position: lasPos, + pointId: lastPointId, + }; +}; -const xyApply = (p1: XYPosition, p2: XYPosition, fcn: (a: number, b: number) => number): XYPosition => ({ - x: fcn(p1.x, p2.x), y: fcn(p1.y, p2.y) -}) +const xyApply = ( + p1: XYPosition, + p2: XYPosition, + fcn: (a: number, b: number) => number +): XYPosition => ({ + x: fcn(p1.x, p2.x), + y: fcn(p1.y, p2.y), +}); -const addEdge = (flowData: FlowData, source: string, target: string, animated: boolean = false) => flowData.edges.push({ +const addEdge = ( + flowData: FlowData, + source: string, + target: string, + animated: boolean = false +) => + flowData.edges.push({ id: `${source}-${target}`, source, target, animated, -}) + }); const addSingleNode = ( - flowData: FlowData, - node: AutomationSequenceNode, - position: XYPosition, - nodeId: string, - { - flipped, - nodeHeight, - nodeWidth, - }: DAGAutomationFlowDims, - opts: { - onEditClick: () => void; - onXClick: () => void; - namer: Namer; - } -) => flowData.nodes.push({ + flowData: FlowData, + node: AutomationSequenceNode, + position: XYPosition, + nodeId: string, + { flipped, nodeHeight, nodeWidth }: DAGAutomationFlowDims, + opts: { + onEditClick: () => void; + onXClick: () => void; + namer: Namer; + } +) => + flowData.nodes.push({ id: nodeId, - type: 'dagnode', + type: "dagnode", data: { - label: getDescriptionFromAutomationNode(node, opts.namer), - height: nodeHeight, - width: nodeWidth, - color: getNodeType(node) === 'action' ? 'green' : 'blue', - hasInput: true, - flipped, - ...opts, + label: getDescriptionFromAutomationNode(node, opts.namer, true), + height: nodeHeight, + width: nodeWidth, + color: getNodeType(node) === "action" ? "green" : "blue", + hasInput: true, + flipped, + ...opts, }, position, -}); + }); const addErrorNode = ( - flowData: FlowData, - nodeId: string, - position: XYPosition, - { - nodeHeight, - nodeWidth, - }: DAGAutomationFlowDims, - error: any, - onEdit: () => void, - hasInput: boolean = true, -) => flowData.nodes.push({ + flowData: FlowData, + nodeId: string, + position: XYPosition, + { nodeHeight, nodeWidth }: DAGAutomationFlowDims, + error: any, + onEdit: () => void, + hasInput: boolean = true +) => + flowData.nodes.push({ id: nodeId, - type: 'errornode', + type: "errornode", position, data: { - height: nodeHeight, - width: nodeWidth, - hasInput, - onEdit, - error, - } -}) - - + height: nodeHeight, + width: nodeWidth, + hasInput, + onEdit, + error, + }, + }); const addChooseNode = ( - node: ChooseAction, - position: XYPosition, - nodeId: string, - dims: DAGAutomationFlowDims, - namer: Namer, - updater: SequenceUpdater, - i: number, + node: ChooseAction, + position: XYPosition, + nodeId: string, + dims: DAGAutomationFlowDims, + namer: Namer, + updater: SequenceUpdater, + i: number ) => { - const flowData: FlowData = { - nodes: [], - edges: [], - }; - addSingleNode(flowData, node, position, nodeId, dims, { - onEditClick: updater.makeOnModalOpenForNode(i, node), - onXClick: () => updater.removeNode(i), - namer, - }); - let lastPos: XYPosition = position; - // conditions - node.choose.forEach(({ sequence, conditions }, j) => { - const sequenceId = `${nodeId}.${j}`; - // edit/delete circle - const circle = makeFlowCircle(`${sequenceId}>delete`, dims.flipped ? { + const flowData: FlowData = { + nodes: [], + edges: [], + }; + addSingleNode(flowData, node, position, nodeId, dims, { + onEditClick: updater.makeOnModalOpenForNode(i, node), + onXClick: () => updater.removeNode(i), + namer, + }); + let lastPos: XYPosition = position; + // conditions + node.choose.forEach(({ sequence, conditions }, j) => { + const sequenceId = `${nodeId}.${j}`; + // edit/delete circle + const circle = makeFlowCircle( + `${sequenceId}>delete`, + dims.flipped + ? { y: position.y + dims.nodeHeight * dims.distanceFactor, x: lastPos.x + dims.nodeWidth * dims.distanceFactor, - } : { + } + : { x: position.x + dims.nodeWidth * dims.distanceFactor, y: lastPos.y + dims.nodeHeight * dims.distanceFactor, - }, { - flipped: dims.flipped, - size: dims.circleSize, - onRemove: createOnRemoveForChooseNode( - node, - d => updater.updateNode(i, d), - j, - ), - }) - flowData.nodes.push(circle) - addEdge(flowData, nodeId, circle.id, true) - const condNode = makeConditionPoint( - `${sequenceId}>cond`, - dims.flipped ? { - y: circle.position.y + dims.circleSize * dims.distanceFactor, - x: circle.position.x - dims.circleSize, - } : { - x: circle.position.x + dims.circleSize * dims.distanceFactor, - y: circle.position.y + dims.circleSize / 8, - }, - dims, - conditions.length, - updater.makeOnEditConditionsForChooseNode(i, j) - ); - flowData.nodes.push(condNode) - addEdge(flowData, circle.id, condNode.id, true) - - // actual sub sequence - const lastPoint = sequenceToFlow(flowData, sequence, condNode.id, { - ...dims, - padding: dims.flipped ? { - y: condNode.position.y - dims.nodeHeight * dims.distanceFactor, - x: condNode.position.x - dims.conditionHeight / 2, - } : { - x: condNode.position.x - (dims.nodeWidth + dims.conditionWidth), - y: condNode.position.y - dims.conditionHeight / 2, - }, - }, namer, updater.makeChildUpdaterForChooseAction(i, j), `${sequenceId}.`); - if (lastPoint.position) { - lastPos = xyApply(lastPos, lastPoint.position, Math.max) - } else { - lastPos = xyApply(lastPos, dims.flipped ? { - y: lastPos.y, - x: lastPos.x + dims.nodeWidth * dims.distanceFactor - } : { - x: lastPos.x, - y: lastPos.y + dims.nodeHeight * dims.distanceFactor - }, Math.max) - } - }) - const totalChildren = node.choose.length; - - // add button - const addCirlce = makeAddButton( - `${nodeId}>add`, - dims.flipped ? { - y: position.y + dims.nodeHeight * dims.distanceFactor, - x: lastPos.x + dims.nodeWidth * dims.distanceFactor, - } : { - x: position.x + dims.nodeWidth * dims.distanceFactor, - y: lastPos.y + dims.nodeHeight * dims.distanceFactor, - }, - dims, - () => updater.updateNode(i, { - ...node, - choose: [ - ...node.choose, - { - sequence: [], - conditions: [], - } - ] - }), - false, + }, + { + flipped: dims.flipped, + size: dims.circleSize, + onRemove: createOnRemoveForChooseNode( + node, + (d) => updater.updateNode(i, d), + j + ), + } ); - flowData.nodes.push(addCirlce) - addEdge(flowData, nodeId, addCirlce.id, true) + flowData.nodes.push(circle); + addEdge(flowData, nodeId, circle.id, true); + const condNode = makeConditionPoint( + `${sequenceId}>cond`, + dims.flipped + ? { + y: circle.position.y + dims.circleSize * dims.distanceFactor, + x: circle.position.x - dims.circleSize, + } + : { + x: circle.position.x + dims.circleSize * dims.distanceFactor, + y: circle.position.y + dims.circleSize / 8, + }, + dims, + conditions.length, + updater.makeOnEditConditionsForChooseNode(i, j) + ); + flowData.nodes.push(condNode); + addEdge(flowData, circle.id, condNode.id, true); - // else - const elseCircle = makeFlowCircle(`${nodeId}>else`, dims.flipped ? { - y: position.y + dims.nodeHeight * dims.distanceFactor, - x: lastPos.x + dims.nodeWidth * dims.distanceFactor * 2, - } : { - x: position.x + dims.nodeWidth * dims.distanceFactor, - y: lastPos.y + dims.nodeHeight * dims.distanceFactor * 2, - }, { size: dims.circleSize, flipped: dims.flipped }) - flowData.nodes.push(elseCircle) - addEdge(flowData, nodeId, elseCircle.id, true) - const lastPoint = sequenceToFlow(flowData, node.default ?? [], elseCircle.id, { + // actual sub sequence + const lastPoint = sequenceToFlow( + flowData, + sequence, + condNode.id, + { ...dims, - padding: dims.flipped ? { + padding: dims.flipped + ? { + y: condNode.position.y - dims.nodeHeight * dims.distanceFactor, + x: condNode.position.x - dims.conditionHeight / 2, + } + : { + x: condNode.position.x - (dims.nodeWidth + dims.conditionWidth), + y: condNode.position.y - dims.conditionHeight / 2, + }, + }, + namer, + updater.makeChildUpdaterForChooseAction(i, j), + `${sequenceId}.` + ); + if (lastPoint.position) { + lastPos = xyApply(lastPos, lastPoint.position, Math.max); + } else { + lastPos = xyApply( + lastPos, + dims.flipped + ? { + y: lastPos.y, + x: lastPos.x + dims.nodeWidth * dims.distanceFactor, + } + : { + x: lastPos.x, + y: lastPos.y + dims.nodeHeight * dims.distanceFactor, + }, + Math.max + ); + } + }); + const totalChildren = node.choose.length; + + // add button + const addCirlce = makeAddButton( + `${nodeId}>add`, + dims.flipped + ? { + y: position.y + dims.nodeHeight * dims.distanceFactor, + x: lastPos.x + dims.nodeWidth * dims.distanceFactor, + } + : { + x: position.x + dims.nodeWidth * dims.distanceFactor, + y: lastPos.y + dims.nodeHeight * dims.distanceFactor, + }, + dims, + () => + updater.updateNode(i, { + ...node, + choose: [ + ...node.choose, + { + sequence: [], + conditions: [], + }, + ], + }), + false + ); + flowData.nodes.push(addCirlce); + addEdge(flowData, nodeId, addCirlce.id, true); + + // else + const elseCircle = makeFlowCircle( + `${nodeId}>else`, + dims.flipped + ? { + y: position.y + dims.nodeHeight * dims.distanceFactor, + x: lastPos.x + dims.nodeWidth * dims.distanceFactor * 2, + } + : { + x: position.x + dims.nodeWidth * dims.distanceFactor, + y: lastPos.y + dims.nodeHeight * dims.distanceFactor * 2, + }, + { size: dims.circleSize, flipped: dims.flipped } + ); + flowData.nodes.push(elseCircle); + addEdge(flowData, nodeId, elseCircle.id, true); + const lastPoint = sequenceToFlow( + flowData, + node.default ?? [], + elseCircle.id, + { + ...dims, + padding: dims.flipped + ? { y: position.y, x: elseCircle.position.x - 1.4 * dims.circleSize, - } : { + } + : { x: position.x - dims.nodeWidth, y: elseCircle.position.y - dims.circleSize / 4, - }, - }, namer, updater.makeChildUpdaterForChooseAction(i, null), `${nodeId}.${totalChildren}.`) - if (lastPoint.position) { - lastPos = xyApply(lastPos, lastPoint.position, Math.max) - } else { - lastPos = xyApply(lastPos, elseCircle.position, Math.max) - } - return { lastPos, flowData } -} + }, + }, + namer, + updater.makeChildUpdaterForChooseAction(i, null), + `${nodeId}.${totalChildren}.` + ); + if (lastPoint.position) { + lastPos = xyApply(lastPos, lastPoint.position, Math.max); + } else { + lastPos = xyApply(lastPos, elseCircle.position, Math.max); + } + return { lastPos, flowData }; +}; export const mergeFlowDatas = (fd1: FlowData, fd2: FlowData): FlowData => { - fd1.edges = fd1.edges.concat(fd2.edges); - fd1.nodes = fd1.nodes.concat(fd2.nodes); - return fd1 -} \ No newline at end of file + fd1.edges = fd1.edges.concat(fd2.edges); + fd1.nodes = fd1.nodes.concat(fd2.nodes); + return fd1; +}; diff --git a/webapp/src/components/DAGFlow/triggerConvertor.ts b/webapp/src/components/DAGFlow/triggerConvertor.ts index c9d7ef9..3e66d91 100644 --- a/webapp/src/components/DAGFlow/triggerConvertor.ts +++ b/webapp/src/components/DAGFlow/triggerConvertor.ts @@ -1,65 +1,74 @@ -import { AutomationTrigger } from 'types/automations/triggers'; -import { DAGAutomationFlowDims, FlowData, TriggerMakerOptions } from './types'; -import { makeAddButton } from './flowDataMods'; -import { getDescriptionFromAutomationNode } from 'utils/formatting'; +import { AutomationTrigger } from "types/automations/triggers"; +import { DAGAutomationFlowDims, FlowData, TriggerMakerOptions } from "./types"; +import { makeAddButton } from "./flowDataMods"; +import { getDescriptionFromAutomationNode } from "utils/formatting"; export const triggerToFlow = ( - flowData: FlowData, - trigger: AutomationTrigger[], - toPoint: string, - dims: DAGAutomationFlowDims, - opts: TriggerMakerOptions, + flowData: FlowData, + trigger: AutomationTrigger[], + toPoint: string, + dims: DAGAutomationFlowDims, + opts: TriggerMakerOptions ) => { - const { - distanceFactor, - nodeHeight, - nodeWidth, - padding, - conditionHeight, - circleSize, + const { + distanceFactor, + nodeHeight, + nodeWidth, + padding, + conditionHeight, + circleSize, + flipped, + } = dims; + // convert all triggers to dag nodes + trigger.forEach((t, i) => { + const flowId = `t-${i}`; + flowData.nodes.push({ + id: flowId, + type: "dagnode", + data: { + label: getDescriptionFromAutomationNode(t, opts.namer, true), + height: nodeHeight, + width: nodeWidth, flipped, - } = dims; - // convert all triggers to dag nodes - trigger.forEach((t, i) => { - const flowId = `t-${i}`; - flowData.nodes.push({ - id: flowId, - type: 'dagnode', - data: { - label: getDescriptionFromAutomationNode(t, opts.namer), - height: nodeHeight, - width: nodeWidth, - flipped, - color: 'red', - onEditClick: () => opts.onEdit(i), - onXClick: () => opts.onDelete(i), - }, - position: flipped ? { - y: padding.y, - x: padding.x + nodeWidth * distanceFactor * i - } : { - x: padding.x, - y: padding.y + nodeHeight * distanceFactor * i - } - }) - flowData.edges.push({ - id: `e(${flowId})-(${toPoint})`, - source: flowId, - target: toPoint, - }) + color: "red", + onEditClick: () => opts.onEdit(i), + onXClick: () => opts.onDelete(i), + }, + position: flipped + ? { + y: padding.y, + x: padding.x + nodeWidth * distanceFactor * i, + } + : { + x: padding.x, + y: padding.y + nodeHeight * distanceFactor * i, + }, }); + flowData.edges.push({ + id: `e(${flowId})-(${toPoint})`, + source: flowId, + target: toPoint, + }); + }); - // create a 'add button' - flowData.nodes.push(makeAddButton( - 'trigger-add', - flipped ? { + // create a 'add button' + flowData.nodes.push( + makeAddButton( + "trigger-add", + flipped + ? { y: padding.y + circleSize / 4, x: padding.x + nodeWidth * distanceFactor * trigger.length, - } : { + } + : { x: padding.x + nodeWidth * 0.4, - y: padding.y + conditionHeight / 2 + nodeHeight * distanceFactor * trigger.length, - }, - dims, - opts.onAdd - )) -} \ No newline at end of file + y: + padding.y + + conditionHeight / 2 + + nodeHeight * distanceFactor * trigger.length, + }, + dims, + opts.onAdd + ) + ); +}; diff --git a/webapp/src/components/DAGFlow/updater.ts b/webapp/src/components/DAGFlow/updater.ts index 522ecad..fca341e 100644 --- a/webapp/src/components/DAGFlow/updater.ts +++ b/webapp/src/components/DAGFlow/updater.ts @@ -2,39 +2,32 @@ import { AutomationSequenceNode } from "types/automations"; import { ChooseAction } from "types/automations/actions"; import { AutomationCondition } from "types/automations/conditions"; import { UpdateModalState } from "./types"; -import { getNodeType } from '../../utils/automations'; +import { getNodeType } from "../../utils/automations"; export type SequenceUpdater = ReturnType; export const makeSequenceUpdater = ( sequence: AutomationSequenceNode[], onChange: (s: AutomationSequenceNode[]) => void, - openModal: UpdateModalState, + openModal: UpdateModalState ) => ({ sequence, onChange, removeNode(i: number) { - return onChange([ - ...sequence.slice(0, i), - ...sequence.slice(i + 1), - ]) + return onChange([...sequence.slice(0, i), ...sequence.slice(i + 1)]); }, addNode() { return openModal({ single: true, saveBtnCreateText: true, node: { - choose: [], - default: [], + device_id: "", }, - update: n => { - onChange([ - ...sequence, - n, - ]) + update: (n) => { + onChange([...sequence, n]); }, - allowedTypes: ['action', 'condition'] - }) + allowedTypes: ["action", "condition"], + }); }, updateNode(i: number, node: AutomationSequenceNode) { if (JSON.stringify(node) !== JSON.stringify(sequence[i])) { @@ -42,7 +35,7 @@ export const makeSequenceUpdater = ( ...sequence.slice(0, i), node, ...sequence.slice(i + 1), - ]) + ]); } }, updateChooseActionData(i: number, data: ChooseAction) { @@ -50,96 +43,90 @@ export const makeSequenceUpdater = ( return this.updateNode(i, { ...node, ...data, - }) + }); }, makeChildUpdaterForChooseAction(i: number, j: number | null) { const node = sequence[i]; - if ('choose' in node) { + if ("choose" in node) { if (j !== null) { return makeSequenceUpdater( node.choose[j].sequence, - seq => this.updateNode(i, { - ...node, - choose: [ - ...node.choose.slice(0, j), - { - ...node.choose[j], - sequence: seq, - }, - ...node.choose.slice(j + 1), - ] - }), - openModal, - ) + (seq) => + this.updateNode(i, { + ...node, + choose: [ + ...node.choose.slice(0, j), + { + ...node.choose[j], + sequence: seq, + }, + ...node.choose.slice(j + 1), + ], + }), + openModal + ); } else { return makeSequenceUpdater( node.default ?? [], - seq => this.updateNode(i, { - ...node, - default: seq - }), - openModal, - ) + (seq) => + this.updateNode(i, { + ...node, + default: seq, + }), + openModal + ); } } - throw new Error(`Cannot create sub-updater for ${node}`) + throw new Error(`Cannot create sub-updater for ${node}`); }, - makeOnModalOpenForNode( - i: number, - node: AutomationSequenceNode, - ) { - return () => openModal({ - single: true, - node, - allowedTypes: ['action', 'condition'], - update: n => this.updateNode(i, n), - }) + makeOnModalOpenForNode(i: number, node: AutomationSequenceNode) { + return () => + openModal({ + single: true, + node, + allowedTypes: ["action", "condition"], + update: (n) => this.updateNode(i, n), + }); }, - makeOnEditConditionsForChooseNode( - i: number, - j: number, - ) { + makeOnEditConditionsForChooseNode(i: number, j: number) { return () => { const node = sequence[i] as ChooseAction; return openModal({ single: false, node: node.choose[j].conditions, - allowedTypes: ['condition'], - update: conditions => this.updateNode(i, { - ...node, - choose: [ - ...node.choose.slice(0, j), - { - sequence: node.choose[j].sequence, - conditions: conditions as AutomationCondition[], - }, - ...node.choose.slice(j + 1), - ] - }), - }) - } + allowedTypes: ["condition"], + update: (conditions) => + this.updateNode(i, { + ...node, + choose: [ + ...node.choose.slice(0, j), + { + sequence: node.choose[j].sequence, + conditions: conditions as AutomationCondition[], + }, + ...node.choose.slice(j + 1), + ], + }), + }); + }; }, makeOnEditForBadNode(i: number) { const node = sequence[i]; - return () => openModal({ - single: true, - isError: true, - node, - allowedTypes: [getNodeType(node)], - update: n => this.updateNode(i, n), - }) - } + return () => + openModal({ + single: true, + isError: true, + node, + allowedTypes: [getNodeType(node)], + update: (n) => this.updateNode(i, n), + }); + }, }); - -export const createOnRemoveForChooseNode = ( - action: ChooseAction, - update: (data: ChooseAction) => void, - j: number -) => () => update({ - ...action, - choose: [ - ...action.choose.slice(0, j), - ...action.choose.slice(j + 1), - ] -}) +export const createOnRemoveForChooseNode = + (action: ChooseAction, update: (data: ChooseAction) => void, j: number) => + () => + update({ + ...action, + choose: [...action.choose.slice(0, j), ...action.choose.slice(j + 1)], + }); diff --git a/webapp/src/components/Modal/index.tsx b/webapp/src/components/Modal/index.tsx index 32081d9..11afe85 100644 --- a/webapp/src/components/Modal/index.tsx +++ b/webapp/src/components/Modal/index.tsx @@ -2,18 +2,22 @@ import "./index.css"; import Paper from "@mui/material/Paper"; import MuiModal from "@mui/material/Modal"; import { FC, ReactNode } from "react"; +import { Card, CardActions, CardContent } from "@mui/material"; export interface ModalStyleProps { open: boolean; } export interface Props extends ModalStyleProps { children: ReactNode; + actions?: ReactNode; } -export const Modal: FC = ({ children, ...props }) => { +export const Modal: FC = ({ children, actions, open }) => { return ( - +
- {children} + + {children} +
); diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx index cde40d6..3a0ad82 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx @@ -115,7 +115,11 @@ export const useEditorNodeState = ( setState({ ...allState.node, diff --git a/webapp/src/utils/formatting.test.ts b/webapp/src/utils/formatting.test.ts index 536dee5..575ed31 100644 --- a/webapp/src/utils/formatting.test.ts +++ b/webapp/src/utils/formatting.test.ts @@ -45,7 +45,8 @@ test("using alias as description", () => { media_content_type: "SPOTIFY", }, }, - dummyNamer + dummyNamer, + false ) ).toEqual("Start Music In Kitchen"); }); @@ -63,7 +64,8 @@ test("get service action description", () => { media_content_type: "SPOTIFY", }, }, - dummyNamer + dummyNamer, + false ) ).toEqual("media_player.play_media"); }); @@ -77,7 +79,8 @@ test("get repeat action description", () => { sequence: [], }, }, - dummyNamer + dummyNamer, + false ) ).toEqual("Repeat 10"); }); @@ -88,7 +91,8 @@ test("get wait action description", () => { { wait_template: "states(switch.kitchen_light) == 'on'", }, - dummyNamer + dummyNamer, + false ) ).toEqual("Wait on states(switch.kitchen_light) == 'on'"); expect( @@ -99,7 +103,8 @@ test("get wait action description", () => { minutes: 1, }, }, - dummyNamer + dummyNamer, + false ) ).toEqual("Wait on states(switch.kitchen_light) == 'on' for 00:01:00:00"); }); @@ -111,7 +116,8 @@ test("get fire event action description", () => { event: "test_event", event_data: {}, }, - dummyNamer + dummyNamer, + false ) ).toEqual("Trigger test_event"); }); @@ -124,7 +130,8 @@ test("get device action description", () => { domain: "zwave_js", type: "set_value", }, - dummyNamer + dummyNamer, + false ) ).toEqual("Set Value 12310das01231"); }); @@ -136,7 +143,8 @@ test("get choose action description", () => { choose: [], default: [], }, - dummyNamer + dummyNamer, + false ) ).toEqual("Choose"); }); @@ -148,7 +156,8 @@ test("get and/or/not condition description", () => { condition: "and", conditions: [], }, - dummyNamer + dummyNamer, + false ) ).toEqual("Logic"); expect( @@ -157,7 +166,8 @@ test("get and/or/not condition description", () => { condition: "or", conditions: [], }, - dummyNamer + dummyNamer, + false ) ).toEqual("Logic"); expect( @@ -166,7 +176,8 @@ test("get and/or/not condition description", () => { condition: "not", conditions: [], }, - dummyNamer + dummyNamer, + false ) ).toEqual("Logic"); }); @@ -178,7 +189,8 @@ test("get Numeric State condition description", () => { condition: "numeric_state", entity_id: "sensor.temperature", }, - dummyNamer + dummyNamer, + false ) ).toEqual("sensor.temperature?>?<"); expect( @@ -187,7 +199,8 @@ test("get Numeric State condition description", () => { condition: "numeric_state", entity_id: "", }, - dummyNamer + dummyNamer, + false ) ).toEqual("Numeric State Condition"); expect( @@ -197,7 +210,8 @@ test("get Numeric State condition description", () => { entity_id: "sensor.temperature", above: "17", }, - dummyNamer + dummyNamer, + false ) ).toEqual("sensor.temperature > 17"); expect( @@ -207,7 +221,8 @@ test("get Numeric State condition description", () => { entity_id: "sensor.temperature", below: "5", }, - dummyNamer + dummyNamer, + false ) ).toEqual("sensor.temperature < 5"); expect( @@ -218,7 +233,8 @@ test("get Numeric State condition description", () => { above: "17", below: "20", }, - dummyNamer + dummyNamer, + false ) ).toEqual("17 < sensor.temperature < 20"); expect( @@ -230,7 +246,8 @@ test("get Numeric State condition description", () => { below: "20", value_template: "{{ float(state.state) + 2 }}", }, - dummyNamer + dummyNamer, + false ) ).toEqual("17 < sensor.temperature < 20"); expect( @@ -245,7 +262,8 @@ test("get Numeric State condition description", () => { below: "20", value_template: "{{ float(state.state) + 2 }}", }, - dummyNamer + dummyNamer, + false ) ).toEqual("17 < ... < 20"); }); @@ -258,7 +276,8 @@ test("get State condition description", () => { entity_id: "switch.kitchen_light", state: "on", }, - dummyNamer + dummyNamer, + false ) ).toEqual("switch.kitchen_light is 'on'"); }); @@ -280,7 +299,8 @@ test("get descrption for device triggerr", () => { entity_id: "binary_sensor.motion_office_inv", domain: "binary_sensor", }, - dummyNamer + dummyNamer, + false ) ).toEqual("binary_sensor.motion_office_inv No Motion"); }); diff --git a/webapp/src/utils/formatting.ts b/webapp/src/utils/formatting.tsx similarity index 89% rename from webapp/src/utils/formatting.ts rename to webapp/src/utils/formatting.tsx index 89fb0d3..6a5a320 100644 --- a/webapp/src/utils/formatting.ts +++ b/webapp/src/utils/formatting.tsx @@ -1,4 +1,6 @@ import { AutomationNode, AutomationNodeTypes } from "types/automations"; +import AltRouteOutlinedIcon from "@mui/icons-material/AltRouteOutlined"; +import { ReactNode } from "react"; import { AutomationDeviceState, AutomationTime, @@ -20,10 +22,14 @@ export const convertTimeToString = (t: AutomationTime) => { .join(":"); } }; -export const getDescriptionFromAutomationNode = ( +export const getDescriptionFromAutomationNode = < + N extends AutomationNodeTypes, + AllowJS extends true | false +>( node: AutomationNode, - namer: Namer -): string => { + namer: Namer, + allowJs: AllowJS +): AllowJS extends true ? ReactNode : string => { if ("alias" in node && node.alias) { return node.alias; } @@ -128,7 +134,17 @@ export const getDescriptionFromAutomationNode = ( return getDescriptionForDeviceType(node as any, namer, false); } if ("choose" in node) { - return "Choose"; + if (allowJs) { + return ( + + ) as any; + } + return "Switch (choose)"; } return "n/a"; };