-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathReactFlowWrapper.tsx
153 lines (141 loc) · 4.46 KB
/
ReactFlowWrapper.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as React from 'react';
import { useState, useEffect } from 'react';
import ReactFlow, {
Background,
ReactFlowProvider,
useStoreState
} from 'react-flow-renderer';
import { RFWrapperProps, LayoutRCProps } from './types';
import {
ReactFlowCustomBranchNode,
ReactFlowCustomEndNode,
ReactFlowCustomNestedPoint,
ReactFlowCustomStartNode,
ReactFlowCustomTaskNode,
ReactFlowCustomSubworkflowNode,
ReactFlowCustomMaxNested,
ReactFlowStaticNested,
ReactFlowStaticNode
} from './customNodeComponents';
import setReactFlowGraphLayout from './utils';
/**
* Mapping for using custom nodes inside ReactFlow
*/
const CustomNodeTypes = {
FlyteNode_task: ReactFlowCustomTaskNode,
FlyteNode_subworkflow: ReactFlowCustomSubworkflowNode,
FlyteNode_branch: ReactFlowCustomBranchNode,
FlyteNode_start: ReactFlowCustomStartNode,
FlyteNode_end: ReactFlowCustomEndNode,
FlyteNode_nestedStart: ReactFlowCustomNestedPoint,
FlyteNode_nestedEnd: ReactFlowCustomNestedPoint,
FlyteNode_nestedMaxDepth: ReactFlowCustomMaxNested,
FlyteNode_staticNode: ReactFlowStaticNode,
FlyteNode_staticNestedNode: ReactFlowStaticNested
};
/**
* Renderless component waits for ReactFlow to give rendered
* dimensions before computing layout
* @param props:LayoutRC
* @returns: void
*/
const LayoutRC: React.FC<LayoutRCProps> = ({
setElements,
setLayout,
hasLayout
}: LayoutRCProps) => {
/* strore is only populated onLoad for each flow */
const nodes = useStoreState(store => store.nodes);
const edges = useStoreState(store => store.edges);
const [computeLayout, setComputeLayout] = useState(true);
if (nodes.length > 0 && computeLayout) {
if (nodes[0].__rf.width) {
setComputeLayout(false);
}
}
useEffect(() => {
if (!hasLayout && !computeLayout) {
setComputeLayout(true);
}
}, [hasLayout, computeLayout]);
useEffect(() => {
if (!computeLayout) {
const nodesAndEdges = (nodes as any[]).concat(edges);
const { graph } = setReactFlowGraphLayout(nodesAndEdges, 'LR');
setElements(graph);
setLayout(true);
}
}, [computeLayout]);
return null;
};
/**
* Notes:
* To support nested graphs we wrap each flow inside its own provider/store
* which allows us to contextualize fitView to only render when its own
* elements change (not parents/children)
*
* Workflow:
* - set initial (unpositioned) elements and wait for onload
* - position elements (with rendered dimensions) in <LayoutRC>
* - fit view
*
* @see https://reactflow.dev/docs/
* @param props:ReactFlowWrapperProps
* @returns rendered component
*/
export const ReactFlowWrapper: React.FC<RFWrapperProps> = ({
rfGraphJson,
backgroundStyle,
version
}) => {
const [elements, setElements] = useState(rfGraphJson);
const [currentVersion, setCurrentVersion] = useState(version);
const [hasLayout, setHasLayout] = useState(false);
const [reactFlowInstance, setReactFlowInstance] = useState<null | any>(
null
);
const onLoad = (rf: any) => {
setReactFlowInstance(rf);
};
useEffect(() => {
if (version != currentVersion) {
setHasLayout(false);
setElements(rfGraphJson);
}
}, [version, rfGraphJson, currentVersion]);
/**
* Note: setLayout passed/called by <LayoutRC>
*/
useEffect(() => {
if (hasLayout && reactFlowInstance) {
reactFlowInstance?.fitView({ padding: 0 });
setCurrentVersion(version);
}
}, [hasLayout, reactFlowInstance]);
/**
* STEPS:
* - have each node click return nodes {text.data}
* - Then figure out the input needed for the slide out (execution id? node id?)
* - Append that to the rf nodes {data} object.
*/
return (
<ReactFlowProvider>
<ReactFlow
elements={elements}
onLoad={onLoad}
nodeTypes={CustomNodeTypes}
>
<Background
style={backgroundStyle.background}
color={backgroundStyle.gridColor}
gap={backgroundStyle.gridSpacing}
/>
</ReactFlow>
<LayoutRC
hasLayout={hasLayout}
setLayout={setHasLayout}
setElements={setElements}
></LayoutRC>
</ReactFlowProvider>
);
};