import { useState } from "react"; import * as types from "../../automation/types"; import { getNameFromAction } from "../../automation/utils"; import AutomationNode from "../Node"; import NewNodeModal from "./NewNodeModal"; import ScriptNode from "./ScriptNode"; export interface Props { automation: types.Automation; } export default function AutomationEditor({ automation }: Props) { // state const [modalState, setModalState] = useState(null); const [state, setState] = useState(automation); // helper state functions const addNode = (node_type: T, node: types.AutomationNodeTypes[T]) => { const nodes: types.AutomationNode[] = state[node_type]; setState({ ...state, [node_type]: nodes.concat([node as any]) }); } const removeNode = (node_type: T, node_id: number) => { const nodes: types.AutomationNode[] = state[node_type]; setState({ ...state, [node_type]: nodes .slice(0, node_id) .concat( nodes.slice( node_id + 1, nodes.length ) ) }); } // helper element functions const makeChild = (node_type: T) => { return [
{state[node_type].map((n, i) => ( removeNode(node_type, i)} data={n as any} /> ))}
, ]; } return

Trigger

{makeChild("trigger")}

Condition

{makeChild("condition")}

Actions

{makeChild("action")}
{modalState && setModalState(null)} onCreate={node => { addNode(modalState, node); setModalState(null); }} />}
}