support for if/else
This commit is contained in:
1 parent
a1ca9d62b7
commit
bbb87ebf9e
12 files changed
+93
-11
No files matched your search
@@ -501,3 +501,19 @@ BadNodes.args = {
|
|||||||
] as any,
|
] as any,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const IfElseNode = Template.bind({});
|
||||||
|
IfElseNode.args = {
|
||||||
|
...BadNodes.args,
|
||||||
|
automation: {
|
||||||
|
condition: [],
|
||||||
|
trigger: [],
|
||||||
|
sequence: [
|
||||||
|
{
|
||||||
|
if: [],
|
||||||
|
else: [],
|
||||||
|
then: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -26,8 +26,8 @@ import { getNodeSubType } from "../../../utils/automations";
|
|||||||
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
import DeleteForeverIcon from "@mui/icons-material/DeleteForever";
|
||||||
import Add from "@mui/icons-material/Add";
|
import Add from "@mui/icons-material/Add";
|
||||||
import { SequenceNodeDataProps } from "../nodes/SequenceNode/types";
|
import { SequenceNodeDataProps } from "../nodes/SequenceNode/types";
|
||||||
import InfoOutlined from "@mui/icons-material/InfoOutlined";
|
|
||||||
import { convertFailuresToSequenceNodeDataProps } from "./util";
|
import { convertFailuresToSequenceNodeDataProps } from "./util";
|
||||||
|
import { convertChooseActionToOrignal } from "../updater/util";
|
||||||
|
|
||||||
export const useAutomationNodes = (
|
export const useAutomationNodes = (
|
||||||
automation: AutomationActionData,
|
automation: AutomationActionData,
|
||||||
@@ -242,7 +242,8 @@ export const makeSequenceNodes: ElementMaker<AutomationSequenceNode> = (
|
|||||||
|
|
||||||
export const makeChooseeNodes = makeSpecialNodeMaker(
|
export const makeChooseeNodes = makeSpecialNodeMaker(
|
||||||
"choose",
|
"choose",
|
||||||
(node, outputState, args) => {
|
(incmNode, outputState, args) => {
|
||||||
|
const node = convertChooseActionToOrignal(incmNode);
|
||||||
// offset position
|
// offset position
|
||||||
let offsetPos: XYPosition = distance.offsetBy(
|
let offsetPos: XYPosition = distance.offsetBy(
|
||||||
"node",
|
"node",
|
||||||
@@ -377,7 +378,6 @@ export const makeChooseeNodes = makeSpecialNodeMaker(
|
|||||||
args.dims,
|
args.dims,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
console.log({ addPos, lastConditionState });
|
|
||||||
if (lastConditionState !== null) {
|
if (lastConditionState !== null) {
|
||||||
addPos = distance.moveFromTo(
|
addPos = distance.moveFromTo(
|
||||||
"collection",
|
"collection",
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { AutomationNodeSubtype } from "types/automations";
|
||||||
import { ActionType, ActionTypeMapping } from "types/automations/actions";
|
import { ActionType, ActionTypeMapping } from "types/automations/actions";
|
||||||
import { DAGElementsOutputState } from "./outputState";
|
import { DAGElementsOutputState } from "./outputState";
|
||||||
import { ElementMaker, ElementMakerProps } from "./types";
|
import { ElementMaker, ElementMakerProps } from "./types";
|
||||||
@@ -41,3 +42,6 @@ export const makeSpecialNodeMaker = <
|
|||||||
export const getSpecialNodeMaker = <AT extends ActionType>(
|
export const getSpecialNodeMaker = <AT extends ActionType>(
|
||||||
nodeType: AT
|
nodeType: AT
|
||||||
): ElementMaker<AT> | null => SpecialNodeRegistry[nodeType] ?? null;
|
): ElementMaker<AT> | null => SpecialNodeRegistry[nodeType] ?? null;
|
||||||
|
|
||||||
|
export const isSpecialNode = (nodeType: AutomationNodeSubtype) =>
|
||||||
|
nodeType in SpecialNodeRegistry;
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { AutomationNode } from "types/automations";
|
import { AutomationNode } from "types/automations";
|
||||||
|
import { isSpecialNode } from "./specialnodes";
|
||||||
|
import { getNodeSubType } from "utils/automations";
|
||||||
|
|
||||||
export type DAGNodeState = Partial<{
|
export type DAGNodeState = Partial<{
|
||||||
isClosed: boolean;
|
isClosed: boolean;
|
||||||
@@ -27,7 +29,7 @@ export const useDAGElementsState = () => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getIsClosedActions(nodeId: string, node: AutomationNode) {
|
getIsClosedActions(nodeId: string, node: AutomationNode) {
|
||||||
if ("choose" in node || "repeat" in node) {
|
if (isSpecialNode(getNodeSubType(node))) {
|
||||||
return {
|
return {
|
||||||
isClosed: this.get(nodeId).isClosed ?? false,
|
isClosed: this.get(nodeId).isClosed ?? false,
|
||||||
setIsClosed: (v: boolean) => this.set(nodeId, "isClosed", v),
|
setIsClosed: (v: boolean) => this.set(nodeId, "isClosed", v),
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import {
|
|||||||
import { ModalState } from "../board/types";
|
import { ModalState } from "../board/types";
|
||||||
import { SequenceNodeActions } from "../nodes/SequenceNode/types";
|
import { SequenceNodeActions } from "../nodes/SequenceNode/types";
|
||||||
import { DAGUpdaterArgs, DAGUpdaterInput } from "./types";
|
import { DAGUpdaterArgs, DAGUpdaterInput } from "./types";
|
||||||
import { mapAutoActionKeyToNodeType } from "./util";
|
import {
|
||||||
|
convertChooseActionToOrignal,
|
||||||
|
mapAutoActionKeyToNodeType,
|
||||||
|
} from "./util";
|
||||||
import {
|
import {
|
||||||
ChooseAction,
|
ChooseAction,
|
||||||
RepeatAction,
|
RepeatAction,
|
||||||
@@ -172,7 +175,9 @@ export const createUpdater = (
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
createChoosNodeUpdater(i: number, j: number | "else") {
|
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") {
|
if (j === "else") {
|
||||||
return {
|
return {
|
||||||
stateUpdater: createUpdater({
|
stateUpdater: createUpdater({
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { AutomationActionData, AutomationNodeTypes } from "types/automations";
|
import { AutomationActionData, AutomationNodeTypes } from "types/automations";
|
||||||
|
import { ChooseAction, OriginalChooseAction } from "types/automations/actions";
|
||||||
|
|
||||||
export const mapAutoActionKeyToNodeType = (
|
export const mapAutoActionKeyToNodeType = (
|
||||||
k: keyof AutomationActionData
|
k: keyof AutomationActionData
|
||||||
@@ -8,3 +9,21 @@ export const mapAutoActionKeyToNodeType = (
|
|||||||
: k === "trigger"
|
: k === "trigger"
|
||||||
? ["trigger"]
|
? ["trigger"]
|
||||||
: ["action", "condition"];
|
: ["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;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -7,6 +7,7 @@ import { InputList } from "components/Inputs/InputList";
|
|||||||
import { Button } from "components/Inputs/Button";
|
import { Button } from "components/Inputs/Button";
|
||||||
import InputBoolean from "components/Inputs/InputBoolean";
|
import InputBoolean from "components/Inputs/InputBoolean";
|
||||||
import { useConfirm } from "material-ui-confirm";
|
import { useConfirm } from "material-ui-confirm";
|
||||||
|
import { getNodeSubTypeDescription } from "./utils";
|
||||||
|
|
||||||
export interface NodeEditorProps {
|
export interface NodeEditorProps {
|
||||||
node: AutomationNode;
|
node: AutomationNode;
|
||||||
@@ -96,7 +97,8 @@ export const NodeEditor: FC<NodeEditorProps> = ({
|
|||||||
{state.nodeType !== "condition" ? (
|
{state.nodeType !== "condition" ? (
|
||||||
<InputList
|
<InputList
|
||||||
label={state.prettyNodeType}
|
label={state.prettyNodeType}
|
||||||
current={state.subType as any}
|
current={state.subType}
|
||||||
|
getDescription={getNodeSubTypeDescription}
|
||||||
options={state.subTypes}
|
options={state.subTypes}
|
||||||
onChange={state.setSubType}
|
onChange={state.setSubType}
|
||||||
prettyOptionLabels
|
prettyOptionLabels
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { AutomationNodeSubtype } from "types/automations";
|
||||||
|
|
||||||
|
const subtypeDescMap: Partial<Record<AutomationNodeSubtype, string>> = {
|
||||||
|
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] ?? "";
|
||||||
|
};
|
||||||
@@ -28,13 +28,19 @@ export type DelayAction = st.Infer<typeof v.DelayAction>;
|
|||||||
export type FireEventAction = st.Infer<typeof v.FireEventAction>;
|
export type FireEventAction = st.Infer<typeof v.FireEventAction>;
|
||||||
export type DeviceAction = st.Infer<typeof v.DeviceAction>;
|
export type DeviceAction = st.Infer<typeof v.DeviceAction>;
|
||||||
export type StopAction = st.Infer<typeof v.StopAction>;
|
export type StopAction = st.Infer<typeof v.StopAction>;
|
||||||
export type ChooseAction = AutomationActionNodeBase<{
|
export type ChooseAction = OriginalChooseAction | IfElseAction;
|
||||||
|
export type OriginalChooseAction = AutomationActionNodeBase<{
|
||||||
choose: Array<{
|
choose: Array<{
|
||||||
conditions: AutomationCondition[];
|
conditions: AutomationCondition[];
|
||||||
sequence: AutomationSequenceNode[];
|
sequence: AutomationSequenceNode[];
|
||||||
}>;
|
}>;
|
||||||
default?: AutomationSequenceNode[];
|
default?: AutomationSequenceNode[];
|
||||||
}>;
|
}>;
|
||||||
|
export type IfElseAction = AutomationActionNodeBase<{
|
||||||
|
if: AutomationCondition[];
|
||||||
|
then: AutomationSequenceNode[];
|
||||||
|
else: AutomationSequenceNode[];
|
||||||
|
}>;
|
||||||
export type ParallelAction = AutomationActionNodeBase<{
|
export type ParallelAction = AutomationActionNodeBase<{
|
||||||
parallel: Array<
|
parallel: Array<
|
||||||
| AutomationSequenceNode
|
| AutomationSequenceNode
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ export const StopAction = st.assign(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
export const ChooseAction = st.intersection([
|
export const OriginalChooseAction = st.intersection([
|
||||||
AutomationActionNodeBase,
|
AutomationActionNodeBase,
|
||||||
st.type({
|
st.type({
|
||||||
choose: st.array(
|
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([
|
export const ParallelAction = st.intersection([
|
||||||
AutomationActionNodeBase,
|
AutomationActionNodeBase,
|
||||||
st.type({
|
st.type({
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ export const getNodeSubType = <
|
|||||||
return "event" as any;
|
return "event" as any;
|
||||||
} else if ("device_id" in node) {
|
} else if ("device_id" in node) {
|
||||||
return "device" as any;
|
return "device" as any;
|
||||||
} else if ("choose" in node) {
|
} else if ("choose" in node || "if" in node) {
|
||||||
return "choose" as any;
|
return "choose" as any;
|
||||||
} else if ("delay" in node) {
|
} else if ("delay" in node) {
|
||||||
return "wait" as any;
|
return "wait" as any;
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ export const getDescriptionFromAutomationNode = <
|
|||||||
if ("device_id" in node) {
|
if ("device_id" in node) {
|
||||||
return getDescriptionForDeviceType(node as any, namer, false);
|
return getDescriptionForDeviceType(node as any, namer, false);
|
||||||
}
|
}
|
||||||
if ("choose" in node) {
|
if ("choose" in node || "if" in node) {
|
||||||
if (allowJs) {
|
if (allowJs) {
|
||||||
return (
|
return (
|
||||||
<AltRouteOutlinedIcon
|
<AltRouteOutlinedIcon
|
||||||
|
|||||||
Reference in new issue
Block a user