From 1d7b6e96c13df867a7d17c2844221a5b69c30c25 Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 10 Jul 2022 13:12:37 -0400 Subject: [PATCH] resizeable collection nodes --- .../DAGGraph/elements/index.stories.tsx | 153 ++++++++++++++++++ .../src/components/DAGGraph/elements/index.ts | 51 ++++-- .../nodes/CollectionNode/CollectionNode.css | 23 ++- .../nodes/CollectionNode/CollectionNode.tsx | 46 +++--- 4 files changed, 234 insertions(+), 39 deletions(-) diff --git a/webapp/src/components/DAGGraph/elements/index.stories.tsx b/webapp/src/components/DAGGraph/elements/index.stories.tsx index 50baf8e..d2e0e59 100644 --- a/webapp/src/components/DAGGraph/elements/index.stories.tsx +++ b/webapp/src/components/DAGGraph/elements/index.stories.tsx @@ -121,3 +121,156 @@ Conditions.args = { sequence: [], }, }; + +export const FullAuto = Template.bind({}); +FullAuto.args = { + ...FullAuto.args, + automation: { + condition: [ + { + condition: "and", + conditions: [ + { + condition: "numeric_state", + entity_id: "sensor.temperature_kitchen", + below: "15", + }, + { + condition: "template", + value_template: 'states(switch.kitchen_light) == "on"', + }, + ], + }, + + { + condition: "numeric_state", + entity_id: "sensor.temperature_kitchen", + below: "15", + }, + ], + trigger: [ + { + platform: "numeric_state", + entity_id: "test", + }, + { + platform: "homeassistant", + event: "start", + }, + { + bad: "node", + } as any, + ], + sequence: [ + { + condition: "and", + conditions: [ + { + condition: "numeric_state", + entity_id: "sensor.temperature_kitchen", + below: "15", + }, + { + condition: "template", + value_template: 'states(switch.kitchen_light) == "on"', + }, + ], + }, + { + alias: "Start Music In Kitchen", + service: "media_player.play_media", + target: { + entity_id: "media_player.kitchen_dot", + }, + data: { + media_content_id: "Good Morning", + media_content_type: "SPOTIFY", + }, + }, + { + alias: "Bad Node", + }, + { + 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: {}, + }, + ], + }, + { + condition: "and", + conditions: [], + }, + ], + }, +}; diff --git a/webapp/src/components/DAGGraph/elements/index.ts b/webapp/src/components/DAGGraph/elements/index.ts index 241c27b..ff99c23 100644 --- a/webapp/src/components/DAGGraph/elements/index.ts +++ b/webapp/src/components/DAGGraph/elements/index.ts @@ -7,7 +7,10 @@ import { import { AutomationTrigger } from "types/automations/triggers"; import * as distance from "./distance"; import { getDescriptionFromAutomationNode } from "utils/formatting"; -import { AutomationActionData } from "types/automations"; +import { + AutomationActionData, + AutomationSequenceNode, +} from "types/automations"; import { AutomationCondition } from "types/automations/conditions"; import { CollectionNodeMaker } from "../nodes/CollectionNode"; @@ -26,7 +29,7 @@ export const makeAutomationNodes = ( nodeIndex: 0, }); - const condOut = makeConditionNodes(automation.condition, { + state = makeConditionNodes(automation.condition, { ...args, elementData: state.elementData, position: distance.moveFromTo( @@ -40,7 +43,21 @@ export const makeAutomationNodes = ( lastNodeId: state.lastNode.nodeId, }); - return condOut.elementData; + state = makeSequenceNodes(automation.sequence, { + ...args, + elementData: state.elementData, + position: distance.moveFromTo( + "condition", + "node", + state.lastNode.pos, + args.dims + ), + nodeId: `${args.dims.flipped}-sequence`, + nodeIndex: 2, + lastNodeId: state.lastNode.nodeId, + }); + + return state.elementData; }; export const makeTriggerNodes: ElementMaker = ( @@ -73,16 +90,7 @@ export const makeTriggerNodes: ElementMaker = ( export const makeConditionNodes: ElementMaker = ( nodes, - { - elementData, - nodeId, - stateUpdater, - position, - dims, - namer, - openModal, - lastNodeId, - } + { elementData, nodeId, stateUpdater, position, dims, namer, lastNodeId } ) => { elementData.nodes.push( CollectionNodeMaker.makeElement({ id: nodeId, position }, dims, { @@ -103,6 +111,7 @@ export const makeConditionNodes: ElementMaker = ( elementData.edges.push({ source: lastNodeId, target: elementData.nodes[elementData.nodes.length - 1].id, + animated: true, id: `${lastNodeId}->${ elementData.nodes[elementData.nodes.length - 1].id }`, @@ -113,7 +122,21 @@ export const makeConditionNodes: ElementMaker = ( lastNode: { nodeId, pos: elementData.nodes[elementData.nodes.length - 1].position, - size: dims.trigger, + size: dims.condition, + }, + }; +}; + +export const makeSequenceNodes: ElementMaker = ( + nodes, + { nodeId, elementData, dims } +) => { + return { + elementData, + lastNode: { + nodeId, + pos: elementData.nodes[elementData.nodes.length - 1].position, + size: dims.node, }, }; }; diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css index 40909e4..d0467bd 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.css @@ -1,11 +1,26 @@ -.collection-nodes { +.collection-nodes--wrap { --padding: 1.5em; - /* outline: 1px dotted white; */ + + min-width: var(--node-width); + min-height: var(--node-height); + max-width: var(--node-width); + max-height: var(--node-height); + + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; +} + +.collection-nodes { + position: relative; display: flex; flex-wrap: wrap; flex-direction: row; align-items: center; gap: 1.5em; + max-width: var(--node-width); + max-height: var(--node-height); border-color: var(--node-color); border-style: solid; @@ -13,4 +28,8 @@ border-radius: 10px; overflow: auto; padding: var(--padding); +} + +.collection-nodes .react-flow__handle { + opacity: 0; } \ No newline at end of file diff --git a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx index a999a68..ae8ca07 100644 --- a/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx +++ b/webapp/src/components/DAGGraph/nodes/CollectionNode/CollectionNode.tsx @@ -22,37 +22,37 @@ export const CollectionNode: FC = ({ return (
- {nodes.map((n, i) => ( - - ))} - } onClick={onAddNode} /> - - {hasInput && ( +
+ {nodes.map((n, i) => ( + + ))} + } onClick={onAddNode} /> - )} + {hasInput && ( + + )} +
); };