expandable choose node
This commit is contained in:
1 parent
0b381a6e50
commit
befe293f1c
7 files changed
+128
-46
No files matched your search
@@ -2,7 +2,7 @@ import React, { FC, useState } from "react";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { DAGGraphBoard } from "components/DAGGraph/board";
|
||||
import { makeAutomationNodes } from "./index";
|
||||
import { useAutomationNodes } from "./index";
|
||||
import { useHA } from "haService";
|
||||
import { AutomationActionData } from "types/automations";
|
||||
import { createUpdaterFromAutomationData } from "../updater";
|
||||
@@ -22,7 +22,7 @@ const Demo: FC<{
|
||||
state,
|
||||
setState
|
||||
);
|
||||
const elementData = makeAutomationNodes(state, {
|
||||
const elementData = useAutomationNodes(state, {
|
||||
dims: {
|
||||
...DEFAULT_DIMS,
|
||||
flipped: props.flipped,
|
||||
|
||||
@@ -21,18 +21,21 @@ import { SequenceNodeMaker } from "../nodes/SequenceNode";
|
||||
import { getNodeType } from "utils/automations";
|
||||
import { ButtonNodeMaker } from "../nodes/ButtonNode";
|
||||
import { AddIcon } from "components/Icons";
|
||||
import { useDAGElementsState } from "./state";
|
||||
|
||||
export const makeAutomationNodes = (
|
||||
export const useAutomationNodes = (
|
||||
automation: AutomationActionData,
|
||||
args: ElementMakerBaseProps
|
||||
) => {
|
||||
const dims: DAGDims = {
|
||||
...args.dims,
|
||||
};
|
||||
const state = new DAGElementsOutputState();
|
||||
state.extend(
|
||||
const state = useDAGElementsState();
|
||||
const outputState = new DAGElementsOutputState();
|
||||
outputState.extend(
|
||||
makeTriggerNodes(automation.trigger, {
|
||||
...args,
|
||||
state,
|
||||
dims,
|
||||
position: dims.position,
|
||||
nodeId: `${dims.flipped}-trigger`,
|
||||
@@ -40,47 +43,49 @@ export const makeAutomationNodes = (
|
||||
})
|
||||
);
|
||||
|
||||
state.extend(
|
||||
outputState.extend(
|
||||
makeConditionNodes(automation.condition, {
|
||||
...args,
|
||||
state,
|
||||
dims,
|
||||
position: distance.moveFromTo(
|
||||
"trigger",
|
||||
"condition",
|
||||
state.lastNodePos,
|
||||
outputState.lastNodePos,
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-condition`,
|
||||
nodeIndex: 1,
|
||||
lastNodeId: state.lastNodeId,
|
||||
lastNodeId: outputState.lastNodeId,
|
||||
})
|
||||
);
|
||||
|
||||
state.extend(
|
||||
outputState.extend(
|
||||
makeSequenceNodes(automation.sequence, {
|
||||
...args,
|
||||
state,
|
||||
dims,
|
||||
position: distance.moveFromTo(
|
||||
"condition",
|
||||
"node",
|
||||
state.lastNodePos,
|
||||
outputState.lastNodePos,
|
||||
dims
|
||||
),
|
||||
nodeId: `${dims.flipped}-sequence`,
|
||||
nodeIndex: 2,
|
||||
lastNodeId: state.lastNodeId,
|
||||
lastNodeId: outputState.lastNodeId,
|
||||
})
|
||||
);
|
||||
|
||||
return state.data;
|
||||
return outputState.data;
|
||||
};
|
||||
|
||||
export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
||||
nodes,
|
||||
{ dims, namer, stateUpdater, nodeId, position }
|
||||
) => {
|
||||
const state = new DAGElementsOutputState();
|
||||
state.addNode(
|
||||
const outputState = new DAGElementsOutputState();
|
||||
outputState.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.trigger,
|
||||
collectionType: "trigger",
|
||||
@@ -92,15 +97,15 @@ export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
|
||||
})),
|
||||
})
|
||||
);
|
||||
return state;
|
||||
return outputState;
|
||||
};
|
||||
|
||||
export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
nodes,
|
||||
{ nodeId, stateUpdater, position, dims, namer, lastNodeId }
|
||||
) => {
|
||||
const state = new DAGElementsOutputState();
|
||||
state.addNode(
|
||||
const outputState = new DAGElementsOutputState();
|
||||
outputState.addNode(
|
||||
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
|
||||
...dims.condition,
|
||||
collectionType: "condition",
|
||||
@@ -113,27 +118,27 @@ export const makeConditionNodes: ElementMaker<AutomationCondition> = (
|
||||
})),
|
||||
})
|
||||
);
|
||||
console.log("pre", { lastNodeId, stateLast: state.lastNodeId });
|
||||
if (lastNodeId && state.lastNodeId) {
|
||||
state.addEdge(lastNodeId, state.lastNodeId, true);
|
||||
if (lastNodeId && outputState.lastNodeId) {
|
||||
outputState.addEdge(lastNodeId, outputState.lastNodeId, true);
|
||||
}
|
||||
return state;
|
||||
return outputState;
|
||||
};
|
||||
|
||||
export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
nodes,
|
||||
args
|
||||
) => {
|
||||
const state = new DAGElementsOutputState();
|
||||
const { lastNodeId, nodeId, position, dims, namer, stateUpdater } = args;
|
||||
const outputState = new DAGElementsOutputState();
|
||||
const { lastNodeId, position, dims, namer, stateUpdater, state } = args;
|
||||
nodes.forEach((node, nodeIndex) => {
|
||||
const element = state.addNode(
|
||||
// the JSON.stringify is hack to make the edges render nicely
|
||||
const nodeId = `${args.nodeId}-${
|
||||
args.nodeIndex
|
||||
}-${nodeIndex}-${JSON.stringify(node)}`;
|
||||
const element = outputState.addNode(
|
||||
SequenceNodeMaker.makeElement(
|
||||
{
|
||||
// the JSON.stringify is hack to make the edges render nicely
|
||||
id: `${nodeId}-${args.nodeIndex}-${nodeIndex}-${JSON.stringify(
|
||||
node
|
||||
)}`,
|
||||
id: nodeId,
|
||||
position: distance.moveAlong("node", position, nodeIndex, dims),
|
||||
},
|
||||
dims,
|
||||
@@ -142,6 +147,7 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
enabled: node.enabled ?? true,
|
||||
hasInput: true,
|
||||
label: getDescriptionFromAutomationNode(node, namer, true),
|
||||
...state.getIsClosedActions(nodeId, node),
|
||||
...stateUpdater.createNodeActions("sequence", nodeIndex, {
|
||||
includeAdd: true,
|
||||
flipped: dims.flipped,
|
||||
@@ -151,14 +157,14 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
true
|
||||
);
|
||||
if (nodeIndex === 0 && lastNodeId) {
|
||||
state.addEdge(lastNodeId, element.id);
|
||||
outputState.addEdge(lastNodeId, element.id);
|
||||
}
|
||||
if ("choose" in node) {
|
||||
state.extend(
|
||||
outputState.extend(
|
||||
makeChooseeNodes([node], {
|
||||
...args,
|
||||
lastNodeId: element.id,
|
||||
position: state.lastNodePos,
|
||||
position: outputState.lastNodePos,
|
||||
nodeIndex,
|
||||
}),
|
||||
true
|
||||
@@ -166,10 +172,10 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
}
|
||||
});
|
||||
|
||||
const addElement = state.addNode(
|
||||
const addElement = outputState.addNode(
|
||||
ButtonNodeMaker.makeElement(
|
||||
{
|
||||
id: `${nodeId}-add-new`,
|
||||
id: `${args.nodeId}-add-new`,
|
||||
position: distance.moveAlong("node", position, nodes.length, dims),
|
||||
},
|
||||
dims,
|
||||
@@ -182,19 +188,22 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
||||
);
|
||||
|
||||
if (nodes.length === 0 && lastNodeId) {
|
||||
state.addEdge(lastNodeId, addElement.id);
|
||||
outputState.addEdge(lastNodeId, addElement.id);
|
||||
}
|
||||
|
||||
return state;
|
||||
return outputState;
|
||||
};
|
||||
|
||||
export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
const state = new DAGElementsOutputState();
|
||||
const outputState = new DAGElementsOutputState();
|
||||
if (nodes.length !== 1) {
|
||||
throw new Error("makeChooseNodes only access a list of 1");
|
||||
}
|
||||
const node = nodes[0];
|
||||
if (node.enabled ?? true) {
|
||||
if (
|
||||
(node.enabled ?? true) &&
|
||||
!args.state.get(args.lastNodeId ?? "").isClosed
|
||||
) {
|
||||
// offset position
|
||||
let offsetPos: XYPosition = {
|
||||
x: args.position.x + args.dims.distanceFactor.node * args.dims.node.width,
|
||||
@@ -219,7 +228,7 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
nodeId: `${args.nodeId}-choose-${j}-condition`,
|
||||
});
|
||||
conditionState.data.edges[0].label = `Condition #${j}`;
|
||||
state.extend(conditionState);
|
||||
outputState.extend(conditionState);
|
||||
pos = distance.moveFromTo("condition", "node", pos, args.dims);
|
||||
lastNodeId = conditionState.lastNodeId;
|
||||
}
|
||||
@@ -234,7 +243,7 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
nodeId: `${args.nodeId}-choose-${j}-sequence`,
|
||||
});
|
||||
if (lastNodeId && output.data.nodes.length > 0) {
|
||||
state.addEdge(
|
||||
outputState.addEdge(
|
||||
lastNodeId,
|
||||
output.data.nodes[0].id,
|
||||
true,
|
||||
@@ -247,7 +256,7 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
args.dims,
|
||||
j === "else" ? "node" : "condition"
|
||||
);
|
||||
state.extend(output);
|
||||
outputState.extend(output);
|
||||
};
|
||||
|
||||
// process options
|
||||
@@ -258,5 +267,5 @@ export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
|
||||
processSequence("else");
|
||||
}
|
||||
|
||||
return state;
|
||||
return outputState;
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import { useState } from "react";
|
||||
import { AutomationNode } from "types/automations";
|
||||
|
||||
export type DAGNodeState = Partial<{
|
||||
isClosed: boolean;
|
||||
}>;
|
||||
export type DAGElementsState = ReturnType<typeof useDAGElementsState>;
|
||||
export const useDAGElementsState = () => {
|
||||
const [data, setData] = useState<Record<string, DAGNodeState>>({});
|
||||
console.log(data);
|
||||
return {
|
||||
get(nodeId: string): DAGNodeState {
|
||||
return data[nodeId] ?? {};
|
||||
},
|
||||
|
||||
set<K extends keyof DAGNodeState>(
|
||||
nodeId: string,
|
||||
key: K,
|
||||
value: DAGNodeState[K]
|
||||
) {
|
||||
setData({
|
||||
...data,
|
||||
[nodeId]: {
|
||||
...(data[nodeId] ?? {}),
|
||||
[key]: value,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
getIsClosedActions(nodeId: string, node: AutomationNode) {
|
||||
if ("choose" in node) {
|
||||
return {
|
||||
isClosed: this.get(nodeId).isClosed ?? false,
|
||||
setIsClosed: (v: boolean) => this.set(nodeId, "isClosed", v),
|
||||
};
|
||||
}
|
||||
return {};
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -5,6 +5,7 @@ import { ModalState } from "../board/types";
|
||||
import { DAGAutomationFlowNode } from "../nodes";
|
||||
import { DAGGraphUpdater } from "../updater";
|
||||
import { DAGElementsOutputState } from "./outputState";
|
||||
import { DAGElementsState } from "./state";
|
||||
|
||||
export type Bbox = [XYPosition, XYPosition];
|
||||
export type Size = { height: number; width: number };
|
||||
@@ -38,6 +39,7 @@ export type ElementMakerProps = ElementMakerBaseProps & {
|
||||
nodeIndex: number;
|
||||
position: XYPosition;
|
||||
lastNodeId?: string;
|
||||
state: DAGElementsState;
|
||||
};
|
||||
export type LastNode = {
|
||||
nodeId: string;
|
||||
|
||||
Reference in new issue
Block a user