Skip to content

Commit b1914fd

Browse files
committed
Got the nodes to render as a tree structure using D3. Need to get the edges to connect upon rendering to finish the tree.
1 parent 33ceabf commit b1914fd

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/webview/Flow.tsx

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import FlowBuilder from './flowBuilder';
1515
import * as d3 from 'd3';
1616
import { Tree } from "../types/tree";
1717
import { hierarchyData } from "../types/hierarchyData";
18+
import { getNonce } from "../getNonce";
1819

1920
const onInit = (reactFlowInstance: ReactFlowInstance) =>
2021
console.log("flow loaded:", reactFlowInstance);
@@ -24,7 +25,7 @@ const OverviewFlow = () => {
2425
const initialNodes = [];
2526
const initialEdges = [];
2627
const elements = [];
27-
const edge = [];
28+
const edgeArr = [];
2829

2930
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
3031
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
@@ -52,7 +53,7 @@ const OverviewFlow = () => {
5253

5354

5455
//create tree layout and give nodes their positions
55-
const treeLayout = d3.tree()
56+
const treeLayout = d3.tree().size([500, 300])
5657
treeLayout(root);
5758

5859
root.each((node: any) : void => {
@@ -61,25 +62,51 @@ const OverviewFlow = () => {
6162
id: node.data.name,
6263
type: 'default',
6364
data: { label: node.data.name },
64-
position: { x: node.x || 0, y: node.y || 0 },
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+
}
6575
});
6676

6777
// figure out how to get edges to connect
6878
if (node.data.parent) {
69-
// elements.push(addEdge({
70-
// id: node.data.id,
71-
// source: node.data.id,
72-
// target: node.data.parent,
73-
// // sourceHandle: null,
74-
// // targetHandle: null,
75-
// }))
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+
}
7695
}
77-
})
96+
}
97+
)
98+
console.log('Initial Edges', initialEdges)
99+
setEdges(initialEdges)
100+
console.log('Edges', edges)
78101
break;
79102
}
80103
}
104+
105+
81106
});
82107

108+
109+
83110
setNodes(elements);
84111

85112
//Rendering reactFlow
@@ -97,9 +124,9 @@ const OverviewFlow = () => {
97124
<div style={{ height: '600px', width: '100%' }}>
98125
<ReactFlow
99126
nodes={nodes}
100-
// edges={edges}
127+
// edges={edgeArr}
101128
// nodes={elements}
102-
// edges={edge}
129+
edges={edges}
103130
onNodesChange={onNodesChange}
104131
onEdgesChange={onEdgesChange}
105132
onConnect={onConnect}

0 commit comments

Comments
 (0)