1- import React , { useState , useEffect , useContext , useMemo } from 'react' ;
1+ import React , { useState , useEffect , useContext , useMemo , useCallback } from 'react' ;
22import PropTypes from 'prop-types' ;
33
44import {
@@ -18,7 +18,7 @@ import {
1818import { InfoCircleIcon , CodeIcon , SearchIcon , FileAltIcon } from '@patternfly/react-icons' ;
1919import { CodeEditor , Language } from '@patternfly/react-code-editor' ;
2020
21- import { Link , useLocation , useNavigate } from 'react-router-dom' ;
21+ import { Link } from 'react-router-dom' ;
2222import Linkify from 'react-linkify' ;
2323
2424import * as http from '../services/http' ;
@@ -30,6 +30,7 @@ import TabTitle from './tabs';
3030import TestHistoryTable from './test-history' ;
3131import ArtifactTab from './artifact-tab' ;
3232import { IbutsuContext } from '../services/context' ;
33+ import { useTabHook } from './tabHook' ;
3334
3435const ResultView = ( {
3536 comparisonResults,
@@ -46,12 +47,7 @@ const ResultView = ({
4647 // State
4748 const [ artifacts , setArtifacts ] = useState ( [ ] ) ;
4849
49- // Hooks
50- const location = useLocation ( ) ;
51- const navigate = useNavigate ( ) ;
52-
5350 // https://v5-archive.patternfly.org/components/tabs#tabs-linked-to-nav-elements
54- const [ activeTab , setActiveTab ] = useState ( defaultTab ) ;
5551
5652 const testHistoryTable = useMemo ( ( ) => {
5753 if ( comparisonResults !== undefined ) {
@@ -79,31 +75,16 @@ const ResultView = ({
7975 )
8076 ) , [ artifacts ] ) ;
8177
82- // Assisted by watsonx Code Assistant
83- // Code generated by WCA@IBM in this programming language is not approved for use in IBM product development.
84- useEffect ( ( ) => {
85- const handleHashChange = ( ) => {
86- const tabIndex = location . hash . slice ( 1 ) ;
87- const validTabIndex = [
88- 'summary' , 'testHistory' , 'testObject' ,
89- ...artifactTabs . map ( ( tab ) => tab . key ) ] . includes ( tabIndex ) ;
90- const newTab = validTabIndex ? tabIndex : defaultTab ;
91-
92- setActiveTab ( newTab ) ;
93- } ;
78+ const artifactKeys = useCallback ( ( ) => {
79+ if ( artifactTabs && artifactTabs ?. length !== 0 ) { return ( artifactTabs . map ( ( tab ) => tab . key ) ) ; }
80+ else { return ( [ ] ) ; }
81+ } , [ artifactTabs ] ) ;
9482
95- window . addEventListener ( 'hashchange' , handleHashChange ) ;
96-
97- return ( ) => {
98- window . removeEventListener ( 'hashchange' , handleHashChange ) ;
99- } ;
100- } , [ artifactTabs , defaultTab , location . hash ] ) ;
101-
102- const onTabSelect = ( _ , tabIndex ) => {
103- navigate ( `#${ tabIndex } ` ) ;
104- setActiveTab ( tabIndex ) ;
105- } ;
106- // end of assisted block
83+ // Tab state and navigation hooks/effects
84+ const { activeTab, onTabSelect} = useTabHook (
85+ [ 'summary' , 'testHistory' , 'testObject' , ...artifactKeys ( ) ] ,
86+ defaultTab
87+ ) ;
10788
10889 useEffect ( ( ) => {
10990 // Get artifacts when the test result changes
@@ -125,7 +106,7 @@ const ResultView = ({
125106 resultIcon = getIconForResult ( testResult . result ) ;
126107 startTime = new Date ( testResult . start_time ) ;
127108 parameters = Object . keys ( testResult . params ) . map ( ( key ) => < div key = { key } > { key } = { testResult . params [ key ] } </ div > ) ;
128- runLink = < Link to = { `../runs/${ testResult . run_id } ` } relative = "Path" > { testResult . run_id } </ Link > ;
109+ runLink = < Link to = { `../runs/${ testResult . run_id } #summary ` } relative = "Path" > { testResult . run_id } </ Link > ;
129110 }
130111
131112 const testJson = useMemo ( ( ) => JSON . stringify ( testResult , null , '\t' ) , [ testResult ] ) ;
0 commit comments