1+ import React from 'react' ;
2+ // will create a build func and then call the helper funcs to return an object
3+ // make a new instance of this class in flow, call the build method, and pass this as state
4+ class FlowBuilder {
5+ constructor ( data ) {
6+ this . parsedData = [ data ] ;
7+ this . id = 0 ;
8+ this . initialNodes = [ ] ;
9+ this . viewData ;
10+ this . edgeId = 0 ;
11+ this . initialEdges = [ ] ;
12+ }
13+
14+ buildNodesArray ( parsedData ) {
15+ if ( ! parsedData ) return ;
16+
17+ parsedData . forEach ( ( item ) => {
18+ const node = {
19+ id : ( ++ this . id ) . toString ( ) ,
20+ data : {
21+ label : (
22+ < div key = { this . id } >
23+ < ul >
24+ < li > { item . fileName } </ li >
25+ < li > Client Component: { item . isClientComponent . toString ( ) } </ li >
26+ </ ul >
27+ </ div >
28+ )
29+ } ,
30+ type : item . depth === 0 ? 'input' : '' ,
31+ position : { x : 0 , y : 0 } ,
32+ style : {
33+ backgroundColor : "var(--vscode-dropdown-background)" ,
34+ borderRadius : "15px" ,
35+ width : '265px' ,
36+ boxShadow : '0px 4px 4px rgba(0, 0, 0, 0.25)' ,
37+ border : 'none' ,
38+ padding : '10px 10px 3px 10px'
39+ } ,
40+ } ;
41+ this . initialNodes . push ( node ) ;
42+ if ( item . children ) {
43+ this . buildNodesArray ( item . children ) ;
44+ }
45+ } ) ;
46+ // console.log('initialNodes', this.initialNodes);
47+ } ;
48+
49+ buildEdgesArray ( parsedData , parentID ) {
50+ if ( ! parsedData ) return ;
51+
52+ parsedData . forEach ( ( item ) => {
53+ const nodeID = ++ this . edgeId ;
54+ if ( parentID ) {
55+ const edge = {
56+ id : `e${ parentID } -${ nodeID } ` ,
57+ source : parentID . toString ( ) ,
58+ target : nodeID . toString ( ) ,
59+ type : 'bezier' ,
60+ animated : false ,
61+ } ;
62+ this . initialEdges . push ( edge ) ;
63+ }
64+ if ( item . children ) {
65+ this . buildEdgesArray ( item . children , nodeID ) ;
66+ }
67+ } ) ;
68+ // console.log('initialEdges', this.initialEdges);
69+ }
70+
71+ build ( settings ) {
72+ const treeParsed = JSON . parse ( JSON . stringify ( this . parsedData [ 0 ] ) ) ;
73+ console . log ( 'settings: ' , settings ) ;
74+ const traverse = ( node ) => {
75+ let validChildren = [ ] ;
76+
77+ for ( let i = 0 ; i < node . children . length ; i ++ ) {
78+ if (
79+ node . children [ i ] . thirdParty &&
80+ settings . thirdParty &&
81+ ! node . children [ i ] . reactRouter
82+ ) {
83+ validChildren . push ( node . children [ i ] ) ;
84+ } else if ( node . children [ i ] . reactRouter && settings . reactRouter ) {
85+ validChildren . push ( node . children [ i ] ) ;
86+ } else if (
87+ ! node . children [ i ] . thirdParty &&
88+ ! node . children [ i ] . reactRouter
89+ ) {
90+ validChildren . push ( node . children [ i ] ) ;
91+ }
92+ }
93+
94+ // Update children with only valid nodes, and recurse through each node
95+ node . children = validChildren ;
96+ node . children . forEach ( ( child ) => {
97+ traverse ( child ) ;
98+ } ) ;
99+ }
100+ traverse ( treeParsed ) ;
101+ // Update the vewData state
102+ this . viewData = ( [ treeParsed ] ) ;
103+ console . log ( 'viewData:' , this . viewData ) ;
104+ this . buildNodesArray ( this . viewData ) ;
105+ this . buildEdgesArray ( this . viewData ) ;
106+ }
107+ }
108+
109+ export default FlowBuilder ;
0 commit comments