1- import React , { useCallback , useEffect } from "react" ;
1+ import React , { useCallback , useEffect , useRef } from "react" ;
22import ReactFlow , {
33 addEdge ,
44 MiniMap ,
@@ -12,38 +12,108 @@ import ReactFlow, {
1212import "reactflow/dist/style.css" ;
1313import { ConnectionLineType } from "../types/connection" ;
1414import FlowBuilder from './flowBuilder' ;
15+ import * as d3 from 'd3' ;
16+ import { Tree } from "../types/tree" ;
17+ import { hierarchyData } from "../types/hierarchyData" ;
18+ import { getNonce } from "../getNonce" ;
1519
1620const onInit = ( reactFlowInstance : ReactFlowInstance ) =>
1721 console . log ( "flow loaded:" , reactFlowInstance ) ;
1822
1923const OverviewFlow = ( ) => {
24+ const reactFlowWrapper = useRef < HTMLDivElement > ( null ) ;
2025 const initialNodes = [ ] ;
2126 const initialEdges = [ ] ;
22-
23- const [ nodes , setNodes , onNodesChange ] = useNodesState ( initialNodes ) ;
24- const [ edges , setEdges , onEdgesChange ] = useEdgesState ( initialEdges ) ;
27+
28+ const [ nodes , setNodes , onNodesChange ] = useNodesState ( [ ] ) ;
29+ const [ edges , setEdges , onEdgesChange ] = useEdgesState ( [ ] ) ;
2530
2631 const onConnect = useCallback (
2732 ( params ) => setEdges ( ( eds ) => addEdge ( { ...params , type : ConnectionLineType . Bezier , animated : true } , eds ) ) ,
2833 [ ]
2934 ) ;
3035
36+
3137 useEffect ( ( ) => {
3238 window . addEventListener ( 'message' , ( e ) => {
33- const msg = e . data ; // object containing type prop and value prop
39+ // object containing type prop and value prop
40+ const msg = e . data ;
41+
3442 switch ( msg . type ) {
3543 case 'parsed-data' : {
36- const results = new FlowBuilder ( msg . value ) ;
37- results . build ( msg . settings )
38- setNodes ( results . initialNodes ) ;
39- setEdges ( results . initialEdges ) ;
44+ let data : object | undefined = msg . value ;
45+ console . log ( 'data' , data )
46+ mappedData ( data )
47+ console . log ( 'nodes' , initialNodes )
48+ setEdges ( initialEdges ) ;
49+ setNodes ( initialNodes )
50+ console . log ( 'edges: ' , edges ) ;
4051 break ;
4152 }
4253 }
4354 } ) ;
4455 } , [ ] ) ;
4556
57+
58+ // Function that creates Tree Structure
59+ function mappedData ( data ) {
60+
61+ // Create a holder for the heirarchical data (msg.value), data comes in an object of all the Trees
62+ const root : any = d3 . hierarchy ( data )
63+ console . log ( 'root' , root )
64+
65+ //create tree layout and give nodes their positions
66+ const treeLayout = d3 . tree ( ) . size ( [ 800 , 500 ] )
67+ treeLayout ( root ) ;
68+
69+ // Iterate through each Tree and create a node
70+ root . each ( ( node : any ) : void => {
71+
72+ // Create a Node from the current Root and add it to our nodes array
73+ initialNodes . push ( {
74+ id : node . data . id ,
75+ type : 'default' ,
76+ data : { label : node . data . name } ,
77+ position : { x : node . x ? node . x : 0 , y : node . y ? node . y : 0 } ,
78+ style : {
79+ borderRadius : '6px' ,
80+ borderWidth : '2px' ,
81+ borderColor : '#6b7280' ,
82+ display : 'flex' ,
83+ justifyContent : 'center' ,
84+ placeItems : 'center' ,
85+ backgroundColor : `${ ( node . data . isClientComponent ) ? '#fdba74' : '#93C5FD' } ` ,
86+ }
87+ } ) ;
88+
89+ // If the current node has a parent, create an edge to show relationship
90+ if ( node . data . parent ) {
91+ const newEdge = {
92+ id : `${ getNonce ( ) } ` ,
93+ source : node . data . parent ,
94+ target : node . data . id ,
95+ type : ConnectionLineType . Bezier ,
96+ animated : true ,
97+ } ;
98+
99+
100+ // Check if the edge already exists before adding
101+ const edgeExists = initialEdges . some (
102+ edge => edge . source === newEdge . source && edge . target === newEdge . target
103+ ) ;
104+
105+ // If edge does not exist, add to our edges array
106+ if ( ! edgeExists ) {
107+ initialEdges . push ( newEdge )
108+ }
109+ }
110+ }
111+ )
112+
113+ }
114+
46115 return (
116+ < div style = { { height : '600px' , width : '100%' } } >
47117 < ReactFlow
48118 nodes = { nodes }
49119 edges = { edges }
@@ -53,6 +123,7 @@ const OverviewFlow = () => {
53123 onInit = { onInit }
54124 fitView
55125 attributionPosition = "top-right"
126+ style = { { width : '100%' , height : '100%' } }
56127 >
57128 < MiniMap
58129 nodeStrokeColor = { ( n ) : string => {
@@ -80,6 +151,7 @@ const OverviewFlow = () => {
80151 < Controls />
81152 < Background color = "#aaa" gap = { 16 } />
82153 </ ReactFlow >
154+ </ div >
83155 ) ;
84156} ;
85157
0 commit comments