1- import * as React from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22
33import TreeView from "@material-ui/lab/TreeView" ;
44
@@ -75,7 +75,7 @@ const getTreeItemsFromStepList = (stepsItems: StepInfo[]) => {
7575
7676const getTreeItemsFromStage = (
7777 stageItems : StageInfo [ ] ,
78- stageSteps : Map < String , StepInfo [ ] >
78+ stageSteps : Map < string , StepInfo [ ] >
7979) => {
8080 return stageItems . map ( ( stageItemData ) => {
8181 let children : JSX . Element [ ] = [ ] ;
@@ -105,7 +105,8 @@ interface DataTreeViewProps {
105105
106106interface State {
107107 stages : Array < StageInfo > ;
108- steps : Map < String , StepInfo [ ] > ;
108+ steps : Map < string , StepInfo [ ] > ;
109+ expanded : Array < string > ;
109110}
110111
111112export class DataTreeView extends React . Component {
@@ -117,7 +118,15 @@ export class DataTreeView extends React.Component {
117118 this . state = {
118119 stages : [ ] ,
119120 steps : new Map ( ) ,
121+ expanded : [ ] ,
120122 } ;
123+ this . handleToggle = this . handleToggle . bind ( this ) ;
124+ }
125+
126+ handleToggle ( event : React . ChangeEvent < { } > , nodeIds : string [ ] ) : void {
127+ this . setState ( {
128+ expanded : nodeIds ,
129+ } ) ;
121130 }
122131
123132 getStepsForStageTree ( stage : StageInfo ) : void {
@@ -139,14 +148,35 @@ export class DataTreeView extends React.Component {
139148 }
140149 }
141150
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+
142169 componentDidMount ( ) {
170+ let params = new URLSearchParams ( document . location . search . substring ( 1 ) ) ;
171+ let selectedNode = params . get ( "selected-node" ) || "" ;
143172 fetch ( "tree" )
144173 . then ( ( res ) => res . json ( ) )
145174 . then ( ( result ) => {
146175 // Get steps for a each stage and add to 'steps' state
147176 this . setState (
148177 {
149178 stages : result . data . stages ,
179+ expanded : this . getNodeHierarchy ( selectedNode , result . data . stages ) ,
150180 } ,
151181 ( ) => {
152182 this . state . stages . forEach ( ( stageData ) => {
@@ -168,6 +198,8 @@ export class DataTreeView extends React.Component {
168198 defaultCollapseIcon = { < ExpandMoreIcon /> }
169199 defaultExpandIcon = { < ChevronRightIcon /> }
170200 onNodeSelect = { this . props . onActionNodeSelect }
201+ expanded = { this . state . expanded }
202+ onNodeToggle = { this . handleToggle }
171203 >
172204 { getTreeItemsFromStage ( this . state . stages , this . state . steps ) }
173205 </ TreeView >
0 commit comments