diff --git a/webapp/src/components/ConditionEditor/ConditionNodeBase.tsx b/webapp/src/components/ConditionEditor/ConditionNodeBase.tsx index 4307f9c..47cc85a 100644 --- a/webapp/src/components/ConditionEditor/ConditionNodeBase.tsx +++ b/webapp/src/components/ConditionEditor/ConditionNodeBase.tsx @@ -18,6 +18,7 @@ export type ConditionNodeBaseProps = { onDelete?: () => void; onUpdate: (data: AutomationCondition) => void; disableDelete?: boolean; + initialViewMode?: ConditionNodeBaseViewMode; }; export type ConditionNodeBaseViewMode = "edit" | "yaml" | "view"; export const ConditionNodeBase: FC = (props) => { @@ -31,7 +32,9 @@ export const ConditionNodeBase: FC = (props) => { isEdited: false, condition: props.condition, }); - const [mode, setMode] = useState("view"); + const [mode, setMode] = useState( + props.initialViewMode ?? "view" + ); // alias const updateCondition = (data: any) => setCondition({ diff --git a/webapp/src/components/ConditionEditor/index.tsx b/webapp/src/components/ConditionEditor/index.tsx index c37cf9c..4bdc992 100644 --- a/webapp/src/components/ConditionEditor/index.tsx +++ b/webapp/src/components/ConditionEditor/index.tsx @@ -1,12 +1,14 @@ import { FC } from "react"; import { AutomationCondition } from "types/automations/conditions"; import { ConditionNodeBase } from "./ConditionNodeBase"; +import { ConditionNodeViewMode } from "./ConditionNodeSettings"; export const ConditionEditor: FC<{ condition: AutomationCondition; onDelete: () => void; onUpdate: (data: AutomationCondition) => void; -}> = ({ condition, onDelete, onUpdate }) => { + initialViewMode?: ConditionNodeViewMode; +}> = ({ condition, onDelete, onUpdate, initialViewMode }) => { // state return ( ); }; diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index 40b893b..6036005 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -41,7 +41,6 @@ export const DAGAutomationFlow: FC = (props) => { modalBody = ( setModalState(null)} onSave={(n) => { modalState.update(n as any); diff --git a/webapp/src/components/DAGGraph/board/index.tsx b/webapp/src/components/DAGGraph/board/index.tsx index 004ae8e..8a40854 100644 --- a/webapp/src/components/DAGGraph/board/index.tsx +++ b/webapp/src/components/DAGGraph/board/index.tsx @@ -34,7 +34,6 @@ export const DAGGraphBoard: FC = ({ modalBody = ( { modalState.update(n as any); diff --git a/webapp/src/components/DAGGraph/elements/distance.ts b/webapp/src/components/DAGGraph/elements/distance.ts index f4c6300..d9a656a 100644 --- a/webapp/src/components/DAGGraph/elements/distance.ts +++ b/webapp/src/components/DAGGraph/elements/distance.ts @@ -23,6 +23,25 @@ export const moveAlong = ( } }; +export const moveFromTo = ( + from: keyof DAGEntities, + to: keyof DAGEntities, + startPoint: XYPosition, + dims: DAGDims +): XYPosition => { + if (!dims.flipped) { + return { + x: startPoint.x + dims[from].width * dims.distanceFactor, + y: startPoint.y + (dims[from].height - dims[to].height) / 2, + }; + } else { + return { + y: startPoint.y + dims[from].height * dims.distanceFactor, + x: startPoint.x + (dims[from].width - dims[to].width) / 2, + }; + } +}; + export const computeSize = ( by: keyof DAGEntities, dims: DAGDims, diff --git a/webapp/src/components/DAGGraph/elements/index.ts b/webapp/src/components/DAGGraph/elements/index.ts index 1be0f27..241c27b 100644 --- a/webapp/src/components/DAGGraph/elements/index.ts +++ b/webapp/src/components/DAGGraph/elements/index.ts @@ -5,13 +5,11 @@ import { ElementMakerOutput, } from "./types"; import { AutomationTrigger } from "types/automations/triggers"; -import { SequenceNodeMaker } from "../nodes/SequenceNode"; import * as distance from "./distance"; import { getDescriptionFromAutomationNode } from "utils/formatting"; import { AutomationActionData } from "types/automations"; import { AutomationCondition } from "types/automations/conditions"; import { CollectionNodeMaker } from "../nodes/CollectionNode"; -import { LastNode } from "./types"; export const makeAutomationNodes = ( automation: AutomationActionData, @@ -24,22 +22,20 @@ export const makeAutomationNodes = ( edges: [], }, position: args.dims.position, - nodeId: "trigger", + nodeId: `${args.dims.flipped}-trigger`, nodeIndex: 0, }); const condOut = makeConditionNodes(automation.condition, { ...args, elementData: state.elementData, - position: { - x: - state.lastNode.pos.x + - args.dims.trigger.width * args.dims.distanceFactor, - y: - state.lastNode.pos.y + - (args.dims.trigger.height - args.dims.condition.height) / 2, - }, - nodeId: "condition", + position: distance.moveFromTo( + "trigger", + "condition", + state.lastNode.pos, + args.dims + ), + nodeId: `${args.dims.flipped}-condition`, nodeIndex: 1, lastNodeId: state.lastNode.nodeId, }); diff --git a/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx b/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx index faf49a6..9e0b1a1 100644 --- a/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/ConditionLogic.tsx @@ -1,18 +1,20 @@ -import { AutomationCondition } from 'types/automations/conditions'; +import { AutomationCondition } from "types/automations/conditions"; import { ConditionEditor } from "components/ConditionEditor"; import { OptionManager } from "./OptionManager"; - export const ConditionLogicState: OptionManager = { defaultState: () => ({ - condition: 'and', + condition: "and", alias: "", conditions: [], }), - isReady: _ => true, - renderOptionList: (state, setState) => { }} - /> -} + isReady: (_) => true, + Component: ({ state, setState, createMode }) => ( + {}} + initialViewMode={createMode ? "edit" : "view"} + /> + ), +}; diff --git a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts index e964565..1b0225b 100644 --- a/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts +++ b/webapp/src/components/NodeEditor/OptionManager/OptionManager.ts @@ -19,6 +19,7 @@ export type OptionManager = { state: T; setState: (s: T) => void; ha: HAService; + createMode: boolean; }>; } ); diff --git a/webapp/src/components/NodeEditor/OptionManager/index.tsx b/webapp/src/components/NodeEditor/OptionManager/index.tsx index f828662..dfd0126 100644 --- a/webapp/src/components/NodeEditor/OptionManager/index.tsx +++ b/webapp/src/components/NodeEditor/OptionManager/index.tsx @@ -18,8 +18,8 @@ type UseState = (s: S) => [S, Dispatch>]; export const useEditorNodeState = ( originalNode: AutomationNode, - isErrored: boolean, allowedTypes: AutomationNodeTypes[], + createMode: boolean, uState: UseState = useState ) => { // state @@ -153,6 +153,7 @@ export const useEditorNodeState = ( state={currentNode} setState={setState} ha={ha} + createMode={createMode} /> )} diff --git a/webapp/src/components/NodeEditor/index.tsx b/webapp/src/components/NodeEditor/index.tsx index 3f48726..4095e46 100644 --- a/webapp/src/components/NodeEditor/index.tsx +++ b/webapp/src/components/NodeEditor/index.tsx @@ -15,7 +15,6 @@ export interface NodeEditorProps { onSave?: (n: AutomationNode) => void; onFlags?: (isReady: boolean, isModified: boolean) => void; saveBtnCreateText?: boolean; - isErrored?: boolean; children?: ReactNode; } @@ -27,10 +26,9 @@ export const NodeEditor: FC = ({ onFlags = () => {}, saveBtnCreateText = false, children, - isErrored = false, }) => { // state - const state = useEditorNodeState(node, isErrored, allowedTypes); + const state = useEditorNodeState(node, allowedTypes, saveBtnCreateText); const confirm = useConfirm(); // alias