@@ -7,7 +7,9 @@ import ReactFlow, {
77 Background ,
88 useNodesState ,
99 useEdgesState ,
10- ReactFlowInstance
10+ ReactFlowInstance ,
11+ Node ,
12+ Edge
1113} from "reactflow" ;
1214import "reactflow/dist/style.css" ;
1315import { ConnectionLineType } from "../types/connection" ;
@@ -16,14 +18,15 @@ import * as d3 from 'd3';
1618import { Tree } from "../types/tree" ;
1719import { hierarchyData } from "../types/hierarchyData" ;
1820import { getNonce } from "../getNonce" ;
21+ import { FlowNode } from "typescript" ;
1922
2023const onInit = ( reactFlowInstance : ReactFlowInstance ) =>
2124 console . log ( "flow loaded:" , reactFlowInstance ) ;
2225
2326const OverviewFlow = ( ) => {
2427 const reactFlowWrapper = useRef < HTMLDivElement > ( null ) ;
25- const initialNodes = [ ] ;
26- const initialEdges = [ ] ;
28+ const initialNodes : Node [ ] = [ ] ;
29+ const initialEdges : Edge [ ] = [ ] ;
2730
2831 const [ nodes , setNodes , onNodesChange ] = useNodesState ( [ ] ) ;
2932 const [ edges , setEdges , onEdgesChange ] = useEdgesState ( [ ] ) ;
@@ -35,19 +38,17 @@ const OverviewFlow = () => {
3538
3639
3740 useEffect ( ( ) => {
38- window . addEventListener ( 'message' , ( e ) => {
41+ window . addEventListener ( 'message' , ( e : MessageEvent ) => {
3942 // object containing type prop and value prop
40- const msg = e . data ;
43+ const msg : MessageEvent = e ;
44+ console . log ( e )
4145
42- switch ( msg . type ) {
46+ switch ( msg . data . type ) {
4347 case 'parsed-data' : {
44- let data : object | undefined = msg . value ;
45- console . log ( 'data' , data )
48+ let data : Tree | undefined = msg . data . value ;
4649 mappedData ( data )
47- console . log ( 'nodes' , initialNodes )
4850 setEdges ( initialEdges ) ;
4951 setNodes ( initialNodes )
50- console . log ( 'edges: ' , edges ) ;
5152 break ;
5253 }
5354 }
@@ -56,25 +57,33 @@ const OverviewFlow = () => {
5657
5758
5859 // Function that creates Tree Structure
59- function mappedData ( data ) {
60+ function mappedData ( data : Tree ) : void {
6061
6162 // 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 )
63+ const root : d3 . HierarchyNode < Tree > = d3 . hierarchy ( data )
64+
65+ // Dynamically adjust height and width of display depending on the amount of nodes
66+ const totalNodes : number = root . descendants ( ) . length ;
67+ const width : number = Math . max ( totalNodes * 100 , 800 ) ;
68+ const height = Math . max ( totalNodes * 20 , 500 )
69+
70+
71+ //create tree layout and give nodes their positions and
72+ const treeLayout : d3 . TreeLayout < unknown > = d3 . tree ( )
73+ . size ( [ width , height ] )
74+ . separation ( ( a : d3 . HierarchyPointNode < Node > , b : d3 . HierarchyPointNode < Node > ) => ( a . parent == b . parent ? 2 : 2.5 ) )
6475
65- //create tree layout and give nodes their positions
66- const treeLayout = d3 . tree ( ) . size ( [ 800 , 500 ] )
67- treeLayout ( root ) ;
6876
77+ treeLayout ( root ) ;
6978 // Iterate through each Tree and create a node
7079 root . each ( ( node : any ) : void => {
71-
80+ console . log ( 'Node' , node )
7281 // Create a Node from the current Root and add it to our nodes array
7382 initialNodes . push ( {
7483 id : node . data . id ,
84+ position : { x : node . x ? node . x : 0 , y : node . y ? node . y : 0 } ,
7585 type : 'default' ,
7686 data : { label : node . data . name } ,
77- position : { x : node . x ? node . x : 0 , y : node . y ? node . y : 0 } ,
7887 style : {
7988 borderRadius : '6px' ,
8089 borderWidth : '2px' ,
@@ -88,7 +97,7 @@ const OverviewFlow = () => {
8897
8998 // If the current node has a parent, create an edge to show relationship
9099 if ( node . data . parent ) {
91- const newEdge = {
100+ const newEdge : Edge = {
92101 id : `${ getNonce ( ) } ` ,
93102 source : node . data . parent ,
94103 target : node . data . id ,
@@ -98,7 +107,7 @@ const OverviewFlow = () => {
98107
99108
100109 // Check if the edge already exists before adding
101- const edgeExists = initialEdges . some (
110+ const edgeExists : boolean = initialEdges . some (
102111 edge => edge . source === newEdge . source && edge . target === newEdge . target
103112 ) ;
104113
0 commit comments