1
+ /* eslint-disable react/no-this-in-sfc */
1
2
/* eslint-disable no-unused-vars */
2
3
/* eslint-disable react/prop-types */
3
4
/* eslint-disable arrow-body-style */
14
15
/* eslint-disable indent */
15
16
/* eslint-disable no-console */
16
17
17
- import React , { useEffect , useState , useRef } from 'react' ;
18
+ import React , { useEffect , useState , useRef , useCallback } from 'react' ;
18
19
import * as d3 from 'd3' ;
19
20
// import { addNewSnapshots } from '../actions/actions';
20
21
@@ -46,7 +47,8 @@ const PerfView = ({ snapshots, viewIndex }) => {
46
47
. interpolate ( d3 . interpolateHcl ) ;
47
48
48
49
// set up circle-packing layout function
49
- const packFunc = data => d3 . pack ( )
50
+ const packFunc = useCallback ( data => {
51
+ return d3 . pack ( )
50
52
. size ( [ width , height ] )
51
53
. padding ( 3 ) ( d3 . hierarchy ( data )
52
54
. sum ( d => {
@@ -56,30 +58,31 @@ const PerfView = ({ snapshots, viewIndex }) => {
56
58
. sort ( ( a , b ) => {
57
59
// console.log('in sort func. a&b=', a, b);
58
60
return b . value - a . value ;
59
- } ) ) ;
60
-
61
- console . log ( 'packFunc' , packFunc ) ;
61
+ } ) ) ;
62
+ } , [ width , height ] ) ;
62
63
64
+ // first run, or user has made changes in their app; clear old tree and get current chartData
63
65
useEffect ( ( ) => {
64
66
console . log ( 'PerfView -> snapshots' , snapshots ) ;
65
67
console . log ( 'Current viewIndex: ' , viewIndex ) ;
66
68
for ( let i = 0 ; i < snapshots . length ; i ++ ) {
67
69
console . log ( `SNAPSHOT[${ i } ] App actualDuration:` , snapshots [ i ] . children [ 0 ] . componentData . actualDuration ) ;
68
70
}
69
71
70
- // empty old tree
72
+ // clear old tree
71
73
while ( svgRef . current . hasChildNodes ( ) ) {
72
74
svgRef . current . removeChild ( svgRef . current . lastChild ) ;
73
75
}
74
76
75
- if ( viewIndex < 0 ) {
76
- updateChartData ( snapshots [ snapshots . length - 1 ] ) ;
77
- console . log ( `Using snapshots[ ${ snapshots . length - 1 } ]` ) ;
78
- } else {
79
- updateChartData ( snapshots [ viewIndex ] ) ;
80
- console . log ( `Using snapshots[${ viewIndex } ]` ) ;
81
- }
77
+ let indexToDisplay = null ;
78
+ if ( viewIndex < 0 ) indexToDisplay = snapshots . length - 1 ;
79
+ else indexToDisplay = viewIndex ;
80
+
81
+ updateChartData ( snapshots [ indexToDisplay ] ) ;
82
+ console . log ( `Using snapshots[${ indexToDisplay } ]` ) ;
83
+ } , [ svgRef , viewIndex , snapshots , chartData ] ) ;
82
84
85
+ useEffect ( ( ) => {
83
86
console . log ( 'PerfView -> chartData' , chartData ) ;
84
87
85
88
// generate tree with our data
@@ -118,19 +121,18 @@ const PerfView = ({ snapshots, viewIndex }) => {
118
121
. style ( 'fill-opacity' , d => ( d . parent === packedRoot ? 1 : 0 ) )
119
122
. style ( 'display' , d => ( d . parent === packedRoot ? 'inline' : 'none' ) )
120
123
. text ( d => {
121
- console . log ( 'generating text label for d: ' , d ) ;
124
+ // console.log('generating text label for d: ', d);
122
125
return `${ d . data . name } : ${ Number . parseFloat ( d . data . componentData . actualDuration ) . toFixed ( 2 ) } ms` ;
123
126
} ) ;
124
127
125
128
label . exit ( ) . remove ( ) ;
126
129
node . exit ( ) . remove ( ) ;
127
130
128
- // console.log('PerfView -> label', label);
129
-
130
131
// jump to default zoom state
131
132
zoomTo ( [ packedRoot . x , packedRoot . y , packedRoot . r * 2 ] ) ;
132
133
133
134
function zoomTo ( v ) {
135
+ // console.log("zoomTo -> v", v);
134
136
const k = width / v [ 2 ] ;
135
137
view = v ;
136
138
label . attr ( 'transform' , d => `translate(${ ( d . x - v [ 0 ] ) * k } ,${ ( d . y - v [ 1 ] ) * k } )` ) ;
@@ -139,6 +141,7 @@ const PerfView = ({ snapshots, viewIndex }) => {
139
141
}
140
142
141
143
function zoom ( d ) {
144
+ // console.log("zoom -> d", d);
142
145
const focus0 = focus ;
143
146
focus = d ;
144
147
@@ -156,7 +159,7 @@ const PerfView = ({ snapshots, viewIndex }) => {
156
159
. on ( 'start' , function ( d ) { if ( d . parent === focus ) this . style . display = 'inline' ; } )
157
160
. on ( 'end' , function ( d ) { if ( d . parent !== focus ) this . style . display = 'none' ; } ) ;
158
161
}
159
- } , [ snapshots . length , height , width , viewIndex ] ) ;
162
+ } , [ chartData , color , packFunc , width , height ] ) ;
160
163
161
164
return < svg className = "perfContainer" ref = { svgRef } /> ;
162
165
} ;
0 commit comments