1- import React , { useContext , useEffect , useState } from 'react' ;
1+ import React , { useContext , useEffect , useState , useMemo } from 'react' ;
22import PropTypes from 'prop-types' ;
33import {
44 Switch ,
@@ -16,7 +16,8 @@ import FilterHeatmapWidget from '../widgets/filterheatmap';
1616import { HEATMAP_MAX_BUILDS } from '../constants' ;
1717import { IbutsuContext } from '../services/context' ;
1818import ParamDropdown from '../components/param-dropdown' ;
19- import { useLocation , useNavigate , useSearchParams } from 'react-router-dom' ;
19+ import { useSearchParams } from 'react-router-dom' ;
20+ import { useTabHook } from '../components/tabHook' ;
2021
2122const DEFAULT_BAR = 8 ;
2223
@@ -25,17 +26,14 @@ const LONG_BUILDS = [...SHORT_BUILDS, 70, 150];
2526
2627const PF_BACK_100 = 'var(--pf-v5-global--BackgroundColor--100)' ;
2728
28- const JenkinsJobAnalysisView = ( props ) => {
29+ const JenkinsJobAnalysisView = ( { view , defaultTab = 'heatmap' } ) => {
2930 const context = useContext ( IbutsuContext ) ;
3031 const { primaryObject} = context ;
31- const { view} = props ;
32- const location = useLocation ( ) ;
33- const navigate = useNavigate ( ) ;
32+
3433 const [ searchParams ] = useSearchParams ( ) ;
3534
3635 const [ isAreaChart , setIsAreaChart ] = useState ( false ) ;
3736 const [ isLoading , setIsLoading ] = useState ( ) ;
38- const [ activeTab , setActiveTab ] = useState ( 'heatmap' ) ;
3937
4038 const [ barWidth , setBarWidth ] = useState ( DEFAULT_BAR ) ;
4139 const [ builds , setBuilds ] = useState ( 20 ) ;
@@ -45,6 +43,12 @@ const JenkinsJobAnalysisView =(props) => {
4543 const [ barchartParams , setBarchartParams ] = useState ( { } ) ;
4644 const [ linechartParams , setLinechartParams ] = useState ( { } ) ;
4745
46+ // Tab state and navigation hooks/effects
47+ const { activeTab, onTabSelect} = useTabHook (
48+ [ 'heatmap' , 'overall-health' , 'build-durations' ] ,
49+ defaultTab
50+ ) ;
51+
4852 useEffect ( ( ) => {
4953 // Fetch the widget parameters for heatmap, barchart and linechart
5054 if ( view ) {
@@ -112,10 +116,35 @@ const JenkinsJobAnalysisView =(props) => {
112116 return color ;
113117 } ;
114118
115- const onTabSelect = ( _ , tabIndex ) => {
116- navigate ( `${ location . pathname } ${ location . search } #${ tabIndex } ` ) ;
117- setActiveTab ( tabIndex ) ;
118- } ;
119+ const heatmapParam = useMemo ( ( ) => {
120+ if ( activeTab === 'heatmap' ) {
121+ return (
122+ < div style = { { backgroundColor : PF_BACK_100 , float : 'right' , clear : 'none' , marginBottom : '-2em' , padding : '0.2em 1em' , width : '30em' } } >
123+ < ParamDropdown
124+ dropdownItems = { [ 'Yes' , 'No' ] }
125+ defaultValue = { ( countSkips ? 'Yes' : 'No' ) }
126+ handleSelect = { ( value ) => setCountSkips ( value === 'Yes' ) }
127+ tooltip = "Count skips as failure:"
128+ />
129+ </ div > )
130+ }
131+ } , [ activeTab ] ) ;
132+
133+ const overallSwitch = useMemo ( ( ) => {
134+ if ( activeTab === 'overall-health' ) {
135+ return (
136+ < div style = { { backgroundColor : PF_BACK_100 , float : 'right' , clear : 'none' , marginBottom : '-2em' , padding : '0.5em 1em' } } >
137+ < Switch
138+ id = "bar-chart-switch"
139+ labelOff = "Change to Area Chart"
140+ label = "Change to Bar Chart"
141+ isChecked = { isAreaChart }
142+ onChange = { ( _ , checked ) => setIsAreaChart ( checked ) }
143+ />
144+ </ div >
145+ )
146+ }
147+ } , [ activeTab ] ) ;
119148
120149 return (
121150 < React . Fragment >
@@ -127,29 +156,10 @@ const JenkinsJobAnalysisView =(props) => {
127156 tooltip = "Number of builds:"
128157 />
129158 </ div >
130- { activeTab === 'heatmap' &&
131- < div style = { { backgroundColor : PF_BACK_100 , float : 'right' , clear : 'none' , marginBottom : '-2em' , padding : '0.2em 1em' , width : '30em' } } >
132- < ParamDropdown
133- dropdownItems = { [ 'Yes' , 'No' ] }
134- defaultValue = { ( countSkips ? 'Yes' : 'No' ) }
135- handleSelect = { ( value ) => setCountSkips ( value === 'Yes' ) }
136- tooltip = "Count skips as failure:"
137- />
138- </ div >
139- }
140- { activeTab === 'overall-health' &&
141- < div style = { { backgroundColor : PF_BACK_100 , float : 'right' , clear : 'none' , marginBottom : '-2em' , padding : '0.5em 1em' } } >
142- < Switch
143- id = "bar-chart-switch"
144- labelOff = "Change to Area Chart"
145- label = "Change to Bar Chart"
146- isChecked = { isAreaChart }
147- onChange = { ( _ , checked ) => setIsAreaChart ( checked ) }
148- />
149- </ div >
150- }
159+ { heatmapParam }
160+ { overallSwitch }
151161 < Tabs activeKey = { activeTab } onSelect = { onTabSelect } isBox >
152- < Tab eventKey = 'heatmap' title = "Heatmap" >
162+ < Tab key = 'heatmap' eventKey = 'heatmap' title = "Heatmap" >
153163 { ! isLoading && activeTab === 'heatmap' &&
154164 < FilterHeatmapWidget
155165 title = { heatmapParams . job_name }
@@ -160,7 +170,7 @@ const JenkinsJobAnalysisView =(props) => {
160170 />
161171 }
162172 </ Tab >
163- < Tab eventKey = 'overall-health' title = "Overall Health" >
173+ < Tab key = 'overall-health' eventKey = 'overall-health' title = "Overall Health" >
164174 { ! isLoading && ! isAreaChart && activeTab === 'overall-health' &&
165175 < GenericBarWidget
166176 title = { 'Test counts for ' + barchartParams . job_name }
@@ -211,7 +221,7 @@ const JenkinsJobAnalysisView =(props) => {
211221 />
212222 }
213223 </ Tab >
214- < Tab eventKey = 'build-durations' title = "Build Duration" >
224+ < Tab key = 'build-durations' eventKey = 'build-durations' title = "Build Duration" >
215225 { ! isLoading && activeTab === 'build-durations' &&
216226 < GenericAreaWidget
217227 title = { 'Durations for ' + linechartParams . job_name }
@@ -239,7 +249,8 @@ const JenkinsJobAnalysisView =(props) => {
239249} ;
240250
241251JenkinsJobAnalysisView . propTypes = {
242- view : PropTypes . object
252+ view : PropTypes . object ,
253+ defaultTab : PropTypes . string
243254} ;
244255
245256export default JenkinsJobAnalysisView ;
0 commit comments