1
- import React , { Component } from 'react' ;
1
+ import React , { Component , useEffect , useState } from 'react' ;
2
2
import * as d3 from 'd3' ;
3
3
4
4
/**
@@ -31,53 +31,41 @@ const filterHooks = (data: any[]) => {
31
31
} ;
32
32
33
33
interface HistoryProps {
34
- hierarchy :any ;
34
+ hierarchy : Record < string , unknown > ;
35
35
}
36
36
37
- let root = { } ;
38
- class History extends Component {
39
- /**
40
- * @method maked3Tree :Creates a new D3 Tree
41
- */
42
- constructor ( props : HistoryProps ) {
43
- super ( props ) ;
44
- // what React.createRef() is doing.
45
- this . HistoryRef = React . createRef ( ) ;
46
- this . maked3Tree = this . maked3Tree . bind ( this ) ;
47
- this . removed3Tree = this . removed3Tree . bind ( this ) ;
48
- this . isRecoil = false ;
49
- }
50
37
51
- componentDidMount ( ) {
52
- const { hierarchy } = this . props ;
53
- root = JSON . parse ( JSON . stringify ( hierarchy ) ) ;
54
- this . maked3Tree ( ) ;
55
- }
38
+ /**
39
+ * @method maked3Tree :Creates a new D3 Tree
40
+ */
56
41
57
- componentDidUpdate ( ) {
58
- const { hierarchy } = this . props ;
59
- root = JSON . parse ( JSON . stringify ( hierarchy ) ) ;
60
- this . maked3Tree ( ) ;
61
- }
42
+ function History ( props ) {
43
+ let { hierarchy } = props ;
44
+ let root = JSON . parse ( JSON . stringify ( hierarchy ) ) ;
45
+ let isRecoil = false ;
46
+ let HistoryRef = root ; //React.createRef(root);
47
+
48
+ useEffect ( ( ) => {
49
+ maked3Tree ( ) ;
50
+ } , [ root ] ) ;
62
51
63
- removed3Tree ( ) {
64
- const { current } = this . HistoryRef ;
52
+ let removed3Tree = function ( ) {
53
+ const { current } = HistoryRef ;
65
54
while ( current . hasChildNodes ( ) ) {
66
55
current . removeChild ( current . lastChild ) ;
67
56
}
68
- }
57
+ } ;
69
58
70
59
/**
71
60
* @method maked3Tree Creates a new Tree History
72
61
* @var
73
62
*/
74
- maked3Tree ( ) : void {
75
- this . removed3Tree ( ) ;
76
- console . log ( "HIEARARCHY" , this . props . hierarchy )
77
- const width : number = 800 ;
78
- const height : number = 600 ;
63
+ let maked3Tree = function ( ) {
64
+ removed3Tree ( ) ;
65
+ const width : number = 800 ;
66
+ const height : number = 600 ;
79
67
const svgContainer = d3
80
- . select ( this . HistoryRef . current )
68
+ . select ( HistoryRef . current )
81
69
. append ( 'svg' ) // svgContainer is now pointing to svg
82
70
. attr ( 'width' , width )
83
71
. attr ( 'height' , height ) ;
@@ -93,9 +81,7 @@ class History extends Component {
93
81
94
82
const tree = d3
95
83
. tree ( )
96
-
97
84
. nodeSize ( [ width / 10 , height / 10 ] )
98
- // .separation(function (a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
99
85
. separation ( function ( a : { parent : object } , b : { parent : object } ) {
100
86
return a . parent == b . parent ? 2 : 2 ;
101
87
} ) ;
@@ -159,17 +145,9 @@ class History extends Component {
159
145
160
146
if ( d . data . stateSnapshot . children [ 0 ] . name === 'RecoilRoot' ) {
161
147
console . log ( 'enter' ) ;
162
- this . isRecoil = true ;
148
+ isRecoil = true ;
163
149
}
164
- console . log ( 'isRecoil' , this . isRecoil ) ;
165
-
166
- console . log ( 'd.data' , d . data ) ;
167
- console . log ( 'd.data.stateSnapshot' , d . data . stateSnapshot ) ;
168
- console . log (
169
- 'd.data.stateSnapshot.children' ,
170
- d . data . stateSnapshot . children
171
- ) ;
172
- if ( ! this . isRecoil ) {
150
+ if ( ! isRecoil ) {
173
151
tooltipDiv
174
152
. html ( filterHooks ( d . data . stateSnapshot . children ) , this )
175
153
. style ( 'left' , d3 . event . pageX - 90 + 'px' )
@@ -247,7 +225,7 @@ class History extends Component {
247
225
. on ( 'drag' , dragged )
248
226
. on ( 'end' , dragended )
249
227
) ;
250
-
228
+
251
229
function dragstarted ( ) {
252
230
d3 . select ( this ) . raise ( ) ;
253
231
g . attr ( 'cursor' , 'grabbing' ) ;
@@ -273,15 +251,13 @@ class History extends Component {
273
251
function reinfeldTidierAlgo ( x : number , y : number ) {
274
252
return [ ( y = + y ) * Math . cos ( ( x -= Math . PI / 2 ) ) , y * Math . sin ( x ) ] ;
275
253
}
276
- }
254
+ } ;
277
255
278
- render ( ) {
279
- return (
280
- < div className = "history-d3-container" >
281
- < div ref = { this . HistoryRef } className = "history-d3-div" />
282
- </ div >
283
- ) ;
284
- }
256
+ return (
257
+ < div className = "history-d3-container" >
258
+ < div ref = { HistoryRef } className = "history-d3-div" />
259
+ </ div >
260
+ ) ;
285
261
}
286
262
287
263
export default History ;
0 commit comments