Skip to content

Commit 01f3ee5

Browse files
committed
Updates for tab history handling
Using WCA@IBM!
1 parent 9b574db commit 01f3ee5

File tree

1 file changed

+33
-51
lines changed

1 file changed

+33
-51
lines changed

frontend/src/components/result.js

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { InfoCircleIcon, CodeIcon, SearchIcon, FileAltIcon } from '@patternfly/react-icons';
1919
import { 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';
2222
import Linkify from 'react-linkify';
2323

2424
import * as http from '../services/http';
@@ -33,7 +33,7 @@ import { IbutsuContext } from '../services/context';
3333

3434
const ResultView = ({
3535
comparisonResults,
36-
defaultTab,
36+
defaultTab='summary',
3737
hideArtifact=false,
3838
hideSummary=false,
3939
hideTestObject=false,
@@ -48,29 +48,11 @@ const ResultView = ({
4848
const [testHistoryTable, setTestHistoryTable] = useState(null);
4949

5050
// Hooks
51-
const navigate = useNavigate();
5251
const location = useLocation();
52+
const navigate = useNavigate();
5353

5454
// https://v5-archive.patternfly.org/components/tabs#tabs-linked-to-nav-elements
55-
const getDefaultTab = () => {
56-
if (defaultTab) {
57-
return defaultTab;
58-
}
59-
else if (!hideSummary) {
60-
return 'summary';
61-
}
62-
else if (!!artifactTabs.length > 0) {
63-
return artifactTabs[0].key;
64-
}
65-
else {
66-
return null;
67-
}
68-
};
69-
70-
const getTabIndex = useCallback((defaultValue) => (!!location && location.hash !== '') ? location.hash.substring(1) : defaultValue,[location]);
71-
72-
const [activeTab, setActiveTab] = useState(getTabIndex(getDefaultTab()));
73-
55+
const [activeTab, setActiveTab] = useState(location.hash.slice(1) || defaultTab);
7456

7557
const getTestHistoryTable = useCallback(() => {
7658
if (comparisonResults !== undefined) {
@@ -86,37 +68,20 @@ const ResultView = ({
8668

8769
}, [setTestHistoryTable, comparisonResults, testResult]);
8870

89-
90-
9171
useEffect(() => {
92-
if (activeTab === 'test-history') {
72+
if (activeTab === 'testHistory') {
9373
getTestHistoryTable();
9474
}
9575
}, [activeTab, getTestHistoryTable]);
9676

97-
const handlePopState = useCallback(() => {
98-
// Handle browser navigation buttons click
99-
const tabIndex = getTabIndex('summary');
100-
setActiveTab(tabIndex);
101-
}, [getTabIndex, setActiveTab]);
102-
103-
useEffect(()=>{
104-
if (activeTab === 'test-history') {
105-
getTestHistoryTable();
106-
}
107-
window.addEventListener('popstate', handlePopState);
108-
return () => {
109-
window.removeEventListener('popstate', handlePopState);
110-
};
111-
}, [activeTab, getTestHistoryTable, handlePopState]);
112-
77+
// Assisted by watsonx Code Assistant
78+
// Code generated by WCA@IBM in this programming language is not approved for use in IBM product development.
11379
const onTabSelect = (_, tabIndex) => {
114-
if (location) {
115-
navigate(`${location.pathname}${location.search}#${tabIndex}`);
116-
}
11780
setActiveTab(tabIndex);
81+
navigate(`#${tabIndex}`);
11882
};
11983

84+
12085
const artifactTabs = useMemo(() => artifacts.map((art) => (
12186
<Tab
12287
key={art.filename}
@@ -129,6 +94,26 @@ const ResultView = ({
12994
)
13095
), [artifacts]);
13196

97+
98+
// Assisted by watsonx Code Assistant
99+
// Code generated by WCA@IBM in this programming language is not approved for use in IBM product development.
100+
useEffect(() => {
101+
const handleHashChange = () => {
102+
const tabIndex = location.hash.slice(1);
103+
const validTabNames = [
104+
'summary', 'testHistory', 'testObject',
105+
artifactTabs.map((tab) => tab.key).filter((key) => key && key.trim() !== '')
106+
];
107+
setActiveTab(validTabNames.includes(tabIndex) ? tabIndex : defaultTab);
108+
};
109+
110+
window.addEventListener('hashchange', handleHashChange);
111+
112+
return () => {
113+
window.removeEventListener('hashchange', handleHashChange);
114+
};
115+
}, [artifactTabs, defaultTab, location.hash]);
116+
132117
useEffect(() => {
133118
// Get artifacts when the test result changes
134119
if (testResult) {
@@ -141,9 +126,6 @@ const ResultView = ({
141126
}
142127
}, [testResult]);
143128

144-
if (activeTab === null) {
145-
setActiveTab(getDefaultTab());
146-
}
147129
let resultIcon = getIconForResult('pending');
148130
let startTime = new Date();
149131
let parameters = <div/>;
@@ -429,15 +411,15 @@ const ResultView = ({
429411
</Card>
430412
</Tab>
431413
}
432-
{!hideArtifact &&
433-
artifactTabs}
434414
{!hideTestHistory &&
435-
<Tab key="test-history" eventKey="test-history" title={<TabTitle icon={<SearchIcon/>} text="Test History" href='#test-history'/>}>
415+
<Tab key="testHistory" eventKey="testHistory" title={<TabTitle icon={<SearchIcon/>} text="Test History" href='#testHistory'/>}>
436416
{testHistoryTable}
437417
</Tab>
438418
}
419+
{!hideArtifact &&
420+
artifactTabs}
439421
{!hideTestObject && testJson &&
440-
<Tab key="test-object" eventKey="test-object" title={<TabTitle icon={<CodeIcon/>} text="Test Object" href='#test-object'/>}>
422+
<Tab key="testObject" eventKey="testObject" title={<TabTitle icon={<CodeIcon/>} text="Test Object" href='#testObject'/>}>
441423
<Card>
442424
<CardBody id='object-card-body'>
443425
<CodeEditor

0 commit comments

Comments
 (0)