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 DEFAULT_DIMS: DAGDims = {
|
||||
trigger: {
|
||||
collection: {
|
||||
height: NODE_HEIGHT * 3,
|
||||
width: NODE_WIDTH * 1.5,
|
||||
},
|
||||
@@ -12,10 +12,6 @@ export const DEFAULT_DIMS: DAGDims = {
|
||||
height: NODE_HEIGHT,
|
||||
width: NODE_WIDTH,
|
||||
},
|
||||
condition: {
|
||||
height: NODE_HEIGHT * 3,
|
||||
width: NODE_WIDTH * 1.5,
|
||||
},
|
||||
distanceFactor: {
|
||||
node: 1.5,
|
||||
collection: 1,
|
||||
|
||||
@@ -26,19 +26,15 @@ export const moveAlongRelativeTo = (
|
||||
offsetPos: XYPosition,
|
||||
relatedPos: XYPosition,
|
||||
dims: DAGDims,
|
||||
incrementBy: keyof DAGEntities = "condition"
|
||||
incrementBy: keyof DAGEntities = "collection"
|
||||
) =>
|
||||
dims.flipped
|
||||
? {
|
||||
x:
|
||||
relatedPos.x +
|
||||
(dims.distanceFactor.node - 1) * dims[incrementBy].width,
|
||||
x: relatedPos.x + dims.distanceFactor.node * dims[incrementBy].width,
|
||||
y: offsetPos.y,
|
||||
}
|
||||
: {
|
||||
y:
|
||||
relatedPos.y +
|
||||
(dims.distanceFactor.node - 1) * dims[incrementBy].height,
|
||||
y: relatedPos.y + dims.distanceFactor.node * dims[incrementBy].height,
|
||||
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",
|
||||
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 { DAGElementsOutputState } from "./outputState";
|
||||
import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
||||
import { getNodeType } from "utils/automations";
|
||||
import {
|
||||
getNodeType,
|
||||
convertScriptConditionFieldToAutomationConditions,
|
||||
} from "utils/automations";
|
||||
import { ButtonNodeMaker } from "../nodes/ButtonNode";
|
||||
import { AddIcon } from "components/Icons";
|
||||
import { useDAGElementsState } from "./state";
|
||||
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 = (
|
||||
automation: AutomationActionData,
|
||||
@@ -44,8 +50,8 @@ export const useAutomationNodes = (
|
||||
state,
|
||||
dims,
|
||||
position: distance.moveFromTo(
|
||||
"trigger",
|
||||
"condition",
|
||||
"collection",
|
||||
"collection",
|
||||
outputState.getLastNodePos(),
|
||||
dims
|
||||
),
|
||||
@@ -61,7 +67,7 @@ export const useAutomationNodes = (
|
||||
state,
|
||||
dims,
|
||||
position: distance.moveFromTo(
|
||||
"condition",
|
||||
"collection",
|
||||
"node",
|
||||
outputState.getLastNodePos(),
|
||||
dims
|
||||
@@ -82,7 +88,7 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
||||
const outputState = new DAGElementsOutputState(position, dims);
|
||||
outputState.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.trigger,
|
||||
...dims.collection,
|
||||
collectionType: "trigger",
|
||||
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
|
||||
nodes: nodes.map((node, index) => ({
|
||||
@@ -102,7 +108,7 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
const outputState = new DAGElementsOutputState(position, dims);
|
||||
outputState.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.condition,
|
||||
...dims.collection,
|
||||
collectionType: "condition",
|
||||
hasInput: !!lastNodeId,
|
||||
onAddNode: () => stateUpdater.basic.condition.addNode(null),
|
||||
@@ -214,108 +220,187 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
}
|
||||
const node = nodes[0];
|
||||
if (
|
||||
(node.enabled ?? true) &&
|
||||
!args.state.get(args.lastNodeId ?? "").isClosed
|
||||
!((node.enabled ?? true) && !args.state.get(args.lastNodeId ?? "").isClosed)
|
||||
) {
|
||||
// add new
|
||||
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");
|
||||
return outputState;
|
||||
}
|
||||
// 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;
|
||||
};
|
||||
@@ -331,32 +416,38 @@ export const makeRepeatNodes: ElementMaker<RepeatAction> = (nodes, args) => {
|
||||
!args.state.get(args.lastNodeId ?? "").isClosed
|
||||
) {
|
||||
// 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,
|
||||
};
|
||||
let offsetPos: XYPosition = distance.offsetBy(
|
||||
"node",
|
||||
args.position,
|
||||
args.dims
|
||||
);
|
||||
let lastNodeId = args.lastNodeId;
|
||||
const stateUpdater = args.stateUpdater.createRepeatNodeUpdater(
|
||||
args.nodeIndex
|
||||
);
|
||||
let pos = offsetPos;
|
||||
let lastNodeId = args.lastNodeId;
|
||||
const conditionState = makeConditionNodes(node.repeat.while, {
|
||||
...args,
|
||||
stateUpdater,
|
||||
position: pos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-while`,
|
||||
});
|
||||
conditionState.data.edges[0].label = `While`;
|
||||
outputState.extend(conditionState);
|
||||
pos = distance.moveFromTo("condition", "node", pos, args.dims);
|
||||
lastNodeId = conditionState.lastNodeId;
|
||||
|
||||
// while
|
||||
const whileState = makeConditionNodes(
|
||||
convertScriptConditionFieldToAutomationConditions(node.repeat.while),
|
||||
{
|
||||
...args,
|
||||
stateUpdater: stateUpdater.while,
|
||||
position: offsetPos,
|
||||
lastNodeId,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-while`,
|
||||
}
|
||||
);
|
||||
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, {
|
||||
...args,
|
||||
stateUpdater,
|
||||
position: pos,
|
||||
stateUpdater: stateUpdater.sequence,
|
||||
position: offsetPos,
|
||||
lastNodeId: undefined,
|
||||
nodeIndex: 0,
|
||||
nodeId: `${args.nodeId}-repeat-sequence`,
|
||||
@@ -367,16 +458,36 @@ export const makeRepeatNodes: ElementMaker<RepeatAction> = (nodes, args) => {
|
||||
label: "do",
|
||||
});
|
||||
}
|
||||
lastNodeId = sequenceState.lastNodeId;
|
||||
offsetPos = distance.moveAlongRelativeTo(
|
||||
offsetPos,
|
||||
sequenceState.bbox[1],
|
||||
args.dims,
|
||||
"condition"
|
||||
"node"
|
||||
);
|
||||
if (sequenceState.lastNodeId && conditionState.lastNodeId) {
|
||||
outputState.addEdge(sequenceState.lastNodeId, conditionState.lastNodeId, {
|
||||
// until
|
||||
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,
|
||||
label: "repeat",
|
||||
sourceHandle: "head",
|
||||
targetHandle: "return",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,15 +16,11 @@ export type ElementData = {
|
||||
export type DAGEntityDim = Size;
|
||||
export type DAGEntities = {
|
||||
node: DAGEntityDim;
|
||||
condition: DAGEntityDim;
|
||||
trigger: DAGEntityDim;
|
||||
collection: DAGEntityDim;
|
||||
};
|
||||
export type DAGDims = DAGEntities & {
|
||||
position: XYPosition;
|
||||
distanceFactor: {
|
||||
node: number;
|
||||
collection: number;
|
||||
};
|
||||
distanceFactor: Record<keyof DAGEntities, number>;
|
||||
flipped: boolean;
|
||||
};
|
||||
export type ElementMakerBaseProps = {
|
||||
|
||||
@@ -85,3 +85,8 @@ const Template: ComponentStory<typeof ButtonNode> = (args) => {
|
||||
};
|
||||
|
||||
export const Basic = Template.bind({});
|
||||
export const Text = Template.bind({});
|
||||
Text.args = {
|
||||
...Text.args,
|
||||
text: "Option",
|
||||
};
|
||||
@@ -11,4 +11,9 @@
|
||||
align-items: center;
|
||||
|
||||
/* 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 { ButtonNodeProps } from "./types";
|
||||
import { Handle, Position } from "react-flow-renderer";
|
||||
import { Button } from "components/Inputs/Button";
|
||||
|
||||
export const ButtonNode: FC<ButtonNodeProps> = ({
|
||||
height,
|
||||
@@ -11,7 +12,18 @@ export const ButtonNode: FC<ButtonNodeProps> = ({
|
||||
icon,
|
||||
flipped,
|
||||
onClick,
|
||||
text,
|
||||
}) => {
|
||||
const inner = (
|
||||
<>
|
||||
<Handle type="target" position={flipped ? Position.Top : Position.Left} />
|
||||
{icon}
|
||||
<Handle
|
||||
type="source"
|
||||
position={!flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
return (
|
||||
<div
|
||||
className="button-node"
|
||||
@@ -22,22 +34,14 @@ export const ButtonNode: FC<ButtonNodeProps> = ({
|
||||
} as any
|
||||
}
|
||||
>
|
||||
<ButtonIcon
|
||||
icon={
|
||||
<>
|
||||
<Handle
|
||||
type="target"
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
{icon}
|
||||
<Handle
|
||||
type="source"
|
||||
position={!flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
onClick={onClick}
|
||||
/>
|
||||
{!text ? (
|
||||
<ButtonIcon icon={inner} onClick={onClick} />
|
||||
) : (
|
||||
<Button onClick={onClick}>
|
||||
{inner}
|
||||
{text}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -3,6 +3,7 @@ import { ReactNode } from "react";
|
||||
export type ButtonNodeDataProps = {
|
||||
icon: ReactNode;
|
||||
onClick: () => void;
|
||||
text?: string;
|
||||
};
|
||||
|
||||
export type ButtonNodeProps = ButtonNodeDataProps & {
|
||||
|
||||
@@ -8,6 +8,9 @@ import { CollectionNodeProps } from "./types";
|
||||
import { Handle, Position } from "react-flow-renderer";
|
||||
import { useSequenceNodeColor } from "../SequenceNode/util";
|
||||
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> = ({
|
||||
onAddNode,
|
||||
@@ -18,8 +21,13 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
||||
flipped,
|
||||
color,
|
||||
collectionType,
|
||||
onDelete,
|
||||
title,
|
||||
}) => {
|
||||
const nodeColor = useSequenceNodeColor(color);
|
||||
const confirm = useConfirm();
|
||||
const snackbr = useSnackbar();
|
||||
const trueTile = title ?? collectionType;
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -33,6 +41,30 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
||||
}
|
||||
>
|
||||
<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">
|
||||
{prettyName(collectionType)}s
|
||||
</span>
|
||||
@@ -48,23 +80,38 @@ export const CollectionNode: FC<CollectionNodeProps> = ({
|
||||
))}
|
||||
</div>
|
||||
<ButtonIcon className="add" icon={<Add />} onClick={onAddNode} />
|
||||
<Handle
|
||||
key="source"
|
||||
type="source"
|
||||
position={flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
<>
|
||||
<Handle
|
||||
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"
|
||||
position={flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
<Handle
|
||||
id="side"
|
||||
key="target-side"
|
||||
type="target"
|
||||
position={!flipped ? Position.Right : Position.Left}
|
||||
/>
|
||||
<Handle
|
||||
id="return"
|
||||
key="target-return"
|
||||
type="target"
|
||||
position={!flipped ? Position.Top : Position.Right}
|
||||
position={!flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
</>
|
||||
<span className="collection-nodes--total">
|
||||
|
||||
@@ -14,6 +14,7 @@ type DemoProps = {
|
||||
width: number;
|
||||
totalChildNodes: number;
|
||||
onAddNode: () => void;
|
||||
hasDelete: boolean;
|
||||
enabledChild: boolean;
|
||||
};
|
||||
const Demo: FC<DemoProps> = (args) => {
|
||||
@@ -26,7 +27,7 @@ const Demo: FC<DemoProps> = (args) => {
|
||||
data: {
|
||||
elements: {
|
||||
edges: [],
|
||||
nodes: ["trogger", "sequence", "condition"].map(
|
||||
nodes: ["trigger", "sequence", "condition"].map(
|
||||
(collectionType: any, i: number) =>
|
||||
CollectionNodeMaker.makeElement(
|
||||
{
|
||||
@@ -40,6 +41,7 @@ const Demo: FC<DemoProps> = (args) => {
|
||||
{
|
||||
hasInput: false,
|
||||
onAddNode: args.onAddNode,
|
||||
onDelete: args.hasDelete ? console.info : undefined,
|
||||
width: args.width,
|
||||
height: args.height,
|
||||
nodes: [
|
||||
@@ -71,9 +73,10 @@ export default {
|
||||
component: Demo,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
...DEFAULT_DIMS.condition,
|
||||
...DEFAULT_DIMS.collection,
|
||||
totalChildNodes: 1,
|
||||
enabledChild: true,
|
||||
hasDelete: false,
|
||||
},
|
||||
} as ComponentMeta<typeof Demo>;
|
||||
|
||||
@@ -101,6 +104,7 @@ export const Empty: ComponentStory<typeof Demo> = (args) => {
|
||||
DEFAULT_DIMS,
|
||||
{
|
||||
hasInput: false,
|
||||
onDelete: args.hasDelete ? console.info : undefined,
|
||||
onAddNode: args.onAddNode,
|
||||
width: args.width,
|
||||
height: args.height,
|
||||
@@ -194,6 +198,7 @@ export const ReturnTarget: ComponentStory<typeof Demo> = (args) => {
|
||||
},
|
||||
DEFAULT_DIMS,
|
||||
{
|
||||
onDelete: args.hasDelete ? console.info : undefined,
|
||||
onAddNode: args.onAddNode,
|
||||
width: args.width,
|
||||
height: args.height,
|
||||
@@ -220,3 +225,9 @@ export const ReturnTarget: ComponentStory<typeof Demo> = (args) => {
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
export const DeleteIconExample = Empty.bind({});
|
||||
DeleteIconExample.args = {
|
||||
...DeleteIconExample.args,
|
||||
hasDelete: true,
|
||||
};
|
||||
@@ -12,6 +12,8 @@ export type CollectionNodeDataProps = Size & {
|
||||
onAddNode: () => void;
|
||||
hasInput?: boolean;
|
||||
collectionType: keyof AutomationActionData;
|
||||
onDelete?: () => void;
|
||||
title?: string;
|
||||
};
|
||||
|
||||
export type CollectionNodeProps = CollectionNodeDataProps & {
|
||||
|
||||
@@ -10,15 +10,22 @@ export const SequenceNode: FC<SequenceNodeProps> = ({
|
||||
return (
|
||||
<SequenceNodeElement {...props}>
|
||||
{hasInput && (
|
||||
<Handle
|
||||
type="target"
|
||||
position={props.flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
<>
|
||||
<Handle
|
||||
type="target"
|
||||
position={props.flipped ? Position.Top : Position.Left}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<Handle
|
||||
type="source"
|
||||
position={props.flipped ? Position.Bottom : Position.Right}
|
||||
/>
|
||||
<Handle
|
||||
id="side"
|
||||
type="source"
|
||||
position={props.flipped ? Position.Right : Position.Bottom}
|
||||
/>
|
||||
</SequenceNodeElement>
|
||||
);
|
||||
};
|
||||
@@ -4,9 +4,11 @@ import {
|
||||
} from "types/automations";
|
||||
import { ModalState } from "../board/types";
|
||||
import { SequenceNodeActions } from "../nodes/SequenceNode/types";
|
||||
import { DAGUpdaterArgs } from "./types";
|
||||
import { DAGUpdaterArgs, DAGUpdaterInput } from "./types";
|
||||
import { mapAutoActionKeyToNodeType } from "./util";
|
||||
import { ChooseAction, RepeatAction } from "types/automations/actions";
|
||||
import { ScriptConditionField } from "types/automations/common";
|
||||
import { convertScriptConditionFieldToAutomationConditions } from "utils/automations";
|
||||
|
||||
export const createBasicUpdater =
|
||||
<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 DAGGraphChooseUpdater = ReturnType<
|
||||
DAGGraphUpdater["createChoosNodeUpdater"]
|
||||
>;
|
||||
export const createUpdater = (
|
||||
args: DAGUpdaterArgs<"sequence" | "condition" | "trigger">
|
||||
) => {
|
||||
@@ -159,109 +170,153 @@ export const createUpdater = (
|
||||
createChoosNodeUpdater(i: number, j: number | "else") {
|
||||
const node = args.sequence.data[i] as ChooseAction;
|
||||
if (j === "else") {
|
||||
return createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
sequence: {
|
||||
data: node.default ?? [],
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
default: upd,
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
trigger: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
});
|
||||
return {
|
||||
stateUpdater: createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
sequence: {
|
||||
data: node.default ?? [],
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
default: upd,
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
trigger: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
}),
|
||||
onRemove: () => {},
|
||||
};
|
||||
} else {
|
||||
return createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: node.choose[j].conditions,
|
||||
onUpdate: (upd: any) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
{
|
||||
...node.choose[j],
|
||||
conditions: upd,
|
||||
},
|
||||
...node.choose.slice(j + 1),
|
||||
],
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
sequence: {
|
||||
data: node.choose[j].sequence,
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
{
|
||||
...node.choose[j],
|
||||
sequence: upd,
|
||||
},
|
||||
...node.choose.slice(j + 1),
|
||||
],
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
trigger: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
});
|
||||
return {
|
||||
onRemove: () =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
...node.choose.slice(j + 1),
|
||||
],
|
||||
},
|
||||
i
|
||||
),
|
||||
stateUpdater: createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: node.choose[j].conditions,
|
||||
onUpdate: (upd: any) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
{
|
||||
...node.choose[j],
|
||||
conditions: upd,
|
||||
},
|
||||
...node.choose.slice(j + 1),
|
||||
],
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
sequence: {
|
||||
data: node.choose[j].sequence,
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
...node,
|
||||
choose: [
|
||||
...node.choose.slice(0, j),
|
||||
{
|
||||
...node.choose[j],
|
||||
sequence: upd,
|
||||
},
|
||||
...node.choose.slice(j + 1),
|
||||
],
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
trigger: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
},
|
||||
createRepeatNodeUpdater(i: number) {
|
||||
const { repeat } = args.sequence.data[i] as RepeatAction;
|
||||
return createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: repeat.while ?? [],
|
||||
onUpdate: (upd: any) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
repeat: {
|
||||
...repeat,
|
||||
while: upd,
|
||||
|
||||
return {
|
||||
sequence: createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: createDummyUpdater(),
|
||||
sequence: {
|
||||
data: repeat.sequence,
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
repeat: {
|
||||
...repeat,
|
||||
sequence: upd,
|
||||
},
|
||||
},
|
||||
},
|
||||
i
|
||||
i
|
||||
),
|
||||
},
|
||||
trigger: createDummyUpdater(),
|
||||
}),
|
||||
while: createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: convertScriptConditionFieldToAutomationConditions(
|
||||
repeat.while
|
||||
),
|
||||
},
|
||||
sequence: {
|
||||
data: repeat.sequence ?? [],
|
||||
onUpdate: (upd) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
repeat: {
|
||||
...repeat,
|
||||
sequence: upd,
|
||||
onUpdate: (upd: any) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
repeat: {
|
||||
...repeat,
|
||||
while: upd,
|
||||
},
|
||||
},
|
||||
},
|
||||
i
|
||||
i
|
||||
),
|
||||
},
|
||||
sequence: createDummyUpdater(),
|
||||
trigger: createDummyUpdater(),
|
||||
}),
|
||||
until: createUpdater({
|
||||
openModal: args.openModal,
|
||||
condition: {
|
||||
data: convertScriptConditionFieldToAutomationConditions(
|
||||
repeat.until
|
||||
),
|
||||
},
|
||||
trigger: {
|
||||
data: [],
|
||||
onUpdate: console.warn,
|
||||
},
|
||||
});
|
||||
onUpdate: (upd: any) =>
|
||||
basic.sequence.updateNode(
|
||||
{
|
||||
repeat: {
|
||||
...repeat,
|
||||
until: upd,
|
||||
},
|
||||
},
|
||||
i
|
||||
),
|
||||
},
|
||||
sequence: createDummyUpdater(),
|
||||
trigger: createDummyUpdater(),
|
||||
}),
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -3,4 +3,8 @@
|
||||
border-style: solid;
|
||||
max-width: 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;
|
||||
disabled?: boolean;
|
||||
color?: IconButtonTypeMap["props"]["color"];
|
||||
borderless?: boolean;
|
||||
};
|
||||
export const ButtonIcon: FC<ButtonIconProps> = (props) => {
|
||||
return (
|
||||
<MuiIconButton
|
||||
className={["icon-button", props.className ?? ""].join(" ")}
|
||||
className={[
|
||||
"icon-button",
|
||||
props.className ?? "",
|
||||
props.borderless ? "borderless" : "",
|
||||
].join(" ")}
|
||||
onClick={props.onClick}
|
||||
title={props.title}
|
||||
color={props.color}
|
||||
|
||||
@@ -23,6 +23,13 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
|
||||
update({ repeat: { ...state.repeat, count } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="While"
|
||||
value={state.repeat.while ?? []}
|
||||
onChange={(sequence) =>
|
||||
update({ repeat: { ...state.repeat, while: sequence } })
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Sequence"
|
||||
value={state.repeat.sequence ?? []}
|
||||
@@ -31,11 +38,9 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
|
||||
}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Condition"
|
||||
value={state.repeat.while ?? []}
|
||||
onChange={(sequence) =>
|
||||
update({ repeat: { ...state.repeat, sequence } })
|
||||
}
|
||||
label="Until"
|
||||
value={state.repeat.until ?? []}
|
||||
onChange={(until) => update({ repeat: { ...state.repeat, until } })}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { AutomationSequenceNode } from ".";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
import * as v from "types/validators/actions";
|
||||
import * as st from "superstruct";
|
||||
import { ScriptConditionField } from "./common";
|
||||
|
||||
type AutomationActionNodeBase<Data extends Object> = Data &
|
||||
st.Infer<typeof v.AutomationActionNodeBase>;
|
||||
@@ -10,7 +11,8 @@ export type ServiceAction = st.Infer<typeof v.ServiceAction>;
|
||||
export type RepeatAction = AutomationActionNodeBase<{
|
||||
repeat: {
|
||||
count?: number;
|
||||
while: AutomationCondition[];
|
||||
while?: ScriptConditionField;
|
||||
until?: ScriptConditionField;
|
||||
sequence: AutomationAction[];
|
||||
};
|
||||
}>;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as st from "superstruct";
|
||||
import * as v from "types/validators/common";
|
||||
import { AutomationCondition } from "./conditions";
|
||||
|
||||
export type AutomationTime = st.Infer<typeof v.AutomationTime>;
|
||||
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 AutomationDeviceState = st.Infer<typeof v.AutomationDeviceState>;
|
||||
export type DayOfWeek = st.Infer<typeof v.DayOfWeek>;
|
||||
export type ScriptConditionField = string | AutomationCondition[];
|
||||
@@ -1,5 +1,9 @@
|
||||
import * as st from "superstruct";
|
||||
import { AutomationDeviceState, AutomationTime } from "./common";
|
||||
import {
|
||||
AutomationDeviceState,
|
||||
AutomationTime,
|
||||
ScriptConditionField,
|
||||
} from "./common";
|
||||
import { AutomationTrigger } from "./triggers";
|
||||
|
||||
export const AutomationActionNodeBase = st.type({
|
||||
@@ -22,7 +26,8 @@ export const RepeatAction = st.intersection([
|
||||
st.type({
|
||||
repeat: st.object({
|
||||
count: st.optional(st.number()),
|
||||
while: st.array(st.object()),
|
||||
while: st.optional(ScriptConditionField),
|
||||
until: st.optional(ScriptConditionField),
|
||||
sequence: st.array(st.object()),
|
||||
}),
|
||||
}),
|
||||
|
||||
@@ -64,3 +64,7 @@ export const DayOfWeek = st.enums([
|
||||
"sat",
|
||||
"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 { getFailures } from "../types/validators/helper";
|
||||
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 { ScriptConditionField } from "types/automations/common";
|
||||
|
||||
export const getNodeTypeAndValidate = (
|
||||
node: AutomationNode,
|
||||
@@ -236,3 +240,15 @@ export const entityDomainsAreBinary = (domains: string[]): boolean => {
|
||||
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