From bbb87ebf9e87fd65c28d4fa8dd2c611c0cee20a5 Mon Sep 17 00:00:00 2001 From: Ari Date: Fri, 15 Jul 2022 23:41:14 -0400 Subject: [PATCH] support for if/else --- .../DAGGraph/elements/index.stories.tsx | 16 ++++++++++++++++ .../components/DAGGraph/elements/index.tsx | 6 +++--- .../DAGGraph/elements/specialnodes.ts | 4 ++++ .../src/components/DAGGraph/elements/state.ts | 4 +++- .../src/components/DAGGraph/updater/index.ts | 9 +++++++-- .../src/components/DAGGraph/updater/util.ts | 19 +++++++++++++++++++ webapp/src/components/NodeEditor/index.tsx | 4 +++- webapp/src/components/NodeEditor/utils.ts | 17 +++++++++++++++++ webapp/src/types/automations/actions.ts | 8 +++++++- webapp/src/types/validators/actions.ts | 13 ++++++++++++- webapp/src/utils/automations.ts | 2 +- webapp/src/utils/formatting.tsx | 2 +- 12 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 webapp/src/components/NodeEditor/utils.ts diff --git a/webapp/src/components/DAGGraph/elements/index.stories.tsx b/webapp/src/components/DAGGraph/elements/index.stories.tsx index 926b203..e2e14bc 100644 --- a/webapp/src/components/DAGGraph/elements/index.stories.tsx +++ b/webapp/src/components/DAGGraph/elements/index.stories.tsx @@ -501,3 +501,19 @@ BadNodes.args = { ] as any, }, }; + +export const IfElseNode = Template.bind({}); +IfElseNode.args = { + ...BadNodes.args, + automation: { + condition: [], + trigger: [], + sequence: [ + { + if: [], + else: [], + then: [], + }, + ], + }, +}; diff --git a/webapp/src/components/DAGGraph/elements/index.tsx b/webapp/src/components/DAGGraph/elements/index.tsx index 43628ee..90c39d2 100644 --- a/webapp/src/components/DAGGraph/elements/index.tsx +++ b/webapp/src/components/DAGGraph/elements/index.tsx @@ -26,8 +26,8 @@ import { getNodeSubType } from "../../../utils/automations"; import DeleteForeverIcon from "@mui/icons-material/DeleteForever"; import Add from "@mui/icons-material/Add"; import { SequenceNodeDataProps } from "../nodes/SequenceNode/types"; -import InfoOutlined from "@mui/icons-material/InfoOutlined"; import { convertFailuresToSequenceNodeDataProps } from "./util"; +import { convertChooseActionToOrignal } from "../updater/util"; export const useAutomationNodes = ( automation: AutomationActionData, @@ -242,7 +242,8 @@ export const makeSequenceNodes: ElementMaker = ( export const makeChooseeNodes = makeSpecialNodeMaker( "choose", - (node, outputState, args) => { + (incmNode, outputState, args) => { + const node = convertChooseActionToOrignal(incmNode); // offset position let offsetPos: XYPosition = distance.offsetBy( "node", @@ -377,7 +378,6 @@ export const makeChooseeNodes = makeSpecialNodeMaker( args.dims, true ); - console.log({ addPos, lastConditionState }); if (lastConditionState !== null) { addPos = distance.moveFromTo( "collection", diff --git a/webapp/src/components/DAGGraph/elements/specialnodes.ts b/webapp/src/components/DAGGraph/elements/specialnodes.ts index b320665..b42049e 100644 --- a/webapp/src/components/DAGGraph/elements/specialnodes.ts +++ b/webapp/src/components/DAGGraph/elements/specialnodes.ts @@ -1,3 +1,4 @@ +import { AutomationNodeSubtype } from "types/automations"; import { ActionType, ActionTypeMapping } from "types/automations/actions"; import { DAGElementsOutputState } from "./outputState"; import { ElementMaker, ElementMakerProps } from "./types"; @@ -41,3 +42,6 @@ export const makeSpecialNodeMaker = < export const getSpecialNodeMaker = ( nodeType: AT ): ElementMaker | null => SpecialNodeRegistry[nodeType] ?? null; + +export const isSpecialNode = (nodeType: AutomationNodeSubtype) => + nodeType in SpecialNodeRegistry; diff --git a/webapp/src/components/DAGGraph/elements/state.ts b/webapp/src/components/DAGGraph/elements/state.ts index b69b300..67dc455 100644 --- a/webapp/src/components/DAGGraph/elements/state.ts +++ b/webapp/src/components/DAGGraph/elements/state.ts @@ -1,5 +1,7 @@ import { useState } from "react"; import { AutomationNode } from "types/automations"; +import { isSpecialNode } from "./specialnodes"; +import { getNodeSubType } from "utils/automations"; export type DAGNodeState = Partial<{ isClosed: boolean; @@ -27,7 +29,7 @@ export const useDAGElementsState = () => { }, getIsClosedActions(nodeId: string, node: AutomationNode) { - if ("choose" in node || "repeat" in node) { + if (isSpecialNode(getNodeSubType(node))) { return { isClosed: this.get(nodeId).isClosed ?? false, setIsClosed: (v: boolean) => this.set(nodeId, "isClosed", v), diff --git a/webapp/src/components/DAGGraph/updater/index.ts b/webapp/src/components/DAGGraph/updater/index.ts index 4af7508..e6487a0 100644 --- a/webapp/src/components/DAGGraph/updater/index.ts +++ b/webapp/src/components/DAGGraph/updater/index.ts @@ -5,7 +5,10 @@ import { import { ModalState } from "../board/types"; import { SequenceNodeActions } from "../nodes/SequenceNode/types"; import { DAGUpdaterArgs, DAGUpdaterInput } from "./types"; -import { mapAutoActionKeyToNodeType } from "./util"; +import { + convertChooseActionToOrignal, + mapAutoActionKeyToNodeType, +} from "./util"; import { ChooseAction, RepeatAction, @@ -172,7 +175,9 @@ export const createUpdater = ( }; }, createChoosNodeUpdater(i: number, j: number | "else") { - const node = args.sequence.data[i] as ChooseAction; + const node = convertChooseActionToOrignal( + args.sequence.data[i] as ChooseAction + ); if (j === "else") { return { stateUpdater: createUpdater({ diff --git a/webapp/src/components/DAGGraph/updater/util.ts b/webapp/src/components/DAGGraph/updater/util.ts index 5cf9bf3..aac964a 100644 --- a/webapp/src/components/DAGGraph/updater/util.ts +++ b/webapp/src/components/DAGGraph/updater/util.ts @@ -1,4 +1,5 @@ import { AutomationActionData, AutomationNodeTypes } from "types/automations"; +import { ChooseAction, OriginalChooseAction } from "types/automations/actions"; export const mapAutoActionKeyToNodeType = ( k: keyof AutomationActionData @@ -8,3 +9,21 @@ export const mapAutoActionKeyToNodeType = ( : k === "trigger" ? ["trigger"] : ["action", "condition"]; + +export const convertChooseActionToOrignal = ( + node: ChooseAction +): OriginalChooseAction => { + if ("if" in node) { + return { + choose: [ + { + conditions: node.if, + sequence: node.then, + }, + ], + default: node.else, + }; + } else { + return node; + } +}; diff --git a/webapp/src/components/NodeEditor/index.tsx b/webapp/src/components/NodeEditor/index.tsx index 89c6206..b4db582 100644 --- a/webapp/src/components/NodeEditor/index.tsx +++ b/webapp/src/components/NodeEditor/index.tsx @@ -7,6 +7,7 @@ import { InputList } from "components/Inputs/InputList"; import { Button } from "components/Inputs/Button"; import InputBoolean from "components/Inputs/InputBoolean"; import { useConfirm } from "material-ui-confirm"; +import { getNodeSubTypeDescription } from "./utils"; export interface NodeEditorProps { node: AutomationNode; @@ -96,7 +97,8 @@ export const NodeEditor: FC = ({ {state.nodeType !== "condition" ? ( > = { + choose: "create conditional if/else statements", + parallel: "run multiple nodes at the same time", + stop: "stop the automation with a status code", + wait: "wait based on a condition", + repeat: "repeat a sequence until or while some conditions are met", + device: "device related actions", + service: "call a service", +}; + +export const getNodeSubTypeDescription = ( + nst: AutomationNodeSubtype +): string => { + return subtypeDescMap[nst] ?? ""; +}; diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index d27e5fc..178a1a5 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -28,13 +28,19 @@ export type DelayAction = st.Infer; export type FireEventAction = st.Infer; export type DeviceAction = st.Infer; export type StopAction = st.Infer; -export type ChooseAction = AutomationActionNodeBase<{ +export type ChooseAction = OriginalChooseAction | IfElseAction; +export type OriginalChooseAction = AutomationActionNodeBase<{ choose: Array<{ conditions: AutomationCondition[]; sequence: AutomationSequenceNode[]; }>; default?: AutomationSequenceNode[]; }>; +export type IfElseAction = AutomationActionNodeBase<{ + if: AutomationCondition[]; + then: AutomationSequenceNode[]; + else: AutomationSequenceNode[]; +}>; export type ParallelAction = AutomationActionNodeBase<{ parallel: Array< | AutomationSequenceNode diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index 88a2ff2..a18b253 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -79,7 +79,7 @@ export const StopAction = st.assign( }) ); -export const ChooseAction = st.intersection([ +export const OriginalChooseAction = st.intersection([ AutomationActionNodeBase, st.type({ choose: st.array( @@ -92,6 +92,17 @@ export const ChooseAction = st.intersection([ }), ]); +export const IfElseAction = st.intersection([ + AutomationActionNodeBase, + st.type({ + if: st.array(st.object()), + then: st.array(st.object()), + else: st.array(st.object()), + }), +]); + +export const ChooseAction = st.union([OriginalChooseAction, IfElseAction]); + export const ParallelAction = st.intersection([ AutomationActionNodeBase, st.type({ diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 8ecc27a..0cad2b3 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -133,7 +133,7 @@ export const getNodeSubType = < return "event" as any; } else if ("device_id" in node) { return "device" as any; - } else if ("choose" in node) { + } else if ("choose" in node || "if" in node) { return "choose" as any; } else if ("delay" in node) { return "wait" as any; diff --git a/webapp/src/utils/formatting.tsx b/webapp/src/utils/formatting.tsx index 104937c..1525466 100644 --- a/webapp/src/utils/formatting.tsx +++ b/webapp/src/utils/formatting.tsx @@ -162,7 +162,7 @@ export const getDescriptionFromAutomationNode = < if ("device_id" in node) { return getDescriptionForDeviceType(node as any, namer, false); } - if ("choose" in node) { + if ("choose" in node || "if" in node) { if (allowJs) { return (