fixerd up event

This commit is contained in:
ari committed 2022-06-19 03:14:06 -04:00
1 parent eaec599109
commit 3acbaf576d
3 files changed
+195 -184

No files matched your search

+161 -141
View File
@@ -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<DAGAutomationFlowProps> = ({
className,
trigger,
condition,
sequence,
onTriggerUpdate,
onSequenceUpdate,
onConditionUpdate,
dims,
className,
trigger,
condition,
sequence,
onTriggerUpdate,
onSequenceUpdate,
onConditionUpdate,
dims,
}) => {
// state
const [modalState, setModalState] = useState<ModalState | null>(null);
const { namer, } = useHA();
// modal
let modalBody = <></>
if (modalState) {
if (modalState.single) {
modalBody = <NodeEditor
node={modalState.node}
isErrored={modalState.isError}
onClose={() => setModalState(null)}
onSave={n => {
modalState.update(n as any);
setModalState(null);
}}
allowedTypes={modalState.allowedTypes}
saveBtnCreateText={modalState.saveBtnCreateText}
/>
} else {
modalBody = <MultiNodeEditor
sequence={modalState.node}
onClose={() => setModalState(null)}
onSave={(n) => modalState.update(n as any)}
allowedTypes={['condition']}
/>
}
// state
const [modalState, setModalState] = useState<ModalState | null>(null);
const { namer } = useHA();
// modal
let modalBody = <></>;
if (modalState) {
if (modalState.single) {
modalBody = (
<NodeEditor
node={modalState.node}
isErrored={modalState.isError}
onClose={() => setModalState(null)}
onSave={(n) => {
modalState.update(n as any);
setModalState(null);
}}
allowedTypes={modalState.allowedTypes}
saveBtnCreateText={modalState.saveBtnCreateText}
/>
);
} else {
modalBody = (
<MultiNodeEditor
sequence={modalState.node}
onClose={() => 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 <>
<Modal open={!!modalState}>
{modalBody}
</Modal>
<ReactFlow
className={className}
nodeTypes={nodeTypes}
snapToGrid
onlyRenderVisibleElements
nodesConnectable={false}
nodesDraggable={false}
{...flowData}
>
<Controls />
</ReactFlow>
</>;
}
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 (
<>
<Modal open={!!modalState}>{modalBody}</Modal>
<ReactFlow
className={className}
nodeTypes={nodeTypes}
snapToGrid
onlyRenderVisibleElements
nodesConnectable={false}
nodesDraggable={false}
{...flowData}
>
<Controls />
</ReactFlow>
</>
);
};
+6 -16
View File
@@ -58,6 +58,11 @@ export default function InputYaml<T extends {}>({
} else {
onChange({} as any);
}
setState({
text,
error,
hasChanged: false,
});
}
};
useEffect(() => {
@@ -67,21 +72,6 @@ export default function InputYaml<T extends {}>({
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 (
<FormControl
className={["input-yaml", className ?? ""].join(" ")}
@@ -106,7 +96,7 @@ export default function InputYaml<T extends {}>({
<InputAdornment position="end">
<IconButton
disabled={!hasChanged}
onChange={onSave}
onClick={() => onSave()}
title={!!error ? "Invalid YAML" : hasChanged ? "Save" : ""}
>
<Save
@@ -1,36 +1,37 @@
import InputText from "components/Inputs/InputText";
import { OptionManager } from './OptionManager';
import { AutomationTriggerEvent } from '../../../types/automations/triggers';
import { OptionManager } from "./OptionManager";
import { AutomationTriggerEvent } from "../../../types/automations/triggers";
import InputYaml from "components/Inputs/InputYaml";
export const TriggerEventState: OptionManager<AutomationTriggerEvent> = {
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 <>
<InputText
label="Event Type"
value={String(state.event_type)}
onChange={event_type => setState({ ...state, event_type })}
/>
<InputYaml
label="Data"
value={state.event_data ?? {}}
onChange={event_data => setState({ ...state, event_data })}
/>
<InputYaml
label="Context"
value={state.event_data ?? {}}
onChange={context => setState({ ...state, context })}
/>
</>
}
}
return (
<>
<InputText
label="Event Type"
value={String(state.event_type)}
onChange={(event_type) => setState({ ...state, event_type })}
/>
<InputYaml
label="Data"
value={state.event_data ?? {}}
onChange={(event_data) => setState({ ...state, event_data })}
/>
<InputYaml
label="Context"
value={state.context ?? {}}
onChange={(context) => setState({ ...state, context })}
/>
</>
);
},
};