fixerd up event
This commit is contained in:
1 parent
eaec599109
commit
3acbaf576d
3 files changed
+195
-184
No files matched your search
@@ -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>
|
||||
</>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user