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;
|
||||
|
||||
@@ -46,6 +46,30 @@
|
||||
min-width: calc(max(var(--sequence-node-width) / 2, var(--sequence-node-height) / 2));
|
||||
}
|
||||
|
||||
.sequence-node.closed .sequence-node--label .icon {
|
||||
transform: rotate(180deg);
|
||||
color: var(--mui-primary-main);
|
||||
}
|
||||
|
||||
.sequence-node.closed:hover .sequence-node--label .icon, .sequence-node.open:hover .sequence-node--label .icon {
|
||||
outline: 1px solid var(--mui-secondary-main);
|
||||
color: var(--mui-secondary-main);
|
||||
border-radius: 50px;
|
||||
transform: scale(0.95, 0.95);
|
||||
}
|
||||
|
||||
.sequence-node.open:hover .sequence-node--label .icon {
|
||||
transform: rotate(-10deg);
|
||||
}
|
||||
|
||||
.sequence-node.closed:hover .sequence-node--label .icon {
|
||||
transform: rotate(190deg);
|
||||
}
|
||||
|
||||
.sequence-node.closed .sequence-node--label, .sequence-node.open .sequence-node--label {
|
||||
cursor: pointer;
|
||||
|
||||
}
|
||||
|
||||
.sequence-node--label {
|
||||
flex: 1;
|
||||
|
||||
@@ -4,20 +4,18 @@ import { FC, PropsWithChildren } from "react";
|
||||
import { useConfirm } from "material-ui-confirm";
|
||||
import { useSnackbar } from "notistack";
|
||||
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
||||
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
|
||||
import SettingsOutlinedIcon from "@mui/icons-material/SettingsOutlined";
|
||||
import Edit from "@mui/icons-material/Edit";
|
||||
import Add from "@mui/icons-material/Add";
|
||||
import KeyboardArrowDownSharpIcon from "@mui/icons-material/KeyboardArrowDownSharp";
|
||||
import AddOutlined from "@mui/icons-material/AddOutlined";
|
||||
|
||||
import InputBoolean from "components/Inputs/InputBoolean";
|
||||
import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { SpeedDial } from "components/SpeedDial";
|
||||
import { SequenceNodeElementProps } from "./types";
|
||||
import { useSequenceNodeColor } from "./util";
|
||||
import { AddOutlined } from "@mui/icons-material";
|
||||
|
||||
export const SequenceNodeElement: FC<
|
||||
PropsWithChildren<SequenceNodeElementProps>
|
||||
@@ -34,6 +32,8 @@ export const SequenceNodeElement: FC<
|
||||
enabled,
|
||||
onSetEnabled,
|
||||
children,
|
||||
isClosed,
|
||||
setIsClosed,
|
||||
}) => {
|
||||
const confirm = useConfirm();
|
||||
const snackbr = useSnackbar();
|
||||
@@ -45,6 +45,7 @@ export const SequenceNodeElement: FC<
|
||||
"sequence-node",
|
||||
flipped ? "flipped" : "",
|
||||
enabled ? "enabled" : "disabled",
|
||||
isClosed === undefined ? "" : isClosed ? "closed" : "open",
|
||||
].join(" ")}
|
||||
style={
|
||||
{
|
||||
@@ -118,8 +119,12 @@ export const SequenceNodeElement: FC<
|
||||
/>
|
||||
)}
|
||||
{children}
|
||||
<div className="sequence-node--label">
|
||||
<div
|
||||
className="sequence-node--label"
|
||||
onClick={() => setIsClosed && setIsClosed(!isClosed)}
|
||||
>
|
||||
{typeof label === "string" ? <span>{label}</span> : label}
|
||||
{!!setIsClosed && <KeyboardArrowDownSharpIcon className="icon" />}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -12,6 +12,7 @@ export type SequenceNodeActions = {
|
||||
onAddNode: () => void;
|
||||
onMove: SequenceNodeOnMoveEvents;
|
||||
onAdd: SequenceNodeOnMoveEvents;
|
||||
setIsClosed: (isClosed: boolean) => void;
|
||||
};
|
||||
export type SequenceNodeOnMoveEvents = Partial<
|
||||
SequenceNodeOnMove<"up" | "down" | "left" | "right">
|
||||
@@ -20,6 +21,7 @@ export type SequenceNodeBaseProps = Partial<SequenceNodeActions> & {
|
||||
color: SequenceNodeColor;
|
||||
label: ReactNode;
|
||||
enabled: boolean;
|
||||
isClosed?: boolean;
|
||||
};
|
||||
export type SequenceNodeElementProps = SequenceNodeBaseProps & {
|
||||
height: number;
|
||||
|
||||
Reference in new issue
Block a user