diff --git a/webapp/src/components/DAGFlow/index.tsx b/webapp/src/components/DAGFlow/index.tsx index 6a9a015..0836987 100644 --- a/webapp/src/components/DAGFlow/index.tsx +++ b/webapp/src/components/DAGFlow/index.tsx @@ -1,12 +1,17 @@ import { FC, useState } from "react"; -import ReactFlow, { Controls } from 'react-flow-renderer'; +import ReactFlow, { Controls } from "react-flow-renderer"; import { AutomationSequenceNode } from "types/automations"; -import { AutomationTrigger } from 'types/automations/triggers'; +import { AutomationTrigger } from "types/automations/triggers"; import { triggerToFlow } from "./triggerConvertor"; import { DAGNode } from "./DAGNode"; -import { DAGAutomationFlowDims, FlowData, ModalState, UpdateModalState } from './types'; -import { DAGCircle } from './DAGCircle'; +import { + DAGAutomationFlowDims, + FlowData, + ModalState, + UpdateModalState, +} from "./types"; +import { DAGCircle } from "./DAGCircle"; import { sequenceToFlow } from "./sequenceConvertor"; import { makeSequenceUpdater } from "./updater"; import { NodeEditor } from "components/NodeEditor"; @@ -19,152 +24,167 @@ import { DAGErrorNode } from "./DAGErrorNode"; import { useHA } from "haService"; const nodeTypes = { - 'dagnode': convertToFlowNode(DAGNode), - 'errornode': convertToFlowNode(DAGErrorNode), - 'dagcircle': DAGCircle, -} + dagnode: convertToFlowNode(DAGNode), + errornode: convertToFlowNode(DAGErrorNode), + dagcircle: DAGCircle, +}; -const makeOnEditAutomationConditions = ( +const makeOnEditAutomationConditions = + ( conditions: AutomationCondition[], updateConditions: (c: AutomationCondition[]) => void, - openModal: UpdateModalState, -) => () => openModal({ - single: false, - node: conditions, - allowedTypes: ['condition'], - update: c => updateConditions(c as any), -}) + openModal: UpdateModalState + ) => + () => + openModal({ + single: false, + node: conditions, + allowedTypes: ["condition"], + update: (c) => updateConditions(c as any), + }); export interface DAGAutomationFlowProps { - className?: string; - trigger: AutomationTrigger[]; - condition: AutomationCondition[]; - sequence: AutomationSequenceNode[]; - dims: DAGAutomationFlowDims; - onTriggerUpdate: (t: AutomationTrigger[]) => void; - onSequenceUpdate: (s: AutomationSequenceNode[]) => void; - onConditionUpdate: (s: AutomationCondition[]) => void; + className?: string; + trigger: AutomationTrigger[]; + condition: AutomationCondition[]; + sequence: AutomationSequenceNode[]; + dims: DAGAutomationFlowDims; + onTriggerUpdate: (t: AutomationTrigger[]) => void; + onSequenceUpdate: (s: AutomationSequenceNode[]) => void; + onConditionUpdate: (s: AutomationCondition[]) => void; } export const DAGAutomationFlow: FC = ({ - className, - trigger, - condition, - sequence, - onTriggerUpdate, - onSequenceUpdate, - onConditionUpdate, - dims, + className, + trigger, + condition, + sequence, + onTriggerUpdate, + onSequenceUpdate, + onConditionUpdate, + dims, }) => { - // state - const [modalState, setModalState] = useState(null); - const { namer, } = useHA(); - // modal - let modalBody = <> - if (modalState) { - if (modalState.single) { - modalBody = setModalState(null)} - onSave={n => { - modalState.update(n as any); - setModalState(null); - }} - allowedTypes={modalState.allowedTypes} - saveBtnCreateText={modalState.saveBtnCreateText} - /> - } else { - modalBody = setModalState(null)} - onSave={(n) => modalState.update(n as any)} - allowedTypes={['condition']} - /> - } + // state + const [modalState, setModalState] = useState(null); + const { namer } = useHA(); + // modal + let modalBody = <>; + if (modalState) { + if (modalState.single) { + modalBody = ( + setModalState(null)} + onSave={(n) => { + modalState.update(n as any); + setModalState(null); + }} + allowedTypes={modalState.allowedTypes} + saveBtnCreateText={modalState.saveBtnCreateText} + /> + ); + } else { + modalBody = ( + setModalState(null)} + onSave={(n) => modalState.update(n as any)} + allowedTypes={["condition"]} + /> + ); } + } - // convert automation to flow data - const condPoint = makeConditionPoint( - 'c', dims.flipped ? { - y: dims.padding.y + dims.nodeHeight * dims.distanceFactor, - x: dims.padding.x + dims.nodeWidth * 0.25 - } : { - x: dims.padding.x + dims.nodeWidth * dims.distanceFactor, - y: dims.padding.y + dims.nodeHeight * 0.25 - }, dims, condition.length, makeOnEditAutomationConditions( - condition, - onConditionUpdate, - setModalState, - )); - const flowData: FlowData = { - nodes: [condPoint], - edges: [], - } - triggerToFlow(flowData, trigger, condPoint.id, dims, { - namer, - onAdd: () => onTriggerUpdate([ - ...trigger, { - platform: 'event', - event_type: '', - } - ]), - onDelete: i => onTriggerUpdate([ - ...trigger.slice(0, i), - ...trigger.slice(i + 1) - ]), - onEdit: i => setModalState({ - single: true, - node: trigger[i], - update: t => onTriggerUpdate([ - ...trigger.slice(0, i), - t, - ...trigger.slice(i + 1) - ]), - allowedTypes: ['trigger'], - }) - }); - sequenceToFlow( - flowData, - sequence, - condPoint.id, - { - ...dims, padding: dims.flipped ? { - y: condPoint.position.y - dims.nodeHeight - dims.conditionHeight, - x: dims.padding.x, - } : { - x: condPoint.position.x - dims.nodeWidth - dims.conditionWidth, - y: dims.padding.y, - } + // convert automation to flow data + const condPoint = makeConditionPoint( + "c", + dims.flipped + ? { + y: dims.padding.y + dims.nodeHeight * dims.distanceFactor, + x: dims.padding.x + dims.nodeWidth * 0.25, + } + : { + x: dims.padding.x + dims.nodeWidth * dims.distanceFactor, + y: dims.padding.y + dims.nodeHeight * 0.25, }, - namer, - makeSequenceUpdater(sequence, onSequenceUpdate, setModalState), - ); - // this fixes some strange edge drawing issue - flowData.edges = flowData.edges.map(e => ({ - ...e, - "source": `${e.source}-${dims.flipped}`, - "target": `${e.target}-${dims.flipped}`, - })) - flowData.nodes = flowData.nodes.map(n => ({ - ...n, - id: `${n.id}-${dims.flipped}` - })) - // render - return <> - - {modalBody} - - - - - ; -} + dims, + condition.length, + makeOnEditAutomationConditions(condition, onConditionUpdate, setModalState) + ); + const flowData: FlowData = { + nodes: [condPoint], + edges: [], + }; + triggerToFlow(flowData, trigger, condPoint.id, dims, { + namer, + onAdd: () => + setModalState({ + single: true, + saveBtnCreateText: true, + node: { + platform: "device", + }, + update: (n) => { + onTriggerUpdate([...trigger, n]); + }, + allowedTypes: ["trigger"], + }), + onDelete: (i) => + onTriggerUpdate([...trigger.slice(0, i), ...trigger.slice(i + 1)]), + onEdit: (i) => + setModalState({ + single: true, + node: trigger[i], + update: (t) => + onTriggerUpdate([...trigger.slice(0, i), t, ...trigger.slice(i + 1)]), + allowedTypes: ["trigger"], + }), + }); + sequenceToFlow( + flowData, + sequence, + condPoint.id, + { + ...dims, + padding: dims.flipped + ? { + y: condPoint.position.y - dims.nodeHeight - dims.conditionHeight, + x: dims.padding.x, + } + : { + x: condPoint.position.x - dims.nodeWidth - dims.conditionWidth, + y: dims.padding.y, + }, + }, + namer, + makeSequenceUpdater(sequence, onSequenceUpdate, setModalState) + ); + // this fixes some strange edge drawing issue + flowData.edges = flowData.edges.map((e) => ({ + ...e, + source: `${e.source}-${dims.flipped}`, + target: `${e.target}-${dims.flipped}`, + })); + flowData.nodes = flowData.nodes.map((n) => ({ + ...n, + id: `${n.id}-${dims.flipped}`, + })); + // render + return ( + <> + {modalBody} + + + + + ); +}; diff --git a/webapp/src/components/Inputs/InputYaml.tsx b/webapp/src/components/Inputs/InputYaml.tsx index 42864e5..c68f195 100644 --- a/webapp/src/components/Inputs/InputYaml.tsx +++ b/webapp/src/components/Inputs/InputYaml.tsx @@ -58,6 +58,11 @@ export default function InputYaml({ } else { onChange({} as any); } + setState({ + text, + error, + hasChanged: false, + }); } }; useEffect(() => { @@ -67,21 +72,6 @@ export default function InputYaml({ hasChanged: false, }); }, [value, incmError]); - // useDelayEffect( - // () => { - // if (!error && yaml.dump(value) !== text) { - // if (text) { - // onChange(yaml.load(text) as any); - // } else if (Array.isArray(value)) { - // onChange([] as any); - // } else { - // onChange({} as any); - // } - // } - // }, - // [text], - // 1000 - // ); return ( ({ onSave()} title={!!error ? "Invalid YAML" : hasChanged ? "Save" : ""} > = { defaultState: () => ({ - "platform": "event", - "event_type": "", - "event_data": {}, - "context": {}, + platform: "event", + event_type: "", + event_data: {}, + context: {}, }), isReady: ({ event_type }) => { - return (event_type !== "") + return event_type !== ""; }, renderOptionList: (state, setState) => { - return <> - setState({ ...state, event_type })} - /> - setState({ ...state, event_data })} - /> - setState({ ...state, context })} - /> - - } -} + return ( + <> + setState({ ...state, event_type })} + /> + setState({ ...state, event_data })} + /> + setState({ ...state, context })} + /> + + ); + }, +};