From d4e2e2f1e540e62451a2f2ab92aa869d4437cf33 Mon Sep 17 00:00:00 2001 From: Ari Date: Thu, 14 Jul 2022 16:09:17 -0400 Subject: [PATCH] Initial support for parallel --- .../OptionManager/ActionParallel.tsx | 22 +++++++++++++++++++ .../OptionManager/getOptionManager.ts | 3 +++ webapp/src/types/automations/actions.ts | 12 +++++++++- webapp/src/types/validators/actions.ts | 7 ++++++ webapp/src/utils/automations.ts | 4 ++++ webapp/src/utils/formatting.tsx | 3 +++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 webapp/src/components/NodeEditor/OptionManager/ActionParallel.tsx diff --git a/webapp/src/components/NodeEditor/OptionManager/ActionParallel.tsx b/webapp/src/components/NodeEditor/OptionManager/ActionParallel.tsx new file mode 100644 index 0000000..8649658 --- /dev/null +++ b/webapp/src/components/NodeEditor/OptionManager/ActionParallel.tsx @@ -0,0 +1,22 @@ +import InputYaml from "components/Inputs/InputYaml"; +import { ParallelAction } from "types/automations/actions"; +import { OptionManager } from "./OptionManager"; + +export const ActionParallelState: OptionManager = { + defaultState: () => ({ + alias: "", + parallel: [], + }), + isReady: () => true, + Component: ({ state, setState }) => { + return ( + <> + setState({ ...state, parallel })} + /> + + ); + }, +}; diff --git a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts index 57b22bc..b26a726 100644 --- a/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/getOptionManager.ts @@ -20,6 +20,7 @@ import { TriggerWebhook } from "./TriggerWebhook"; import { TriggerZone } from "./TriggerZone"; import { TriggerDevice } from "./TriggerDevice"; import { ActionStopState } from "./ActionStop"; +import { ActionParallelState } from "./ActionParallel"; export const getOptionManager = ( nodeType: T, @@ -43,6 +44,8 @@ export const getOptionManager = ( return ActionStopState; case "delay": return ActionWaitState; + case "parallel": + return ActionParallelState; } } if (nodeType === "condition") { diff --git a/webapp/src/types/automations/actions.ts b/webapp/src/types/automations/actions.ts index 2c3ee0a..2c6ced1 100644 --- a/webapp/src/types/automations/actions.ts +++ b/webapp/src/types/automations/actions.ts @@ -28,7 +28,16 @@ export type ChooseAction = AutomationActionNodeBase<{ }>; default?: AutomationSequenceNode[]; }>; +export type ParallelAction = AutomationActionNodeBase<{ + parallel: Array< + | AutomationAction + | { + sequence: AutomationAction[]; + } + >; +}>; export type AutomationAction = + | ParallelAction | ServiceAction | RepeatAction | WaitAction @@ -44,4 +53,5 @@ export type ActionType = | "event" | "device" | "stop" - | "choose"; + | "choose" + | "parallel"; diff --git a/webapp/src/types/validators/actions.ts b/webapp/src/types/validators/actions.ts index 86751aa..4395ef4 100644 --- a/webapp/src/types/validators/actions.ts +++ b/webapp/src/types/validators/actions.ts @@ -83,3 +83,10 @@ export const ChooseAction = st.intersection([ default: st.optional(st.array(st.object())), }), ]); + +export const ParallelAction = st.intersection([ + AutomationActionNodeBase, + st.type({ + parallel: st.array(st.object()), + }), +]); diff --git a/webapp/src/utils/automations.ts b/webapp/src/utils/automations.ts index 113bc96..6896ab1 100644 --- a/webapp/src/utils/automations.ts +++ b/webapp/src/utils/automations.ts @@ -49,6 +49,7 @@ export const getSubTypeList = (nodeType: T) => { "device", "stop", "choose", + "parallel", ] as AutomationNodeSubtype<"action">[]; case "condition": return [ @@ -132,6 +133,8 @@ export const getNodeSubType = ( return "wait" as any; } else if ("stop" in node) { return "stop" as any; + } else if ("parallel" in node) { + return "parallel" as any; } else { throw new Error("Invalid node type!"); } @@ -153,6 +156,7 @@ export const TriggerValidators: Record = { }; export const ActionValidators: Record = { + parallel: v.actions.ParallelAction, choose: v.actions.ChooseAction, device: v.actions.DeviceAction, event: v.actions.FireEventAction, diff --git a/webapp/src/utils/formatting.tsx b/webapp/src/utils/formatting.tsx index 5544803..f74d525 100644 --- a/webapp/src/utils/formatting.tsx +++ b/webapp/src/utils/formatting.tsx @@ -188,6 +188,9 @@ export const getDescriptionFromAutomationNode = < } return `Stopping due to ${node.stop}`; } + if ("parallel" in node) { + return "Parallel"; + } return "n/a"; };