updated choose name
This commit is contained in:
1 parent
218972ed5d
commit
21dd880e37
8 files changed
+584
-468
No files matched your search
@@ -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<C extends AutomationCondition> extends FC<{
|
||||
condition: C;
|
||||
namer: Namer;
|
||||
onChange: (condition: C) => void;
|
||||
}> { }
|
||||
interface Viewer<C extends AutomationCondition>
|
||||
extends FC<{
|
||||
condition: C;
|
||||
namer: Namer;
|
||||
onChange: (condition: C) => void;
|
||||
}> {}
|
||||
|
||||
export const getViewer = (condition: AutomationCondition): Viewer<any> => {
|
||||
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<AutomationCondition> = ({
|
||||
condition,
|
||||
namer,
|
||||
}) => {
|
||||
return <>
|
||||
{getDescriptionFromAutomationNode(condition, namer)}
|
||||
</>
|
||||
}
|
||||
return <>{getDescriptionFromAutomationNode(condition, namer, true)}</>;
|
||||
};
|
||||
|
||||
export const LogicViewer: Viewer<LogicCondition> = ({
|
||||
condition,
|
||||
onChange,
|
||||
}) => {
|
||||
return <>
|
||||
{condition.conditions.map((c, i) => {
|
||||
return <ConditionNode key={i} condition={c} displayMode
|
||||
{...genUpdateMethods(condition, onChange)(i)}
|
||||
/>
|
||||
})}
|
||||
</>
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{condition.conditions.map((c, i) => {
|
||||
return (
|
||||
<ConditionNode
|
||||
key={i}
|
||||
condition={c}
|
||||
displayMode
|
||||
{...genUpdateMethods(condition, onChange)(i)}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
};
|
||||
@@ -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
|
||||
}
|
||||
fd1.edges = fd1.edges.concat(fd2.edges);
|
||||
fd1.nodes = fd1.nodes.concat(fd2.nodes);
|
||||
return fd1;
|
||||
};
|
||||
@@ -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
|
||||
))
|
||||
}
|
||||
y:
|
||||
padding.y +
|
||||
conditionHeight / 2 +
|
||||
nodeHeight * distanceFactor * trigger.length,
|
||||
},
|
||||
dims,
|
||||
opts.onAdd
|
||||
)
|
||||
);
|
||||
};
|
||||
@@ -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<typeof makeSequenceUpdater>;
|
||||
|
||||
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)],
|
||||
});
|
||||
@@ -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<Props> = ({ children, ...props }) => {
|
||||
export const Modal: FC<Props> = ({ children, actions, open }) => {
|
||||
return (
|
||||
<MuiModal open={props.open}>
|
||||
<MuiModal open={open}>
|
||||
<div className="modal--root">
|
||||
<Paper>{children}</Paper>
|
||||
<Card variant="outlined">
|
||||
<CardContent>{children}</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</MuiModal>
|
||||
);
|
||||
|
||||
@@ -115,7 +115,11 @@ export const useEditorNodeState = (
|
||||
<InputText
|
||||
key="alias"
|
||||
label="Alias"
|
||||
value={getDescriptionFromAutomationNode(allState.node, ha.namer)}
|
||||
value={getDescriptionFromAutomationNode(
|
||||
allState.node,
|
||||
ha.namer,
|
||||
false
|
||||
)}
|
||||
onChange={(alias) =>
|
||||
setState({
|
||||
...allState.node,
|
||||
|
||||
Reference in new issue
Block a user