refactor of graph pt2

This commit is contained in:
ari committed 2022-07-11 17:12:49 -04:00
1 parent 730c804c22
commit 2cb2e779d7
20 files changed
+714 -246

No files matched your search

@@ -23,7 +23,7 @@ export const DEFAULT_DIMS: DAGDims = {
},
distanceFactor: {
node: 1.5,
collection: 1.25,
collection: 1,
},
position: {
x: 50,
@@ -5,7 +5,8 @@ export const moveAlong = (
by: keyof DAGEntities,
startPoint: XYPosition,
increment: number,
dims: DAGDims
dims: DAGDims,
reversed: boolean = false
): XYPosition => {
const moveOnY = {
y: startPoint.y + dims[by].height * dims.distanceFactor.node * increment,
@@ -15,6 +16,9 @@ export const moveAlong = (
x: startPoint.x + dims[by].width * dims.distanceFactor.node * increment,
y: startPoint.y,
};
if (reversed) {
return dims.flipped ? moveOnX : moveOnY;
}
return !dims.flipped ? moveOnX : moveOnY;
};
@@ -2,8 +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 { AutomationTrigger } from "types/automations/triggers";
import { makeTriggerNodes, makeAutomationNodes } from ".";
import { makeAutomationNodes } from "./index";
import { useHA } from "haService";
import { AutomationActionData } from "types/automations";
import { createUpdaterFromAutomationData } from "../updater";
@@ -274,3 +273,97 @@ FullAuto.args = {
],
},
};
export const ChooseAuto = Template.bind({});
ChooseAuto.args = {
...ChooseAuto.args,
automation: {
condition: [],
trigger: [],
sequence: [
{
choose: [
{
conditions: [
{
condition: "state",
entity_id: "lights.bathroom",
state: "off",
},
],
sequence: [
{
service: "light.turn_on",
target: {
entity_id: "lights.bathroom",
},
data: {},
},
{
alias: "Turn off bedroom",
service: "light.turn_off",
target: {
entity_id: "lights.bedroom",
},
data: {},
},
],
},
{
conditions: [
{
condition: "state",
entity_id: "lights.bedroom",
state: "off",
},
],
sequence: [
{
alias: "Turn on bedroom",
service: "light.turn_on",
target: {
entity_id: "lights.bedroom",
},
data: {},
},
{
choose: [
{
conditions: [],
sequence: [
{
choose: [],
default: [],
},
],
},
],
default: [
{
choose: [],
default: [],
},
],
},
],
},
],
default: [
{
service: "light.turn_off",
target: {
entity_id: "lights.bathroom",
},
data: {},
},
],
},
{
service: "switch.turn_on",
target: {
entity_id: "switch.light_balcony",
},
},
],
},
};
@@ -1,194 +0,0 @@
import {
Bbox,
DAGDims,
ElementMaker,
ElementMakerBaseProps,
ElementMakerOutput,
LastNode,
} from "./types";
import { AutomationTrigger } from "types/automations/triggers";
import * as distance from "./distance";
import { getDescriptionFromAutomationNode } from "utils/formatting";
import {
AutomationActionData,
AutomationSequenceNode,
} from "types/automations";
import { AutomationCondition } from "types/automations/conditions";
import { CollectionNodeMaker } from "../nodes/CollectionNode";
import { SequenceNodeMaker } from "../nodes/SequenceNode";
import { getNodeType } from "utils/automations";
export const makeAutomationNodes = (
automation: AutomationActionData,
args: ElementMakerBaseProps
) => {
const dims: DAGDims = {
...args.dims,
// think of a better to do this
// condition: args.dims.flipped
// ? args.dims.condition
// : {
// height: args.dims.condition.width,
// width: args.dims.condition.height,
// },
// trigger: args.dims.flipped
// ? args.dims.trigger
// : {
// height: args.dims.trigger.width,
// width: args.dims.trigger.height,
// },
};
let state: ElementMakerOutput = makeTriggerNodes(automation.trigger, {
...args,
dims,
elementData: {
nodes: [],
edges: [],
},
position: dims.position,
nodeId: `${dims.flipped}-trigger`,
nodeIndex: 0,
});
state = makeConditionNodes(automation.condition, {
...args,
dims,
elementData: state.elementData,
position: distance.moveFromTo(
"trigger",
"condition",
state.lastNode.pos,
dims
),
nodeId: `${dims.flipped}-condition`,
nodeIndex: 1,
lastNodeId: state.lastNode.nodeId,
});
state = makeSequenceNodes(automation.sequence, {
...args,
dims,
elementData: state.elementData,
position: distance.moveFromTo(
"condition",
"node",
state.lastNode.pos,
dims
),
nodeId: `${dims.flipped}-sequence`,
nodeIndex: 2,
lastNodeId: state.lastNode.nodeId,
});
return state.elementData;
};
export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
nodes,
{ elementData, dims, namer, openModal, stateUpdater, nodeId, position }
) => {
elementData.nodes.push(
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
...dims.trigger,
color: "red",
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("trigger", index, {}),
})),
})
);
return {
elementData,
lastNode: {
nodeId,
pos: elementData.nodes[elementData.nodes.length - 1].position,
size: dims.trigger,
},
};
};
export const makeConditionNodes: ElementMaker<AutomationCondition> = (
nodes,
{ elementData, nodeId, stateUpdater, position, dims, namer, lastNodeId }
) => {
elementData.nodes.push(
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
...dims.condition,
color: "blue",
hasInput: !!lastNodeId,
onAddNode: () => stateUpdater.basic.condition.addNode(null),
nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("condition", index, {}),
})),
})
);
if (lastNodeId) {
elementData.edges.push({
source: lastNodeId,
target: elementData.nodes[elementData.nodes.length - 1].id,
animated: true,
id: `${lastNodeId}->${
elementData.nodes[elementData.nodes.length - 1].id
}`,
});
}
return {
elementData,
lastNode: {
nodeId,
pos: elementData.nodes[elementData.nodes.length - 1].position,
size: dims.condition,
},
};
};
export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
nodes,
{ nodeId, lastNodeId, elementData, dims, position, namer, stateUpdater }
) => {
let lastNode: LastNode = {
nodeId: lastNodeId ?? nodeId,
pos: elementData.nodes[elementData.nodes.length - 1].position,
size: dims.node,
};
nodes.forEach((node, nodeIndex) => {
const element = SequenceNodeMaker.makeElement(
{
// the JSON.stringify is hack to make the edges render nicely
id: `${nodeId}-${nodeIndex}-${JSON.stringify(node)}`,
position: distance.moveAlong("node", position, nodeIndex, dims),
},
dims,
{
color: getNodeType(node) === "action" ? "green" : "blue",
enabled: node.enabled ?? true,
hasInput: true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("sequence", nodeIndex, {
includeAdd: true,
flipped: dims.flipped,
}),
}
);
elementData.nodes.push(element);
elementData.edges.push({
id: `${lastNode.nodeId}->${element.id}`,
target: element.id,
source: lastNode.nodeId,
});
lastNode = {
nodeId: element.id,
pos: element.position,
size: dims.node,
};
});
return {
elementData,
lastNode,
};
};
@@ -0,0 +1,286 @@
import {
DAGDims,
ElementData,
ElementMaker,
ElementMakerBaseProps,
LastNode,
} from "./types";
import { AutomationTrigger } from "types/automations/triggers";
import * as distance from "./distance";
import { getDescriptionFromAutomationNode } from "utils/formatting";
import {
AutomationActionData,
AutomationSequenceNode,
} from "types/automations";
import { AutomationCondition } from "types/automations/conditions";
import { CollectionNodeMaker } from "../nodes/CollectionNode";
import { ChooseAction } from "types/automations/actions";
import { XYPosition } from "react-flow-renderer";
import { DAGElementsState } from "./state";
import { SequenceNodeMaker } from "../nodes/SequenceNode";
import { getNodeType } from "utils/automations";
import { pointApply } from "./math";
import { ButtonNodeMaker } from "../nodes/ButtonNode";
import { AddIcon } from "components/Icons";
export const makeAutomationNodes = (
automation: AutomationActionData,
args: ElementMakerBaseProps
) => {
const dims: DAGDims = {
...args.dims,
// think of a better to do this
// condition: args.dims.flipped
// ? args.dims.condition
// : {
// height: args.dims.condition.width,
// width: args.dims.condition.height,
// },
// trigger: args.dims.flipped
// ? args.dims.trigger
// : {
// height: args.dims.trigger.width,
// width: args.dims.trigger.height,
// },
};
const state = new DAGElementsState();
state.extend(
makeTriggerNodes(automation.trigger, {
...args,
dims,
position: dims.position,
nodeId: `${dims.flipped}-trigger`,
nodeIndex: 0,
})
);
state.extend(
makeConditionNodes(automation.condition, {
...args,
dims,
position: distance.moveFromTo(
"trigger",
"condition",
state.lastNodePos,
dims
),
nodeId: `${dims.flipped}-condition`,
nodeIndex: 1,
lastNodeId: state.lastNodeId,
})
);
state.extend(
makeSequenceNodes(automation.sequence, {
...args,
dims,
position: distance.moveFromTo(
"condition",
"node",
state.lastNodePos,
dims
),
nodeId: `${dims.flipped}-sequence`,
nodeIndex: 2,
lastNodeId: state.lastNodeId,
})
);
return state.data;
};
export const makeTriggerNodes: ElementMaker<AutomationTrigger> = (
nodes,
{ dims, namer, stateUpdater, nodeId, position }
) => {
const state = new DAGElementsState();
state.addNode(
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
...dims.trigger,
color: "red",
onAddNode: () => stateUpdater.basic.trigger.addNode(null),
nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("trigger", index, {}),
})),
})
);
return state;
};
export const makeConditionNodes: ElementMaker<AutomationCondition> = (
nodes,
{ nodeId, stateUpdater, position, dims, namer, lastNodeId }
) => {
const state = new DAGElementsState();
state.addNode(
CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, {
...dims.condition,
color: "blue",
hasInput: !!lastNodeId,
onAddNode: () => stateUpdater.basic.condition.addNode(null),
nodes: nodes.map((node, index) => ({
enabled: node.enabled ?? true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("condition", index, {}),
})),
})
);
if (lastNodeId && state.lastNodeId) {
state.addEdge(lastNodeId, state.lastNodeId, true);
}
return state;
};
export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
nodes,
args
) => {
const state = new DAGElementsState();
const { lastNodeId, nodeId, position, dims, namer, stateUpdater } = args;
nodes.forEach((node, nodeIndex) => {
const element = state.addNode(
SequenceNodeMaker.makeElement(
{
// the JSON.stringify is hack to make the edges render nicely
id: `${nodeId}-${args.nodeIndex}-${nodeIndex}-${JSON.stringify(
node
)}`,
position: distance.moveAlong("node", position, nodeIndex, dims),
},
dims,
{
color: getNodeType(node) === "action" ? "green" : "blue",
enabled: node.enabled ?? true,
hasInput: true,
label: getDescriptionFromAutomationNode(node, namer, true),
...stateUpdater.createNodeActions("sequence", nodeIndex, {
includeAdd: true,
flipped: dims.flipped,
}),
}
),
true
);
if (nodeIndex === 0 && lastNodeId) {
state.addEdge(lastNodeId, element.id);
}
if ("choose" in node) {
state.extend(
makeChooseeNodes([node], {
...args,
lastNodeId: element.id,
position: state.lastNodePos,
nodeIndex,
}),
true
);
}
});
const addElement = state.addNode(
ButtonNodeMaker.makeElement(
{
id: `${nodeId}-add-new`,
position: distance.moveAlong("node", position, nodes.length, dims),
},
dims,
{
icon: <AddIcon />,
onClick: () => stateUpdater.basic.sequence.addNode(null),
}
),
true
);
if (nodes.length === 0 && lastNodeId) {
state.addEdge(lastNodeId, addElement.id);
}
return state;
};
export const makeChooseeNodes: ElementMaker<ChooseAction> = (nodes, args) => {
const state = new DAGElementsState();
nodes.forEach((node, nodeIndex) => {
if (node.enabled ?? true) {
// offset position
let offsetPos: XYPosition = {
x:
args.position.x +
args.dims.distanceFactor.node *
args.dims.node.width *
(nodeIndex + 1),
y:
args.position.y +
args.dims.distanceFactor.node *
args.dims.node.height *
(nodeIndex + 1),
};
// functions
const processSequence = (
sequence: AutomationSequenceNode[],
index: number,
condition: AutomationCondition[] | null = null
) => {
let pos = offsetPos;
let lastNodeId = args.lastNodeId;
// if (condition !== null) {
// const conditionState = makeConditionNodes(condition, {
// ...args,
// position: pos,
// lastNodeId,
// nodeIndex: index,
// nodeId: `${args.nodeId}-choose-${index}-condition`,
// });
// // pos.y = conditionState.bbox[1].y;
// state.extend(conditionState);
// if (lastNodeId && conditionState.data.nodes.length > 0) {
// state.addEdge(lastNodeId, conditionState.data.nodes[0].id, true);
// }
// lastNodeId = conditionState.lastNodeId ?? lastNodeId;
// pos = {
// y: conditionState.bbox[1].y,
// x: pos.x,
// };
// }
const output = makeSequenceNodes(sequence, {
...args,
position: pos,
lastNodeId: undefined,
nodeIndex: index,
nodeId: `${args.nodeId}-choose-${index}-sequence`,
});
if (lastNodeId && output.data.nodes.length > 0) {
state.addEdge(lastNodeId, output.data.nodes[0].id, true);
}
offsetPos = args.dims.flipped
? {
x:
output.bbox[1].x +
(args.dims.distanceFactor.node - 1) * args.dims.node.width,
y: offsetPos.y,
}
: {
y:
output.bbox[1].y +
(args.dims.distanceFactor.node - 1) * args.dims.node.height,
x: offsetPos.x,
};
state.extend(output);
};
// process options
for (let sIndex = 0; sIndex < node.choose.length; sIndex++) {
const subSequence = node.choose[sIndex];
processSequence(subSequence.sequence, sIndex, subSequence.conditions);
}
// else
processSequence(node.default ?? [], node.choose.length);
}
});
return state;
};
@@ -0,0 +1,74 @@
import { DAGAutomationFlowNode } from "../nodes";
import { addSize, pointApply } from "./math";
import { Bbox, ElementData, LastNode } from "./types";
export class DAGElementsState {
public data: ElementData = {
nodes: [],
edges: [],
};
public lastNode?: LastNode;
public bbox: Bbox = [
{ x: 0, y: 0 },
{ x: 0, y: 0 },
];
public extend(other: DAGElementsState, ignoreLastNode: boolean = false) {
this.data.nodes = this.data.nodes.concat(other.data.nodes);
this.data.edges = this.data.edges.concat(other.data.edges);
this.bbox[0] = pointApply(this.bbox[0], other.bbox[0], Math.min);
this.bbox[1] = pointApply(this.bbox[1], other.bbox[1], Math.max);
if (!ignoreLastNode) {
this.lastNode = other.lastNode;
}
}
public addNode(
node: DAGAutomationFlowNode,
addEdge: boolean = false,
animatedEdge: boolean = false
) {
if (addEdge && this.lastNodeId) {
this.addEdge(this.lastNodeId, node.id, animatedEdge);
}
this.data.nodes.push(node);
this.bbox[0] = pointApply(this.bbox[0], node.position, Math.min);
this.bbox[1] = pointApply(
this.bbox[1],
addSize(node.position, node.data),
Math.max
);
this.lastNode = {
nodeId: node.id,
pos: node.position,
size: {
height: node.data.height,
width: node.data.width,
},
};
return node;
}
public addEdge(from: string, to: string, animated: boolean = false) {
this.data.edges.push({
id: `${from}->${to}`,
source: from,
target: to,
animated,
});
}
public get lastNodeId() {
if (this.lastNode) {
return this.lastNode.nodeId;
}
return undefined;
}
public get lastNodePos() {
if (this.lastNode) {
return this.lastNode.pos;
}
throw Error("missing lastnode");
}
}
@@ -4,6 +4,7 @@ import { Namer } from "utils/formatting";
import { ModalState } from "../board/types";
import { DAGAutomationFlowNode } from "../nodes";
import { DAGGraphUpdater } from "../updater";
import { DAGElementsState } from "./state";
export type Bbox = [XYPosition, XYPosition];
export type Size = { height: number; width: number };
@@ -33,7 +34,6 @@ export type ElementMakerBaseProps = {
openModal: (m: ModalState) => void;
};
export type ElementMakerProps = ElementMakerBaseProps & {
elementData: ElementData;
nodeId: string;
nodeIndex: number;
position: XYPosition;
@@ -44,12 +44,7 @@ export type LastNode = {
pos: XYPosition;
size: Size;
};
export type ElementMakerOutput = {
elementData: ElementData;
lastNode: LastNode;
};
export type ElementMaker<A extends AutomationNode = AutomationNode> = (
haNodes: A[],
args: ElementMakerProps
) => ElementMakerOutput;
) => DAGElementsState;