condition + repeat node
This commit is contained in:
1 parent
89c7007612
commit
07ab4b9089
22 files changed
+631
-282
No files matched your search
@@ -4,7 +4,7 @@ export const NODE_WIDTH = 200 as const;
|
|||||||
export const NODE_HEIGHT = 80 as const;
|
export const NODE_HEIGHT = 80 as const;
|
||||||
|
|
||||||
export const DEFAULT_DIMS: DAGDims = {
|
export const DEFAULT_DIMS: DAGDims = {
|
||||||
trigger: {
|
collection: {
|
||||||
height: NODE_HEIGHT * 3,
|
height: NODE_HEIGHT * 3,
|
||||||
width: NODE_WIDTH * 1.5,
|
width: NODE_WIDTH * 1.5,
|
||||||
},
|
},
|
||||||
@@ -12,10 +12,6 @@ export const DEFAULT_DIMS: DAGDims = {
|
|||||||
height: NODE_HEIGHT,
|
height: NODE_HEIGHT,
|
||||||
width: NODE_WIDTH,
|
width: NODE_WIDTH,
|
||||||
},
|
},
|
||||||
condition: {
|
|
||||||
height: NODE_HEIGHT * 3,
|
|
||||||
width: NODE_WIDTH * 1.5,
|
|
||||||
},
|
|
||||||
distanceFactor: {
|
distanceFactor: {
|
||||||
node: 1.5,
|
node: 1.5,
|
||||||
collection: 1,
|
collection: 1,
|
||||||
|
|||||||
@@ -26,19 +26,15 @@ export const moveAlongRelativeTo = (
|
|||||||
offsetPos: XYPosition,
|
offsetPos: XYPosition,
|
||||||
relatedPos: XYPosition,
|
relatedPos: XYPosition,
|
||||||
dims: DAGDims,
|
dims: DAGDims,
|
||||||
incrementBy: keyof DAGEntities = "condition"
|
incrementBy: keyof DAGEntities = "collection"
|
||||||
) =>
|
) =>
|
||||||
dims.flipped
|
dims.flipped
|
||||||
? {
|
? {
|
||||||
x:
|
x: relatedPos.x + dims.distanceFactor.node * dims[incrementBy].width,
|
||||||
relatedPos.x +
|
|
||||||
(dims.distanceFactor.node - 1) * dims[incrementBy].width,
|
|
||||||
y: offsetPos.y,
|
y: offsetPos.y,
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
y:
|
y: relatedPos.y + dims.distanceFactor.node * dims[incrementBy].height,
|
||||||
relatedPos.y +
|
|
||||||
(dims.distanceFactor.node - 1) * dims[incrementBy].height,
|
|
||||||
x: offsetPos.x,
|
x: offsetPos.x,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -60,3 +56,12 @@ export const moveFromTo = (
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const offsetBy = (
|
||||||
|
by: keyof DAGEntities,
|
||||||
|
position: XYPosition,
|
||||||
|
dims: DAGDims
|
||||||
|
): XYPosition => ({
|
||||||
|
x: position.x + dims.distanceFactor[by] * dims[by].width,
|
||||||
|
y: position.y + dims.distanceFactor[by] * dims[by].height,
|
||||||
|
});
|
||||||
@@ -358,6 +358,10 @@ ChooseAuto.args = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
choose: [],
|
||||||
|
default: [],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
service: "switch.turn_on",
|
service: "switch.turn_on",
|
||||||
target: {
|
target: {
|
||||||
@@ -367,3 +371,60 @@ ChooseAuto.args = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const EmptyChooseAuto = Template.bind({});
|
||||||
|
|
||||||
|
EmptyChooseAuto.args = {
|
||||||
|
...EmptyChooseAuto.args,
|
||||||
|
automation: {
|
||||||
|
condition: [],
|
||||||
|
trigger: [],
|
||||||
|
sequence: [
|
||||||
|
{
|
||||||
|
choose: [],
|
||||||
|
default: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const RepeatAuto = Template.bind({});
|
||||||
|
RepeatAuto.args = {
|
||||||
|
...RepeatAuto.args,
|
||||||
|
automation: {
|
||||||
|
condition: [],
|
||||||
|
trigger: [],
|
||||||
|
sequence: [
|
||||||
|
{
|
||||||
|
repeat: {
|
||||||
|
while: 'states(lights.switch) == "on"',
|
||||||
|
sequence: [
|
||||||
|
{
|
||||||
|
delay: {
|
||||||
|
minutes: 5,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
repeat: {
|
||||||
|
count: 2,
|
||||||
|
until: [
|
||||||
|
{
|
||||||
|
condition: "state",
|
||||||
|
entity_id: "lights.bathroom",
|
||||||
|
state: "off",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
sequence: [
|
||||||
|
{
|
||||||
|
entity_id: "button.ping_mainboard",
|
||||||
|
service: "button.pres",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -12,11 +12,17 @@ import { ChooseAction, RepeatAction } from "types/automations/actions";
|
|||||||
import { XYPosition } from "react-flow-renderer";
|
import { XYPosition } from "react-flow-renderer";
|
||||||
import { DAGElementsOutputState } from "./outputState";
|
import { DAGElementsOutputState } from "./outputState";
|
||||||
import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
||||||
import { getNodeType } from "utils/automations";
|
import {
|
||||||
|
getNodeType,
|
||||||
|
convertScriptConditionFieldToAutomationConditions,
|
||||||
|
} from "utils/automations";
|
||||||
import { ButtonNodeMaker } from "../nodes/ButtonNode";
|
import { ButtonNodeMaker } from "../nodes/ButtonNode";
|
||||||
import { AddIcon } from "components/Icons";
|
import { AddIcon } from "components/Icons";
|
||||||
import { useDAGElementsState } from "./state";
|
import { useDAGElementsState } from "./state";
|
||||||
import AddBox from "@mui/icons-material/AddBox";
|
import AddBox from "@mui/icons-material/AddBox";
|
||||||
|
import { DAGGraphChooseUpdater, DAGGraphUpdater } from "../updater";
|
||||||
|
import { ReactNode } from "react";
|
||||||
|
import DeleteForever from "@mui/icons-material/DeleteForever";
|
||||||
|
|
||||||
export const useAutomationNodes = (
|
export const useAutomationNodes = (
|
||||||
automation: AutomationActionData,
|
automation: AutomationActionData,
|
||||||
@@ -44,8 +50,8 @@ export const useAutomationNodes = (
|
|||||||
state,
|
state,
|
||||||
dims,
|
dims,
|
||||||
position: distance.moveFromTo(
|
position: distance.moveFromTo(
|
||||||
"trigger",
|
"collection",
|
||||||
"condition",
|
"collection",
|
||||||
outputState.getLastNodePos(),
|
outputState.getLastNodePos(),
|
||||||
dims
|
dims
|
||||||
),
|
),
|
||||||
@@ -61,7 +67,7 @@ export const useAutomationNodes = (
|
|||||||
state,
|
state,
|
||||||
dims,
|
dims,
|
||||||
position: distance.moveFromTo(
|
position: distance.moveFromTo(
|
||||||
"condition",
|
"collection",
|
||||||
"node",
|
"node",
|
||||||
outputState.getLastNodePos(),
|
outputState.getLastNodePos(),
|
||||||
dims
|
dims
|
||||||
@@ -82,7 +88,7 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
|||||||
const outputState = new DAGElementsOutputState(position, dims);
|
const outputState = new DAGElementsOutputState(position, dims);
|
||||||
outputState.addNode(
|
outputState.addNode(
|
||||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||||
...dims.trigger,
|
...dims.collection,
|
||||||
collectionType: "trigger",
|
collectionType: "trigger",
|
||||||
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
|
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
|
||||||
nodes: nodes.map((node, index) => ({
|
nodes: nodes.map((node, index) => ({
|
||||||
@@ -102,7 +108,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
|||||||
const outputState = new DAGElementsOutputState(position, dims);
|
const outputState = new DAGElementsOutputState(position, dims);
|
||||||
outputState.addNode(
|
outputState.addNode(
|
||||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||||
...dims.condition,
|
...dims.collection,
|
||||||
collectionType: "condition",
|
collectionType: "condition",
|
||||||
hasInput: !!lastNodeId,
|
hasInput: !!lastNodeId,
|
||||||
onAddNode: () => stateUpdater.basic.condition.addNode(null),
|
onAddNode: () => stateUpdater.basic.condition.addNode(null),
|
||||||
@@ -214,108 +220,187 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
|||||||
}
|
}
|
||||||
const node = nodes[0];
|
const node = nodes[0];
|
||||||
if (
|
if (
|
||||||
(node.enabled ?? true) &&
|
!((node.enabled ?? true) && !args.state.get(args.lastNodeId ?? "").isClosed)
|
||||||
!args.state.get(args.lastNodeId ?? "").isClosed
|
|
||||||
) {
|
) {
|
||||||
// add new
|
return outputState;
|
||||||
outputState.addNode(
|
|
||||||
ButtonNodeMaker.makeElement(
|
|
||||||
{
|
|
||||||
id: `${args.nodeId}-choose-add-new`,
|
|
||||||
position: distance.moveAlong(
|
|
||||||
"node",
|
|
||||||
outputState.nextPos,
|
|
||||||
0.3,
|
|
||||||
args.dims,
|
|
||||||
true
|
|
||||||
),
|
|
||||||
},
|
|
||||||
args.dims,
|
|
||||||
{
|
|
||||||
icon: <AddBox />,
|
|
||||||
onClick: () =>
|
|
||||||
args.stateUpdater.basic.sequence.updateNode(
|
|
||||||
{
|
|
||||||
...node,
|
|
||||||
choose: [
|
|
||||||
...node.choose,
|
|
||||||
{
|
|
||||||
conditions: [],
|
|
||||||
sequence: [],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
args.nodeIndex
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
// offset position
|
|
||||||
let offsetPos: XYPosition = {
|
|
||||||
x: args.position.x + args.dims.distanceFactor.node * args.dims.node.width,
|
|
||||||
y:
|
|
||||||
args.position.y + args.dims.distanceFactor.node * args.dims.node.height,
|
|
||||||
};
|
|
||||||
// functions
|
|
||||||
const processSequence = (j: number | "else") => {
|
|
||||||
const stateUpdater = args.stateUpdater.createChoosNodeUpdater(
|
|
||||||
args.nodeIndex,
|
|
||||||
j
|
|
||||||
);
|
|
||||||
let pos = offsetPos;
|
|
||||||
let lastNodeId = args.lastNodeId;
|
|
||||||
if (j !== "else") {
|
|
||||||
const conditionState = makeConditionNodes(node.choose[j].conditions, {
|
|
||||||
...args,
|
|
||||||
stateUpdater,
|
|
||||||
position: pos,
|
|
||||||
lastNodeId,
|
|
||||||
nodeIndex: j,
|
|
||||||
nodeId: `${args.nodeId}-choose-${j}-condition`,
|
|
||||||
});
|
|
||||||
conditionState.data.edges[0].label = `Condition ${j + 1}`;
|
|
||||||
conditionState.data.edges[0].style = {
|
|
||||||
stroke: "var(--mui-info-main)",
|
|
||||||
};
|
|
||||||
outputState.extend(conditionState);
|
|
||||||
pos = distance.moveFromTo("condition", "node", pos, args.dims);
|
|
||||||
lastNodeId = conditionState.lastNodeId;
|
|
||||||
}
|
|
||||||
const sequence =
|
|
||||||
j === "else" ? node.default ?? [] : node.choose[j].sequence;
|
|
||||||
const output = makeSequenceNodes(sequence, {
|
|
||||||
...args,
|
|
||||||
stateUpdater,
|
|
||||||
position: pos,
|
|
||||||
lastNodeId: undefined,
|
|
||||||
nodeIndex: j === "else" ? node.choose.length : j,
|
|
||||||
nodeId: `${args.nodeId}-choose-${j}-sequence`,
|
|
||||||
});
|
|
||||||
if (lastNodeId && output.data.nodes.length > 0) {
|
|
||||||
outputState.addEdge(lastNodeId, output.data.nodes[0].id, {
|
|
||||||
animated: true,
|
|
||||||
label: j === "else" ? "else" : undefined,
|
|
||||||
style: {
|
|
||||||
stroke: "var(--mui-error-main)",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
offsetPos = distance.moveAlongRelativeTo(
|
|
||||||
offsetPos,
|
|
||||||
output.bbox[1],
|
|
||||||
args.dims,
|
|
||||||
j === "else" ? "node" : "condition"
|
|
||||||
);
|
|
||||||
outputState.extend(output);
|
|
||||||
};
|
|
||||||
|
|
||||||
// process options
|
|
||||||
for (let sIndex = 0; sIndex < node.choose.length; sIndex++) {
|
|
||||||
processSequence(sIndex);
|
|
||||||
}
|
|
||||||
// else
|
|
||||||
processSequence("else");
|
|
||||||
}
|
}
|
||||||
|
// offset position
|
||||||
|
let offsetPos: XYPosition = distance.offsetBy(
|
||||||
|
"node",
|
||||||
|
args.position,
|
||||||
|
args.dims
|
||||||
|
);
|
||||||
|
let lastConditionState: DAGElementsOutputState | null = null;
|
||||||
|
// functions
|
||||||
|
const drawCondition = (
|
||||||
|
{ stateUpdater, onRemove }: DAGGraphChooseUpdater,
|
||||||
|
position: XYPosition,
|
||||||
|
lastNodeId: string | undefined,
|
||||||
|
nodeIndex: number,
|
||||||
|
conditions: AutomationCondition[]
|
||||||
|
) => {
|
||||||
|
const conditionState = makeConditionNodes(conditions, {
|
||||||
|
...args,
|
||||||
|
stateUpdater,
|
||||||
|
position,
|
||||||
|
lastNodeId,
|
||||||
|
nodeIndex,
|
||||||
|
nodeId: `${args.nodeId}-choose-${nodeIndex}-condition`,
|
||||||
|
});
|
||||||
|
lastConditionState = conditionState;
|
||||||
|
(conditionState.data.nodes[0].data as any).title = `Option ${
|
||||||
|
nodeIndex + 1
|
||||||
|
}`;
|
||||||
|
|
||||||
|
(conditionState.data.nodes[0].data as any).onDelete = onRemove;
|
||||||
|
conditionState.data.edges[0].sourceHandle = `side`;
|
||||||
|
conditionState.data.edges[0].label = `Option ${nodeIndex + 1}`;
|
||||||
|
conditionState.data.edges[0].style = {
|
||||||
|
stroke: "var(--mui-info-main)",
|
||||||
|
};
|
||||||
|
outputState.extend(conditionState);
|
||||||
|
return conditionState;
|
||||||
|
};
|
||||||
|
const drawSequence = (
|
||||||
|
{ stateUpdater }: DAGGraphChooseUpdater,
|
||||||
|
position: XYPosition,
|
||||||
|
lastNodeId: string | undefined,
|
||||||
|
nodeIndex: number,
|
||||||
|
sequence: AutomationSequenceNode[],
|
||||||
|
label: "else" | undefined
|
||||||
|
) => {
|
||||||
|
const output = makeSequenceNodes(sequence, {
|
||||||
|
...args,
|
||||||
|
stateUpdater,
|
||||||
|
position,
|
||||||
|
lastNodeId: undefined,
|
||||||
|
nodeIndex,
|
||||||
|
nodeId: `${args.nodeId}-choose-${nodeIndex}-sequence`,
|
||||||
|
});
|
||||||
|
if (lastNodeId && output.data.nodes.length > 0) {
|
||||||
|
outputState.addEdge(lastNodeId, output.data.nodes[0].id, {
|
||||||
|
animated: true,
|
||||||
|
label,
|
||||||
|
sourceHandle: label === "else" ? "side" : undefined,
|
||||||
|
style: {
|
||||||
|
stroke: "var(--mui-error-main)",
|
||||||
|
zIndex: -1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
// process options
|
||||||
|
for (let sIndex = 0; sIndex < node.choose.length; sIndex++) {
|
||||||
|
const stateUpdater = args.stateUpdater.createChoosNodeUpdater(
|
||||||
|
args.nodeIndex,
|
||||||
|
sIndex
|
||||||
|
);
|
||||||
|
let pos = offsetPos;
|
||||||
|
let lastNodeId = args.lastNodeId;
|
||||||
|
const conditionState = drawCondition(
|
||||||
|
stateUpdater,
|
||||||
|
pos,
|
||||||
|
lastNodeId,
|
||||||
|
sIndex,
|
||||||
|
node.choose[sIndex].conditions
|
||||||
|
);
|
||||||
|
pos = distance.moveFromTo("collection", "node", pos, args.dims);
|
||||||
|
lastConditionState = conditionState;
|
||||||
|
lastNodeId = conditionState.lastNodeId;
|
||||||
|
const sequence = node.choose[sIndex].sequence;
|
||||||
|
const output = drawSequence(
|
||||||
|
stateUpdater,
|
||||||
|
pos,
|
||||||
|
lastNodeId,
|
||||||
|
sIndex,
|
||||||
|
sequence,
|
||||||
|
undefined
|
||||||
|
);
|
||||||
|
offsetPos = distance.moveAlongRelativeTo(
|
||||||
|
offsetPos,
|
||||||
|
output.bbox[1],
|
||||||
|
args.dims,
|
||||||
|
"collection"
|
||||||
|
);
|
||||||
|
outputState.extend(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
// else
|
||||||
|
const stateUpdater = args.stateUpdater.createChoosNodeUpdater(
|
||||||
|
args.nodeIndex,
|
||||||
|
"else"
|
||||||
|
);
|
||||||
|
let pos = offsetPos;
|
||||||
|
let lastNodeId = args.lastNodeId;
|
||||||
|
const elseOutput = drawSequence(
|
||||||
|
stateUpdater,
|
||||||
|
pos,
|
||||||
|
lastNodeId,
|
||||||
|
node.choose.length,
|
||||||
|
node.default ?? [],
|
||||||
|
"else"
|
||||||
|
);
|
||||||
|
offsetPos = distance.moveAlongRelativeTo(
|
||||||
|
offsetPos,
|
||||||
|
elseOutput.bbox[1],
|
||||||
|
args.dims,
|
||||||
|
"node"
|
||||||
|
);
|
||||||
|
outputState.extend(elseOutput);
|
||||||
|
|
||||||
|
// add btn
|
||||||
|
let addPos = distance.moveAlong(
|
||||||
|
"node",
|
||||||
|
elseOutput.data.nodes[0].position,
|
||||||
|
1 / args.dims.distanceFactor.node,
|
||||||
|
args.dims,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
console.log({ addPos, lastConditionState });
|
||||||
|
if (lastConditionState !== null) {
|
||||||
|
addPos = distance.moveFromTo(
|
||||||
|
"collection",
|
||||||
|
"node",
|
||||||
|
(lastConditionState as DAGElementsOutputState).nextPos,
|
||||||
|
{
|
||||||
|
...args.dims,
|
||||||
|
flipped: !args.dims.flipped,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const addElement = outputState.addNode(
|
||||||
|
ButtonNodeMaker.makeElement(
|
||||||
|
{
|
||||||
|
id: `${args.nodeId}-choose-add-new`,
|
||||||
|
position: addPos,
|
||||||
|
},
|
||||||
|
args.dims,
|
||||||
|
{
|
||||||
|
text: "Option",
|
||||||
|
icon: <AddBox />,
|
||||||
|
onClick: () =>
|
||||||
|
args.stateUpdater.basic.sequence.updateNode(
|
||||||
|
{
|
||||||
|
...node,
|
||||||
|
choose: [
|
||||||
|
...node.choose,
|
||||||
|
{
|
||||||
|
conditions: [],
|
||||||
|
sequence: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
args.nodeIndex
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
outputState.addEdge(args.nodeId, addElement.id, {
|
||||||
|
animated: true,
|
||||||
|
sourceHandle: "side",
|
||||||
|
});
|
||||||
|
|
||||||
return outputState;
|
return outputState;
|
||||||
};
|
};
|
||||||
@@ -331,32 +416,38 @@ export const makeRepeatNodes: ElementMaker<RepeatAction> = (nodes, args) => {
|
|||||||
!args.state.get(args.lastNodeId ?? "").isClosed
|
!args.state.get(args.lastNodeId ?? "").isClosed
|
||||||
) {
|
) {
|
||||||
// offset position
|
// offset position
|
||||||
let offsetPos: XYPosition = {
|
let offsetPos: XYPosition = distance.offsetBy(
|
||||||
x: args.position.x + args.dims.distanceFactor.node * args.dims.node.width,
|
"node",
|
||||||
y:
|
args.position,
|
||||||
args.position.y + args.dims.distanceFactor.node * args.dims.node.height,
|
args.dims
|
||||||
};
|
);
|
||||||
|
let lastNodeId = args.lastNodeId;
|
||||||
const stateUpdater = args.stateUpdater.createRepeatNodeUpdater(
|
const stateUpdater = args.stateUpdater.createRepeatNodeUpdater(
|
||||||
args.nodeIndex
|
args.nodeIndex
|
||||||
);
|
);
|
||||||
let pos = offsetPos;
|
|
||||||
let lastNodeId = args.lastNodeId;
|
// while
|
||||||
const conditionState = makeConditionNodes(node.repeat.while, {
|
const whileState = makeConditionNodes(
|
||||||
...args,
|
convertScriptConditionFieldToAutomationConditions(node.repeat.while),
|
||||||
stateUpdater,
|
{
|
||||||
position: pos,
|
...args,
|
||||||
lastNodeId,
|
stateUpdater: stateUpdater.while,
|
||||||
nodeIndex: 0,
|
position: offsetPos,
|
||||||
nodeId: `${args.nodeId}-repeat-while`,
|
lastNodeId,
|
||||||
});
|
nodeIndex: 0,
|
||||||
conditionState.data.edges[0].label = `While`;
|
nodeId: `${args.nodeId}-repeat-while`,
|
||||||
outputState.extend(conditionState);
|
}
|
||||||
pos = distance.moveFromTo("condition", "node", pos, args.dims);
|
);
|
||||||
lastNodeId = conditionState.lastNodeId;
|
whileState.data.edges[0].sourceHandle = `side`;
|
||||||
|
whileState.data.edges[0].label = `While`;
|
||||||
|
outputState.extend(whileState);
|
||||||
|
offsetPos = distance.moveFromTo("collection", "node", offsetPos, args.dims);
|
||||||
|
lastNodeId = whileState.lastNodeId;
|
||||||
|
// sequence
|
||||||
const sequenceState = makeSequenceNodes(node.repeat.sequence, {
|
const sequenceState = makeSequenceNodes(node.repeat.sequence, {
|
||||||
...args,
|
...args,
|
||||||
stateUpdater,
|
stateUpdater: stateUpdater.sequence,
|
||||||
position: pos,
|
position: offsetPos,
|
||||||
lastNodeId: undefined,
|
lastNodeId: undefined,
|
||||||
nodeIndex: 0,
|
nodeIndex: 0,
|
||||||
nodeId: `${args.nodeId}-repeat-sequence`,
|
nodeId: `${args.nodeId}-repeat-sequence`,
|
||||||
@@ -367,16 +458,36 @@ export const makeRepeatNodes: ElementMaker<RepeatAction> = (nodes, args) => {
|
|||||||
label: "do",
|
label: "do",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
lastNodeId = sequenceState.lastNodeId;
|
||||||
offsetPos = distance.moveAlongRelativeTo(
|
offsetPos = distance.moveAlongRelativeTo(
|
||||||
offsetPos,
|
offsetPos,
|
||||||
sequenceState.bbox[1],
|
sequenceState.bbox[1],
|
||||||
args.dims,
|
args.dims,
|
||||||
"condition"
|
"node"
|
||||||
);
|
);
|
||||||
if (sequenceState.lastNodeId && conditionState.lastNodeId) {
|
// until
|
||||||
outputState.addEdge(sequenceState.lastNodeId, conditionState.lastNodeId, {
|
const untilState = makeConditionNodes(
|
||||||
|
convertScriptConditionFieldToAutomationConditions(node.repeat.until),
|
||||||
|
{
|
||||||
|
...args,
|
||||||
|
stateUpdater: stateUpdater.until,
|
||||||
|
position: offsetPos,
|
||||||
|
lastNodeId,
|
||||||
|
nodeIndex: 0,
|
||||||
|
nodeId: `${args.nodeId}-repeat-until`,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
untilState.data.edges[0].label = `Until`;
|
||||||
|
untilState.data.edges[0].targetHandle = `side`;
|
||||||
|
outputState.extend(untilState);
|
||||||
|
offsetPos = distance.moveFromTo("collection", "node", offsetPos, args.dims);
|
||||||
|
lastNodeId = untilState.lastNodeId;
|
||||||
|
// loop
|
||||||
|
if (untilState.lastNodeId && whileState.lastNodeId) {
|
||||||
|
outputState.addEdge(untilState.lastNodeId, whileState.lastNodeId, {
|
||||||
animated: true,
|
animated: true,
|
||||||
label: "repeat",
|
label: "repeat",
|
||||||
|
sourceHandle: "head",
|
||||||
targetHandle: "return",
|
targetHandle: "return",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,15 +16,11 @@ export type ElementData = {
|
|||||||
export type DAGEntityDim = Size;
|
export type DAGEntityDim = Size;
|
||||||
export type DAGEntities = {
|
export type DAGEntities = {
|
||||||
node: DAGEntityDim;
|
node: DAGEntityDim;
|
||||||
condition: DAGEntityDim;
|
collection: DAGEntityDim;
|
||||||
trigger: DAGEntityDim;
|
|
||||||
};
|
};
|
||||||
export type DAGDims = DAGEntities & {
|
export type DAGDims = DAGEntities & {
|
||||||
position: XYPosition;
|
position: XYPosition;
|
||||||
distanceFactor: {
|
distanceFactor: Record<keyof DAGEntities, number>;
|
||||||
node: number;
|
|
||||||
collection: number;
|
|
||||||
};
|
|
||||||
flipped: boolean;
|
flipped: boolean;
|
||||||
};
|
};
|
||||||
export type ElementMakerBaseProps = {
|
export type ElementMakerBaseProps = {
|
||||||
|
|||||||
@@ -85,3 +85,8 @@ const Template: ComponentStory<typeof ButtonNode> = (args) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const Basic = Template.bind({});
|
export const Basic = Template.bind({});
|
||||||
|
export const Text = Template.bind({});
|
||||||
|
Text.args = {
|
||||||
|
...Text.args,
|
||||||
|
text: "Option",
|
||||||
|
};
|
||||||
@@ -11,4 +11,9 @@
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
/* outline: 1px dashed white; */
|
/* outline: 1px dashed white; */
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-node button {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.25em;
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import { ButtonIcon } from "components/Icons/ButtonIcons";
|
|||||||
import { FC } from "react";
|
import { FC } from "react";
|
||||||
import { ButtonNodeProps } from "./types";
|
import { ButtonNodeProps } from "./types";
|
||||||
import { Handle, Position } from "react-flow-renderer";
|
import { Handle, Position } from "react-flow-renderer";
|
||||||
|
import { Button } from "components/Inputs/Button";
|
||||||
|
|
||||||
export const ButtonNode: FC<ButtonNodeProps> = ({
|
export const ButtonNode: FC<ButtonNodeProps> = ({
|
||||||
height,
|
height,
|
||||||
@@ -11,7 +12,18 @@ export const ButtonNode: FC<ButtonNodeProps> = ({
|
|||||||
icon,
|
icon,
|
||||||
flipped,
|
flipped,
|
||||||
onClick,
|
onClick,
|
||||||
|
text,
|
||||||
}) => {
|
}) => {
|
||||||
|
const inner = (
|
||||||
|
<>
|
||||||
|
<Handle type="target" position={flipped ? Position.Top : Position.Left} />
|
||||||
|
{icon}
|
||||||
|
<Handle
|
||||||
|
type="source"
|
||||||
|
position={!flipped ? Position.Bottom : Position.Right}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="button-node"
|
className="button-node"
|
||||||
@@ -22,22 +34,14 @@ export const ButtonNode: FC<ButtonNodeProps> = ({
|
|||||||
} as any
|
} as any
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<ButtonIcon
|
{!text ? (
|
||||||
icon={
|
<ButtonIcon icon={inner} onClick={onClick} />
|
||||||
<>
|
) : (
|
||||||
<Handle
|
<Button onClick={onClick}>
|
||||||
type="target"
|
{inner}
|
||||||
position={flipped ? Position.Top : Position.Left}
|
{text}
|
||||||
/>
|
</Button>
|
||||||
{icon}
|
)}
|
||||||
<Handle
|
|
||||||
type="source"
|
|
||||||
position={!flipped ? Position.Bottom : Position.Right}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
onClick={onClick}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -3,6 +3,7 @@ import { ReactNode } from "react";
|
|||||||
export type ButtonNodeDataProps = {
|
export type ButtonNodeDataProps = {
|
||||||
icon: ReactNode;
|
icon: ReactNode;
|
||||||
onClick: () => void;
|
onClick: () => void;
|
||||||
|
text?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ButtonNodeProps = ButtonNodeDataProps & {
|
export type ButtonNodeProps = ButtonNodeDataProps & {
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ import { CollectionNodeProps } from "./types";
|
|||||||
import { Handle, Position } from "react-flow-renderer";
|
import { Handle, Position } from "react-flow-renderer";
|
||||||
import { useSequenceNodeColor } from "../SequenceNode/util";
|
import { useSequenceNodeColor } from "../SequenceNode/util";
|
||||||
import { prettyName } from "utils/formatting";
|
import { prettyName } from "utils/formatting";
|
||||||
|
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
||||||
|
import { useConfirm } from "material-ui-confirm";
|
||||||
|
import { useSnackbar } from "notistack";
|
||||||
|
|
||||||
export const CollectionNode: FC<CollectionNodeProps> = ({
|
export const CollectionNode: FC<CollectionNodeProps> = ({
|
||||||
onAddNode,
|
onAddNode,
|
||||||
@@ -18,8 +21,13 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
|||||||
flipped,
|
flipped,
|
||||||
color,
|
color,
|
||||||
collectionType,
|
collectionType,
|
||||||
|
onDelete,
|
||||||
|
title,
|
||||||
}) => {
|
}) => {
|
||||||
const nodeColor = useSequenceNodeColor(color);
|
const nodeColor = useSequenceNodeColor(color);
|
||||||
|
const confirm = useConfirm();
|
||||||
|
const snackbr = useSnackbar();
|
||||||
|
const trueTile = title ?? collectionType;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -33,6 +41,30 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
|||||||
}
|
}
|
||||||
>
|
>
|
||||||
<div className="collection-nodes">
|
<div className="collection-nodes">
|
||||||
|
{!!onDelete && (
|
||||||
|
<ButtonIcon
|
||||||
|
className="delete-icon"
|
||||||
|
icon={<DeleteForeverIcon />}
|
||||||
|
onClick={() => {
|
||||||
|
confirm({
|
||||||
|
description: `Are you sure you want to delete ${trueTile}?`,
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
onDelete();
|
||||||
|
snackbr.enqueueSnackbar("Deleted.", {
|
||||||
|
variant: "info",
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(() =>
|
||||||
|
snackbr.enqueueSnackbar(`Did not delete ${trueTile}.`, {
|
||||||
|
variant: "info",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
borderless
|
||||||
|
color="secondary"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<span className="collection-nodes--title">
|
<span className="collection-nodes--title">
|
||||||
{prettyName(collectionType)}s
|
{prettyName(collectionType)}s
|
||||||
</span>
|
</span>
|
||||||
@@ -48,23 +80,38 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
|
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
|
||||||
<Handle
|
|
||||||
key="source"
|
|
||||||
type="source"
|
|
||||||
position={flipped ? Position.Bottom : Position.Right}
|
|
||||||
/>
|
|
||||||
<>
|
<>
|
||||||
<Handle
|
<Handle
|
||||||
id="default"
|
id="default"
|
||||||
key="target"
|
key="source-def"
|
||||||
|
type="source"
|
||||||
|
position={flipped ? Position.Bottom : Position.Right}
|
||||||
|
/>
|
||||||
|
<Handle
|
||||||
|
id="head"
|
||||||
|
key="source-head"
|
||||||
|
type="source"
|
||||||
|
position={flipped ? Position.Top : Position.Left}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
<>
|
||||||
|
<Handle
|
||||||
|
id="default"
|
||||||
|
key="target-def"
|
||||||
type="target"
|
type="target"
|
||||||
position={flipped ? Position.Top : Position.Left}
|
position={flipped ? Position.Top : Position.Left}
|
||||||
/>
|
/>
|
||||||
|
<Handle
|
||||||
|
id="side"
|
||||||
|
key="target-side"
|
||||||
|
type="target"
|
||||||
|
position={!flipped ? Position.Right : Position.Left}
|
||||||
|
/>
|
||||||
<Handle
|
<Handle
|
||||||
id="return"
|
id="return"
|
||||||
key="target-return"
|
key="target-return"
|
||||||
type="target"
|
type="target"
|
||||||
position={!flipped ? Position.Top : Position.Right}
|
position={!flipped ? Position.Bottom : Position.Right}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
<span className="collection-nodes--total">
|
<span className="collection-nodes--total">
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type DemoProps = {
|
|||||||
width: number;
|
width: number;
|
||||||
totalChildNodes: number;
|
totalChildNodes: number;
|
||||||
onAddNode: () => void;
|
onAddNode: () => void;
|
||||||
|
hasDelete: boolean;
|
||||||
enabledChild: boolean;
|
enabledChild: boolean;
|
||||||
};
|
};
|
||||||
const Demo: FC<DemoProps> = (args) => {
|
const Demo: FC<DemoProps> = (args) => {
|
||||||
@@ -26,7 +27,7 @@ const Demo: FC<DemoProps> = (args) => {
|
|||||||
data: {
|
data: {
|
||||||
elements: {
|
elements: {
|
||||||
edges: [],
|
edges: [],
|
||||||
nodes: ["trogger", "sequence", "condition"].map(
|
nodes: ["trigger", "sequence", "condition"].map(
|
||||||
(collectionType: any, i: number) =>
|
(collectionType: any, i: number) =>
|
||||||
CollectionNodeMaker.makeElement(
|
CollectionNodeMaker.makeElement(
|
||||||
{
|
{
|
||||||
@@ -40,6 +41,7 @@ const Demo: FC<DemoProps> = (args) => {
|
|||||||
{
|
{
|
||||||
hasInput: false,
|
hasInput: false,
|
||||||
onAddNode: args.onAddNode,
|
onAddNode: args.onAddNode,
|
||||||
|
onDelete: args.hasDelete ? console.info : undefined,
|
||||||
width: args.width,
|
width: args.width,
|
||||||
height: args.height,
|
height: args.height,
|
||||||
nodes: [
|
nodes: [
|
||||||
@@ -71,9 +73,10 @@ export default {
|
|||||||
component: Demo,
|
component: Demo,
|
||||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||||
args: {
|
args: {
|
||||||
...DEFAULT_DIMS.condition,
|
...DEFAULT_DIMS.collection,
|
||||||
totalChildNodes: 1,
|
totalChildNodes: 1,
|
||||||
enabledChild: true,
|
enabledChild: true,
|
||||||
|
hasDelete: false,
|
||||||
},
|
},
|
||||||
} as ComponentMeta<typeof Demo>;
|
} as ComponentMeta<typeof Demo>;
|
||||||
|
|
||||||
@@ -101,6 +104,7 @@ export const Empty: ComponentStory<typeof Demo> = (args) => {
|
|||||||
DEFAULT_DIMS,
|
DEFAULT_DIMS,
|
||||||
{
|
{
|
||||||
hasInput: false,
|
hasInput: false,
|
||||||
|
onDelete: args.hasDelete ? console.info : undefined,
|
||||||
onAddNode: args.onAddNode,
|
onAddNode: args.onAddNode,
|
||||||
width: args.width,
|
width: args.width,
|
||||||
height: args.height,
|
height: args.height,
|
||||||
@@ -194,6 +198,7 @@ export const ReturnTarget: ComponentStory<typeof Demo> = (args) => {
|
|||||||
},
|
},
|
||||||
DEFAULT_DIMS,
|
DEFAULT_DIMS,
|
||||||
{
|
{
|
||||||
|
onDelete: args.hasDelete ? console.info : undefined,
|
||||||
onAddNode: args.onAddNode,
|
onAddNode: args.onAddNode,
|
||||||
width: args.width,
|
width: args.width,
|
||||||
height: args.height,
|
height: args.height,
|
||||||
@@ -220,3 +225,9 @@ export const ReturnTarget: ComponentStory<typeof Demo> = (args) => {
|
|||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const DeleteIconExample = Empty.bind({});
|
||||||
|
DeleteIconExample.args = {
|
||||||
|
...DeleteIconExample.args,
|
||||||
|
hasDelete: true,
|
||||||
|
};
|
||||||
@@ -12,6 +12,8 @@ export type CollectionNodeDataProps = Size & {
|
|||||||
onAddNode: () => void;
|
onAddNode: () => void;
|
||||||
hasInput?: boolean;
|
hasInput?: boolean;
|
||||||
collectionType: keyof AutomationActionData;
|
collectionType: keyof AutomationActionData;
|
||||||
|
onDelete?: () => void;
|
||||||
|
title?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CollectionNodeProps = CollectionNodeDataProps & {
|
export type CollectionNodeProps = CollectionNodeDataProps & {
|
||||||
|
|||||||
@@ -10,15 +10,22 @@ export const SequenceNode: FC<SequenceNodeProps> = ({
|
|||||||
return (
|
return (
|
||||||
<SequenceNodeElement {...props}>
|
<SequenceNodeElement {...props}>
|
||||||
{hasInput && (
|
{hasInput && (
|
||||||
<Handle
|
<>
|
||||||
type="target"
|
<Handle
|
||||||
position={props.flipped ? Position.Top : Position.Left}
|
type="target"
|
||||||
/>
|
position={props.flipped ? Position.Top : Position.Left}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
<Handle
|
<Handle
|
||||||
type="source"
|
type="source"
|
||||||
position={props.flipped ? Position.Bottom : Position.Right}
|
position={props.flipped ? Position.Bottom : Position.Right}
|
||||||
/>
|
/>
|
||||||
|
<Handle
|
||||||
|
id="side"
|
||||||
|
type="source"
|
||||||
|
position={props.flipped ? Position.Right : Position.Bottom}
|
||||||
|
/>
|
||||||
</SequenceNodeElement>
|
</SequenceNodeElement>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -4,9 +4,11 @@ import {
|
|||||||
} from "types/automations";
|
} from "types/automations";
|
||||||
import { ModalState } from "../board/types";
|
import { ModalState } from "../board/types";
|
||||||
import { SequenceNodeActions } from "../nodes/SequenceNode/types";
|
import { SequenceNodeActions } from "../nodes/SequenceNode/types";
|
||||||
import { DAGUpdaterArgs } from "./types";
|
import { DAGUpdaterArgs, DAGUpdaterInput } from "./types";
|
||||||
import { mapAutoActionKeyToNodeType } from "./util";
|
import { mapAutoActionKeyToNodeType } from "./util";
|
||||||
import { ChooseAction, RepeatAction } from "types/automations/actions";
|
import { ChooseAction, RepeatAction } from "types/automations/actions";
|
||||||
|
import { ScriptConditionField } from "types/automations/common";
|
||||||
|
import { convertScriptConditionFieldToAutomationConditions } from "utils/automations";
|
||||||
|
|
||||||
export const createBasicUpdater =
|
export const createBasicUpdater =
|
||||||
<K extends keyof AutomationActionData, N extends AutomationActionData[K][0]>(
|
<K extends keyof AutomationActionData, N extends AutomationActionData[K][0]>(
|
||||||
@@ -73,7 +75,16 @@ export const createBasicUpdater =
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const createDummyUpdater = (): DAGUpdaterInput<any> => ({
|
||||||
|
data: [],
|
||||||
|
onUpdate: console.warn,
|
||||||
|
});
|
||||||
|
// export const createScriptConditionFieldUpdater = (f: ScriptConditionField, )
|
||||||
|
|
||||||
export type DAGGraphUpdater = ReturnType<typeof createUpdater>;
|
export type DAGGraphUpdater = ReturnType<typeof createUpdater>;
|
||||||
|
export type DAGGraphChooseUpdater = ReturnType<
|
||||||
|
DAGGraphUpdater["createChoosNodeUpdater"]
|
||||||
|
>;
|
||||||
export const createUpdater = (
|
export const createUpdater = (
|
||||||
args: DAGUpdaterArgs<"sequence" | "condition" | "trigger">
|
args: DAGUpdaterArgs<"sequence" | "condition" | "trigger">
|
||||||
) => {
|
) => {
|
||||||
@@ -159,109 +170,153 @@ export const createUpdater = (
|
|||||||
createChoosNodeUpdater(i: number, j: number | "else") {
|
createChoosNodeUpdater(i: number, j: number | "else") {
|
||||||
const node = args.sequence.data[i] as ChooseAction;
|
const node = args.sequence.data[i] as ChooseAction;
|
||||||
if (j === "else") {
|
if (j === "else") {
|
||||||
return createUpdater({
|
return {
|
||||||
openModal: args.openModal,
|
stateUpdater: createUpdater({
|
||||||
condition: {
|
openModal: args.openModal,
|
||||||
data: [],
|
condition: {
|
||||||
onUpdate: console.warn,
|
data: [],
|
||||||
},
|
onUpdate: console.warn,
|
||||||
sequence: {
|
},
|
||||||
data: node.default ?? [],
|
sequence: {
|
||||||
onUpdate: (upd) =>
|
data: node.default ?? [],
|
||||||
basic.sequence.updateNode(
|
onUpdate: (upd) =>
|
||||||
{
|
basic.sequence.updateNode(
|
||||||
...node,
|
{
|
||||||
default: upd,
|
...node,
|
||||||
},
|
default: upd,
|
||||||
i
|
},
|
||||||
),
|
i
|
||||||
},
|
),
|
||||||
trigger: {
|
},
|
||||||
data: [],
|
trigger: {
|
||||||
onUpdate: console.warn,
|
data: [],
|
||||||
},
|
onUpdate: console.warn,
|
||||||
});
|
},
|
||||||
|
}),
|
||||||
|
onRemove: () => {},
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
return createUpdater({
|
return {
|
||||||
openModal: args.openModal,
|
onRemove: () =>
|
||||||
condition: {
|
basic.sequence.updateNode(
|
||||||
data: node.choose[j].conditions,
|
{
|
||||||
onUpdate: (upd: any) =>
|
...node,
|
||||||
basic.sequence.updateNode(
|
choose: [
|
||||||
{
|
...node.choose.slice(0, j),
|
||||||
...node,
|
...node.choose.slice(j + 1),
|
||||||
choose: [
|
],
|
||||||
...node.choose.slice(0, j),
|
},
|
||||||
{
|
i
|
||||||
...node.choose[j],
|
),
|
||||||
conditions: upd,
|
stateUpdater: createUpdater({
|
||||||
},
|
openModal: args.openModal,
|
||||||
...node.choose.slice(j + 1),
|
condition: {
|
||||||
],
|
data: node.choose[j].conditions,
|
||||||
},
|
onUpdate: (upd: any) =>
|
||||||
i
|
basic.sequence.updateNode(
|
||||||
),
|
{
|
||||||
},
|
...node,
|
||||||
sequence: {
|
choose: [
|
||||||
data: node.choose[j].sequence,
|
...node.choose.slice(0, j),
|
||||||
onUpdate: (upd) =>
|
{
|
||||||
basic.sequence.updateNode(
|
...node.choose[j],
|
||||||
{
|
conditions: upd,
|
||||||
...node,
|
},
|
||||||
choose: [
|
...node.choose.slice(j + 1),
|
||||||
...node.choose.slice(0, j),
|
],
|
||||||
{
|
},
|
||||||
...node.choose[j],
|
i
|
||||||
sequence: upd,
|
),
|
||||||
},
|
},
|
||||||
...node.choose.slice(j + 1),
|
sequence: {
|
||||||
],
|
data: node.choose[j].sequence,
|
||||||
},
|
onUpdate: (upd) =>
|
||||||
i
|
basic.sequence.updateNode(
|
||||||
),
|
{
|
||||||
},
|
...node,
|
||||||
trigger: {
|
choose: [
|
||||||
data: [],
|
...node.choose.slice(0, j),
|
||||||
onUpdate: console.warn,
|
{
|
||||||
},
|
...node.choose[j],
|
||||||
});
|
sequence: upd,
|
||||||
|
},
|
||||||
|
...node.choose.slice(j + 1),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
i
|
||||||
|
),
|
||||||
|
},
|
||||||
|
trigger: {
|
||||||
|
data: [],
|
||||||
|
onUpdate: console.warn,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
createRepeatNodeUpdater(i: number) {
|
createRepeatNodeUpdater(i: number) {
|
||||||
const { repeat } = args.sequence.data[i] as RepeatAction;
|
const { repeat } = args.sequence.data[i] as RepeatAction;
|
||||||
return createUpdater({
|
|
||||||
openModal: args.openModal,
|
return {
|
||||||
condition: {
|
sequence: createUpdater({
|
||||||
data: repeat.while ?? [],
|
openModal: args.openModal,
|
||||||
onUpdate: (upd: any) =>
|
condition: createDummyUpdater(),
|
||||||
basic.sequence.updateNode(
|
sequence: {
|
||||||
{
|
data: repeat.sequence,
|
||||||
repeat: {
|
onUpdate: (upd) =>
|
||||||
...repeat,
|
basic.sequence.updateNode(
|
||||||
while: upd,
|
{
|
||||||
|
repeat: {
|
||||||
|
...repeat,
|
||||||
|
sequence: upd,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
i
|
||||||
i
|
),
|
||||||
|
},
|
||||||
|
trigger: createDummyUpdater(),
|
||||||
|
}),
|
||||||
|
while: createUpdater({
|
||||||
|
openModal: args.openModal,
|
||||||
|
condition: {
|
||||||
|
data: convertScriptConditionFieldToAutomationConditions(
|
||||||
|
repeat.while
|
||||||
),
|
),
|
||||||
},
|
onUpdate: (upd: any) =>
|
||||||
sequence: {
|
basic.sequence.updateNode(
|
||||||
data: repeat.sequence ?? [],
|
{
|
||||||
onUpdate: (upd) =>
|
repeat: {
|
||||||
basic.sequence.updateNode(
|
...repeat,
|
||||||
{
|
while: upd,
|
||||||
repeat: {
|
},
|
||||||
...repeat,
|
|
||||||
sequence: upd,
|
|
||||||
},
|
},
|
||||||
},
|
i
|
||||||
i
|
),
|
||||||
|
},
|
||||||
|
sequence: createDummyUpdater(),
|
||||||
|
trigger: createDummyUpdater(),
|
||||||
|
}),
|
||||||
|
until: createUpdater({
|
||||||
|
openModal: args.openModal,
|
||||||
|
condition: {
|
||||||
|
data: convertScriptConditionFieldToAutomationConditions(
|
||||||
|
repeat.until
|
||||||
),
|
),
|
||||||
},
|
onUpdate: (upd: any) =>
|
||||||
trigger: {
|
basic.sequence.updateNode(
|
||||||
data: [],
|
{
|
||||||
onUpdate: console.warn,
|
repeat: {
|
||||||
},
|
...repeat,
|
||||||
});
|
until: upd,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
i
|
||||||
|
),
|
||||||
|
},
|
||||||
|
sequence: createDummyUpdater(),
|
||||||
|
trigger: createDummyUpdater(),
|
||||||
|
}),
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,4 +3,8 @@
|
|||||||
border-style: solid;
|
border-style: solid;
|
||||||
max-width: 1.5em;
|
max-width: 1.5em;
|
||||||
max-height: 1.5em;
|
max-height: 1.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.MuiButtonBase-root.borderless.icon-button[type="button"] {
|
||||||
|
border-width: 0;
|
||||||
}
|
}
|
||||||
@@ -9,11 +9,16 @@ export type ButtonIconProps = {
|
|||||||
title?: string;
|
title?: string;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
color?: IconButtonTypeMap["props"]["color"];
|
color?: IconButtonTypeMap["props"]["color"];
|
||||||
|
borderless?: boolean;
|
||||||
};
|
};
|
||||||
export const ButtonIcon: FC<ButtonIconProps> = (props) => {
|
export const ButtonIcon: FC<ButtonIconProps> = (props) => {
|
||||||
return (
|
return (
|
||||||
<MuiIconButton
|
<MuiIconButton
|
||||||
className={["icon-button", props.className ?? ""].join(" ")}
|
className={[
|
||||||
|
"icon-button",
|
||||||
|
props.className ?? "",
|
||||||
|
props.borderless ? "borderless" : "",
|
||||||
|
].join(" ")}
|
||||||
onClick={props.onClick}
|
onClick={props.onClick}
|
||||||
title={props.title}
|
title={props.title}
|
||||||
color={props.color}
|
color={props.color}
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
|
|||||||
update({ repeat: { ...state.repeat, count } })
|
update({ repeat: { ...state.repeat, count } })
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
<InputYaml
|
||||||
|
label="While"
|
||||||
|
value={state.repeat.while ?? []}
|
||||||
|
onChange={(sequence) =>
|
||||||
|
update({ repeat: { ...state.repeat, while: sequence } })
|
||||||
|
}
|
||||||
|
/>
|
||||||
<InputYaml
|
<InputYaml
|
||||||
label="Sequence"
|
label="Sequence"
|
||||||
value={state.repeat.sequence ?? []}
|
value={state.repeat.sequence ?? []}
|
||||||
@@ -31,11 +38,9 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<InputYaml
|
<InputYaml
|
||||||
label="Condition"
|
label="Until"
|
||||||
value={state.repeat.while ?? []}
|
value={state.repeat.until ?? []}
|
||||||
onChange={(sequence) =>
|
onChange={(until) => update({ repeat: { ...state.repeat, until } })}
|
||||||
update({ repeat: { ...state.repeat, sequence } })
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { AutomationSequenceNode } from ".";
|
|||||||
import { AutomationCondition } from "./conditions";
|
import { AutomationCondition } from "./conditions";
|
||||||
import * as v from "types/validators/actions";
|
import * as v from "types/validators/actions";
|
||||||
import * as st from "superstruct";
|
import * as st from "superstruct";
|
||||||
|
import { ScriptConditionField } from "./common";
|
||||||
|
|
||||||
type AutomationActionNodeBase<Data extends Object> = Data &
|
type AutomationActionNodeBase<Data extends Object> = Data &
|
||||||
st.Infer<typeof v.AutomationActionNodeBase>;
|
st.Infer<typeof v.AutomationActionNodeBase>;
|
||||||
@@ -10,7 +11,8 @@ export type ServiceAction = st.Infer<typeof v.ServiceAction>;
|
|||||||
export type RepeatAction = AutomationActionNodeBase<{
|
export type RepeatAction = AutomationActionNodeBase<{
|
||||||
repeat: {
|
repeat: {
|
||||||
count?: number;
|
count?: number;
|
||||||
while: AutomationCondition[];
|
while?: ScriptConditionField;
|
||||||
|
until?: ScriptConditionField;
|
||||||
sequence: AutomationAction[];
|
sequence: AutomationAction[];
|
||||||
};
|
};
|
||||||
}>;
|
}>;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as st from "superstruct";
|
import * as st from "superstruct";
|
||||||
import * as v from "types/validators/common";
|
import * as v from "types/validators/common";
|
||||||
|
import { AutomationCondition } from "./conditions";
|
||||||
|
|
||||||
export type AutomationTime = st.Infer<typeof v.AutomationTime>;
|
export type AutomationTime = st.Infer<typeof v.AutomationTime>;
|
||||||
export type AutomationTimeObject = st.Infer<typeof v.AutomationTimeObject>;
|
export type AutomationTimeObject = st.Infer<typeof v.AutomationTimeObject>;
|
||||||
@@ -7,3 +8,4 @@ export type AutomationTimeString = st.Infer<typeof v.AutomationTimeString>;
|
|||||||
export type EntityId = st.Infer<typeof v.EntityId>;
|
export type EntityId = st.Infer<typeof v.EntityId>;
|
||||||
export type AutomationDeviceState = st.Infer<typeof v.AutomationDeviceState>;
|
export type AutomationDeviceState = st.Infer<typeof v.AutomationDeviceState>;
|
||||||
export type DayOfWeek = st.Infer<typeof v.DayOfWeek>;
|
export type DayOfWeek = st.Infer<typeof v.DayOfWeek>;
|
||||||
|
export type ScriptConditionField = string | AutomationCondition[];
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
import * as st from "superstruct";
|
import * as st from "superstruct";
|
||||||
import { AutomationDeviceState, AutomationTime } from "./common";
|
import {
|
||||||
|
AutomationDeviceState,
|
||||||
|
AutomationTime,
|
||||||
|
ScriptConditionField,
|
||||||
|
} from "./common";
|
||||||
import { AutomationTrigger } from "./triggers";
|
import { AutomationTrigger } from "./triggers";
|
||||||
|
|
||||||
export const AutomationActionNodeBase = st.type({
|
export const AutomationActionNodeBase = st.type({
|
||||||
@@ -22,7 +26,8 @@ export const RepeatAction = st.intersection([
|
|||||||
st.type({
|
st.type({
|
||||||
repeat: st.object({
|
repeat: st.object({
|
||||||
count: st.optional(st.number()),
|
count: st.optional(st.number()),
|
||||||
while: st.array(st.object()),
|
while: st.optional(ScriptConditionField),
|
||||||
|
until: st.optional(ScriptConditionField),
|
||||||
sequence: st.array(st.object()),
|
sequence: st.array(st.object()),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -64,3 +64,7 @@ export const DayOfWeek = st.enums([
|
|||||||
"sat",
|
"sat",
|
||||||
"sun",
|
"sun",
|
||||||
]);
|
]);
|
||||||
|
export const ScriptConditionField = st.union([
|
||||||
|
st.array(st.object()),
|
||||||
|
st.string(),
|
||||||
|
]);
|
||||||
@@ -9,8 +9,12 @@ import { AutomationTrigger, TriggerType } from "types/automations/triggers";
|
|||||||
import { MiniFailure } from "types/validators/helper";
|
import { MiniFailure } from "types/validators/helper";
|
||||||
import { getFailures } from "../types/validators/helper";
|
import { getFailures } from "../types/validators/helper";
|
||||||
import { ActionType } from "types/automations/actions";
|
import { ActionType } from "types/automations/actions";
|
||||||
import { ConditionType } from "types/automations/conditions";
|
import {
|
||||||
|
ConditionType,
|
||||||
|
AutomationCondition,
|
||||||
|
} from "types/automations/conditions";
|
||||||
import * as st from "superstruct";
|
import * as st from "superstruct";
|
||||||
|
import { ScriptConditionField } from "types/automations/common";
|
||||||
|
|
||||||
export const getNodeTypeAndValidate = (
|
export const getNodeTypeAndValidate = (
|
||||||
node: AutomationNode,
|
node: AutomationNode,
|
||||||
@@ -236,3 +240,15 @@ export const entityDomainsAreBinary = (domains: string[]): boolean => {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const convertScriptConditionFieldToAutomationConditions = (
|
||||||
|
field: ScriptConditionField | undefined
|
||||||
|
): AutomationCondition[] => {
|
||||||
|
if (!field) {
|
||||||
|
return [];
|
||||||
|
} else if (typeof field === "string") {
|
||||||
|
return [{ condition: "template", value_template: field }];
|
||||||
|
} else {
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in new issue
Block a user