1- import React , { useState , useEffect , useCallback , useContext , useMemo } from 'react' ;
1+ import React , { useState , useEffect , useContext , useMemo } 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 , useNavigate , useLocation } from 'react-router-dom' ;
21+ import { Link , useLocation , useNavigate } from 'react-router-dom' ;
2222import Linkify from 'react-linkify' ;
2323
2424import * as http from '../services/http' ;
@@ -33,7 +33,7 @@ import { IbutsuContext } from '../services/context';
3333
3434const ResultView = ( {
3535 comparisonResults,
36- defaultTab,
36+ defaultTab= 'summary' ,
3737 hideArtifact= false ,
3838 hideSummary= false ,
3939 hideTestObject= false ,
@@ -45,74 +45,28 @@ const ResultView = ({
4545
4646 // State
4747 const [ artifacts , setArtifacts ] = useState ( [ ] ) ;
48- const [ testHistoryTable , setTestHistoryTable ] = useState ( null ) ;
4948
5049 // Hooks
51- const navigate = useNavigate ( ) ;
5250 const location = useLocation ( ) ;
51+ const navigate = useNavigate ( ) ;
5352
54- const getDefaultTab = ( ) => {
55- if ( defaultTab ) {
56- return defaultTab ;
57- }
58- else if ( ! hideSummary ) {
59- return 'summary' ;
60- }
61- else if ( ! ! artifactTabs . length > 0 ) {
62- return artifactTabs [ 0 ] . key ;
63- }
64- else {
65- return null ;
66- }
67- } ;
68-
69- const getTabIndex = useCallback ( ( defaultValue ) => ( ! ! location && location . hash !== '' ) ? location . hash . substring ( 1 ) : defaultValue , [ location ] ) ;
53+ // https://v5-archive.patternfly.org/components/tabs#tabs-linked-to-nav-elements
54+ const [ activeTab , setActiveTab ] = useState ( defaultTab ) ;
7055
71- const getTestHistoryTable = useCallback ( ( ) => {
56+ const testHistoryTable = useMemo ( ( ) => {
7257 if ( comparisonResults !== undefined ) {
73- setTestHistoryTable (
58+ return (
7459 < TestHistoryTable
7560 comparisonResults = { comparisonResults }
7661 testResult = { testResult }
7762 /> ) ;
7863 } else {
79- setTestHistoryTable (
64+ return (
8065 < TestHistoryTable testResult = { testResult } /> ) ;
8166 }
8267
83- } , [ setTestHistoryTable , comparisonResults , testResult ] ) ;
84-
85- const [ activeTab , setActiveTab ] = useState ( getTabIndex ( getDefaultTab ( ) ) ) ;
86-
87-
88- useEffect ( ( ) => {
89- if ( activeTab === 'test-history' ) {
90- getTestHistoryTable ( ) ;
91- }
92- } , [ activeTab , getTestHistoryTable ] ) ;
93-
94- const handlePopState = useCallback ( ( ) => {
95- // Handle browser navigation buttons click
96- const tabIndex = getTabIndex ( 'summary' ) ;
97- setActiveTab ( tabIndex ) ;
98- } , [ getTabIndex , setActiveTab ] ) ;
99-
100- useEffect ( ( ) => {
101- if ( activeTab === 'test-history' ) {
102- getTestHistoryTable ( ) ;
103- }
104- window . addEventListener ( 'popstate' , handlePopState ) ;
105- return ( ) => {
106- window . removeEventListener ( 'popstate' , handlePopState ) ;
107- } ;
108- } , [ activeTab , getTestHistoryTable , handlePopState ] ) ;
68+ } , [ comparisonResults , testResult ] ) ;
10969
110- const onTabSelect = ( _ , tabIndex ) => {
111- if ( location ) {
112- navigate ( `${ location . pathname } ${ location . search } #${ tabIndex } ` ) ;
113- }
114- setActiveTab ( tabIndex ) ;
115- } ;
11670
11771 const artifactTabs = useMemo ( ( ) => artifacts . map ( ( art ) => (
11872 < Tab
@@ -125,6 +79,32 @@ const ResultView = ({
12579 )
12680 ) , [ artifacts ] ) ;
12781
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+ } ;
94+
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
107+
128108 useEffect ( ( ) => {
129109 // Get artifacts when the test result changes
130110 if ( testResult ) {
@@ -137,9 +117,6 @@ const ResultView = ({
137117 }
138118 } , [ testResult ] ) ;
139119
140- if ( activeTab === null ) {
141- setActiveTab ( getDefaultTab ( ) ) ;
142- }
143120 let resultIcon = getIconForResult ( 'pending' ) ;
144121 let startTime = new Date ( ) ;
145122 let parameters = < div /> ;
@@ -425,15 +402,15 @@ const ResultView = ({
425402 </ Card >
426403 </ Tab >
427404 }
428- { ! hideArtifact &&
429- artifactTabs }
430405 { ! hideTestHistory &&
431- < Tab key = "test-history " eventKey = "test-history " title = { < TabTitle icon = { < SearchIcon /> } text = "Test History" /> } >
406+ < Tab key = "testHistory " eventKey = "testHistory " title = { < TabTitle icon = { < SearchIcon /> } text = "Test History" /> } >
432407 { testHistoryTable }
433408 </ Tab >
434409 }
410+ { ! hideArtifact &&
411+ artifactTabs }
435412 { ! hideTestObject && testJson &&
436- < Tab key = "test-object " eventKey = "test-object " title = { < TabTitle icon = { < CodeIcon /> } text = "Test Object" /> } >
413+ < Tab key = "testObject " eventKey = "testObject " title = { < TabTitle icon = { < CodeIcon /> } text = "Test Object" /> } >
437414 < Card >
438415 < CardBody id = 'object-card-body' >
439416 < CodeEditor
0 commit comments