1
- import * as React from "react" ;
1
+ import React , { useEffect , useState } from "react" ;
2
2
3
3
import TreeView from "@material-ui/lab/TreeView" ;
4
4
@@ -75,7 +75,7 @@ const getTreeItemsFromStepList = (stepsItems: StepInfo[]) => {
75
75
76
76
const getTreeItemsFromStage = (
77
77
stageItems : StageInfo [ ] ,
78
- stageSteps : Map < String , StepInfo [ ] >
78
+ stageSteps : Map < string , StepInfo [ ] >
79
79
) => {
80
80
return stageItems . map ( ( stageItemData ) => {
81
81
let children : JSX . Element [ ] = [ ] ;
@@ -105,7 +105,8 @@ interface DataTreeViewProps {
105
105
106
106
interface State {
107
107
stages : Array < StageInfo > ;
108
- steps : Map < String , StepInfo [ ] > ;
108
+ steps : Map < string , StepInfo [ ] > ;
109
+ expanded : Array < string > ;
109
110
}
110
111
111
112
export class DataTreeView extends React . Component {
@@ -117,7 +118,15 @@ export class DataTreeView extends React.Component {
117
118
this . state = {
118
119
stages : [ ] ,
119
120
steps : new Map ( ) ,
121
+ expanded : [ ] ,
120
122
} ;
123
+ this . handleToggle = this . handleToggle . bind ( this ) ;
124
+ }
125
+
126
+ handleToggle ( event : React . ChangeEvent < { } > , nodeIds : string [ ] ) : void {
127
+ this . setState ( {
128
+ expanded : nodeIds ,
129
+ } ) ;
121
130
}
122
131
123
132
getStepsForStageTree ( stage : StageInfo ) : void {
@@ -139,14 +148,35 @@ export class DataTreeView extends React.Component {
139
148
}
140
149
}
141
150
151
+ getNodeHierarchy ( nodeId : string , stages : StageInfo [ ] ) : Array < string > {
152
+ for ( let i = 0 ; i < stages . length ; i ++ ) {
153
+ let stage = stages [ i ] ;
154
+ if ( String ( stage . id ) == nodeId ) {
155
+ // Found the node, so start a list of expanded nodes - it will be this and it's ancestors.
156
+ return [ String ( stage . id ) ] ;
157
+ } else if ( stage . children && stage . children . length > 0 ) {
158
+ let expandedNodes = this . getNodeHierarchy ( nodeId , stage . children ) ;
159
+ if ( expandedNodes . length > 0 ) {
160
+ // Our child is expanded, so we need to be expanded too.
161
+ expandedNodes . push ( String ( stage . id ) ) ;
162
+ return expandedNodes ;
163
+ }
164
+ }
165
+ }
166
+ return [ ] ;
167
+ }
168
+
142
169
componentDidMount ( ) {
170
+ let params = new URLSearchParams ( document . location . search . substring ( 1 ) ) ;
171
+ let selectedNode = params . get ( "selected-node" ) || "" ;
143
172
fetch ( "tree" )
144
173
. then ( ( res ) => res . json ( ) )
145
174
. then ( ( result ) => {
146
175
// Get steps for a each stage and add to 'steps' state
147
176
this . setState (
148
177
{
149
178
stages : result . data . stages ,
179
+ expanded : this . getNodeHierarchy ( selectedNode , result . data . stages ) ,
150
180
} ,
151
181
( ) => {
152
182
this . state . stages . forEach ( ( stageData ) => {
@@ -168,6 +198,8 @@ export class DataTreeView extends React.Component {
168
198
defaultCollapseIcon = { < ExpandMoreIcon /> }
169
199
defaultExpandIcon = { < ChevronRightIcon /> }
170
200
onNodeSelect = { this . props . onActionNodeSelect }
201
+ expanded = { this . state . expanded }
202
+ onNodeToggle = { this . handleToggle }
171
203
>
172
204
{ getTreeItemsFromStage ( this . state . stages , this . state . steps ) }
173
205
</ TreeView >
0 commit comments