1- import React , { useCallback , useEffect } from "react" ;
1+ import React , { useCallback , useEffect , useRef } from "react" ;
22import ReactFlow , {
33 addEdge ,
44 MiniMap ,
@@ -12,13 +12,19 @@ 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" ;
1518
1619const onInit = ( reactFlowInstance : ReactFlowInstance ) =>
1720 console . log ( "flow loaded:" , reactFlowInstance ) ;
1821
1922const OverviewFlow = ( ) => {
23+ const reactFlowWrapper = useRef < HTMLDivElement > ( null ) ;
2024 const initialNodes = [ ] ;
2125 const initialEdges = [ ] ;
26+ const elements = [ ] ;
27+ const edge = [ ] ;
2228
2329 const [ nodes , setNodes , onNodesChange ] = useNodesState ( initialNodes ) ;
2430 const [ edges , setEdges , onEdgesChange ] = useEdgesState ( initialEdges ) ;
@@ -33,26 +39,74 @@ const OverviewFlow = () => {
3339 const msg = e . data ; // object containing type prop and value prop
3440 switch ( msg . type ) {
3541 case 'parsed-data' : {
36- const results = new FlowBuilder ( msg . value ) ;
37- results . build ( msg . settings )
38- setNodes ( results . initialNodes ) ;
39- setEdges ( results . initialEdges ) ;
42+ // const results = new FlowBuilder(msg.value);
43+ // results.build(msg.settings)
44+ let data : object | undefined = msg . value ;
45+ console . log ( 'data' , data )
46+ // setNodes(results.initialNodes);
47+ // setEdges(results.initialEdges);
48+
49+ // create a holder for the heirarchical data (msg.value), data comes in an object of all the Trees
50+ const root : any = d3 . hierarchy ( data )
51+ console . log ( 'root' , root )
52+
53+
54+ //create tree layout and give nodes their positions
55+ const treeLayout = d3 . tree ( )
56+ treeLayout ( root ) ;
57+
58+ root . each ( ( node : any ) : void => {
59+ // console.log('node name',Object.keys(node))
60+ elements . push ( {
61+ id : node . data . name ,
62+ type : 'default' ,
63+ data : { label : node . data . name } ,
64+ position : { x : node . x || 0 , y : node . y || 0 } ,
65+ } ) ;
66+
67+ // figure out how to get edges to connect
68+ 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+ // }))
76+ }
77+ } )
4078 break ;
4179 }
4280 }
4381 } ) ;
82+
83+ setNodes ( elements ) ;
84+
85+ //Rendering reactFlow
86+ if ( reactFlowWrapper . current ) {
87+ // Set initial position for the nodes
88+ const initialPosition = { x : 0 , y : 0 } ;
89+ // reactFlowWrapper.current?.fitView(initialPosition);
90+ }
91+ // assign root variable using d3.heirarchy(data[0]), this will use d3 to format our data in a easy way to visualize the heirarchy
92+ // const elements: Tree[];
93+
4494 } , [ ] ) ;
4595
4696 return (
97+ < div style = { { height : '600px' , width : '100%' } } >
4798 < ReactFlow
4899 nodes = { nodes }
49- edges = { edges }
100+ // edges={edges}
101+ // nodes={elements}
102+ // edges={edge}
50103 onNodesChange = { onNodesChange }
51104 onEdgesChange = { onEdgesChange }
52105 onConnect = { onConnect }
53106 onInit = { onInit }
54107 fitView
55108 attributionPosition = "top-right"
109+ style = { { width : '100%' , height : '100%' } }
56110 >
57111 < MiniMap
58112 nodeStrokeColor = { ( n ) : string => {
@@ -80,6 +134,7 @@ const OverviewFlow = () => {
80134 < Controls />
81135 < Background color = "#aaa" gap = { 16 } />
82136 </ ReactFlow >
137+ </ div >
83138 ) ;
84139} ;
85140
0 commit comments