Skip to content

Commit 8b8c34d

Browse files
committed
Got the React Flow diagram working
1 parent b1914fd commit 8b8c34d

File tree

1 file changed

+85
-66
lines changed

1 file changed

+85
-66
lines changed

src/webview/Flow.tsx

Lines changed: 85 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -24,108 +24,127 @@ const OverviewFlow = () => {
2424
const reactFlowWrapper = useRef<HTMLDivElement>(null);
2525
const initialNodes = [];
2626
const initialEdges = [];
27+
const initialEdge = [];
2728
const elements = [];
28-
const edgeArr = [];
29+
// const elementsRef = useRef(elements);
30+
// const edgesRef = useRef(initialEdges)
2931

30-
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
31-
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
32+
const [nodes, setNodes, onNodesChange] = useNodesState([]);
33+
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
3234

3335
const onConnect = useCallback(
3436
(params) => setEdges((eds) => addEdge({ ...params, type: ConnectionLineType.Bezier, animated: true }, eds)),
3537
[]
3638
);
3739

40+
// const memoizedOnNodesChange = useCallback(
41+
// (newNodes) => {
42+
// setNodes(newNodes);
43+
// elementsRef.current = newNodes;
44+
// },
45+
// [setNodes]
46+
// );
47+
48+
// const memoizedOnEdgesChange = useCallback(
49+
// (newEdges) => {
50+
// setEdges(newEdges);
51+
// edgesRef.current = newEdges
52+
// },
53+
// [setEdges]
54+
// );
55+
3856
useEffect(() => {
3957
window.addEventListener('message', (e) => {
4058
const msg = e.data; // object containing type prop and value prop
4159
switch (msg.type) {
4260
case 'parsed-data': {
4361
// const results = new FlowBuilder(msg.value);
4462
// results.build(msg.settings)
45-
let data : object | undefined = msg.value;
46-
console.log('data', data)
4763
// setNodes(results.initialNodes);
4864
// setEdges(results.initialEdges);
49-
50-
// create a holder for the heirarchical data (msg.value), data comes in an object of all the Trees
51-
const root : any = d3.hierarchy(data)
52-
console.log('root', root)
53-
54-
55-
//create tree layout and give nodes their positions
56-
const treeLayout = d3.tree().size([500, 300])
57-
treeLayout(root);
58-
59-
root.each((node: any) : void => {
60-
// console.log('node name',Object.keys(node))
61-
elements.push({
62-
id: node.data.name,
63-
type: 'default',
64-
data: { label: node.data.name },
65-
position: { x: node.x ? node.x : 0, y: node.y ? node.y : 0 },
66-
style: {
67-
borderRadius: '6px',
68-
borderWidth: '2px',
69-
borderColor: '#6b7280',
70-
display: 'flex',
71-
justifyContent: 'center',
72-
placeItems: 'center',
73-
backgroundColor: `${(node.data.isClientComponent) ? '#fdba74' : '#93C5FD'}`,
74-
}
75-
});
76-
77-
// figure out how to get edges to connect
78-
if (node.data.parent) {
79-
const newEdge = {
80-
id: `${getNonce()}`,
81-
source: node.data.parent,
82-
target: node.data.id,
83-
type: ConnectionLineType.Bezier,
84-
animated: true
85-
};
86-
87-
// Check if the edge already exists before adding
88-
const edgeExists = edgeArr.some(
89-
edge => edge[0].source === newEdge.source && edge[0].target === newEdge.target
90-
);
91-
92-
if (!edgeExists) {
93-
initialEdges.push(newEdge);
94-
}
95-
}
96-
}
97-
)
98-
console.log('Initial Edges', initialEdges)
99-
setEdges(initialEdges)
100-
console.log('Edges', edges)
65+
let data : object | undefined = msg.value;
66+
console.log('data', data)
67+
mappedData(data)
68+
console.log('nodes', elements)
69+
setEdges(initialEdges);
70+
setNodes(elements)
71+
console.log('edges: ', edges);
10172
break;
10273
}
10374
}
104-
105-
10675
});
107-
10876

109-
110-
setNodes(elements);
77+
78+
79+
// memoizedOnEdgesChange(edgesRef.current);
80+
// memoizedOnNodesChange(elementsRef.current);
11181

11282
//Rendering reactFlow
11383
if (reactFlowWrapper.current) {
11484
// Set initial position for the nodes
11585
const initialPosition = { x: 0, y: 0 };
11686
// reactFlowWrapper.current?.fitView(initialPosition);
11787
}
118-
// assign root variable using d3.heirarchy(data[0]), this will use d3 to format our data in a easy way to visualize the heirarchy
119-
// const elements: Tree[];
12088

12189
}, []);
12290

91+
function mappedData (data) {
92+
// create a holder for the heirarchical data (msg.value), data comes in an object of all the Trees
93+
const root : any = d3.hierarchy(data)
94+
console.log('root', root)
95+
96+
97+
//create tree layout and give nodes their positions
98+
const treeLayout = d3.tree().size([800, 500])
99+
treeLayout(root);
100+
101+
root.each((node: any) : void => {
102+
103+
elements.push({
104+
id: node.data.id,
105+
type: 'default',
106+
data: { label: node.data.name },
107+
position: { x: node.x ? node.x : 0, y: node.y ? node.y : 0 },
108+
style: {
109+
borderRadius: '6px',
110+
borderWidth: '2px',
111+
borderColor: '#6b7280',
112+
display: 'flex',
113+
justifyContent: 'center',
114+
placeItems: 'center',
115+
backgroundColor: `${(node.data.isClientComponent) ? '#fdba74' : '#93C5FD'}`,
116+
}
117+
});
118+
119+
// figure out how to get edges to connect
120+
if (node.data.parent) {
121+
const newEdge = {
122+
id: `${getNonce()}`,
123+
source: node.data.parent,
124+
target: node.data.id,
125+
type: ConnectionLineType.Bezier,
126+
animated: true,
127+
};
128+
129+
130+
// Check if the edge already exists before adding
131+
const edgeExists = initialEdges.some(
132+
edge => edge.source === newEdge.source && edge.target === newEdge.target
133+
);
134+
135+
if (!edgeExists) {
136+
initialEdges.push(newEdge)
137+
}
138+
}
139+
}
140+
)
141+
142+
}
143+
123144
return (
124145
<div style={{ height: '600px', width: '100%' }}>
125146
<ReactFlow
126147
nodes={nodes}
127-
// edges={edgeArr}
128-
// nodes={elements}
129148
edges={edges}
130149
onNodesChange={onNodesChange}
131150
onEdgesChange={onEdgesChange}

0 commit comments

Comments
 (0)