Skip to content

Commit a505590

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

File tree

1 file changed

+37
-51
lines changed

1 file changed

+37
-51
lines changed

frontend/src/components/result.js

Lines changed: 37 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, useHistory } 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 history = useHistory();
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,24 @@ 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+
history.push({
82+
pathname: window.location.pathname,
83+
search: window.location.search,
84+
hash: `#${tabIndex}`
85+
});
11886
};
11987

88+
12089
const artifactTabs = useMemo(() => artifacts.map((art) => (
12190
<Tab
12291
key={art.filename}
@@ -129,6 +98,26 @@ const ResultView = ({
12998
)
13099
), [artifacts]);
131100

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

144-
if (activeTab === null) {
145-
setActiveTab(getDefaultTab());
146-
}
147133
let resultIcon = getIconForResult('pending');
148134
let startTime = new Date();
149135
let parameters = <div/>;
@@ -429,15 +415,15 @@ const ResultView = ({
429415
</Card>
430416
</Tab>
431417
}
432-
{!hideArtifact &&
433-
artifactTabs}
434418
{!hideTestHistory &&
435-
<Tab key="test-history" eventKey="test-history" title={<TabTitle icon={<SearchIcon/>} text="Test History" href='#test-history'/>}>
419+
<Tab key="testHistory" eventKey="testHistory" title={<TabTitle icon={<SearchIcon/>} text="Test History" href='#testHistory'/>}>
436420
{testHistoryTable}
437421
</Tab>
438422
}
423+
{!hideArtifact &&
424+
artifactTabs}
439425
{!hideTestObject && testJson &&
440-
<Tab key="test-object" eventKey="test-object" title={<TabTitle icon={<CodeIcon/>} text="Test Object" href='#test-object'/>}>
426+
<Tab key="testObject" eventKey="testObject" title={<TabTitle icon={<CodeIcon/>} text="Test Object" href='#testObject'/>}>
441427
<Card>
442428
<CardBody id='object-card-body'>
443429
<CodeEditor

0 commit comments

Comments
 (0)