Skip to content

Commit 1bc8e1f

Browse files
committed
useTabHook, restore state and add skipHash
When nested, the activeTab should not sync with the location hash.
1 parent 12f7390 commit 1bc8e1f

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

frontend/src/components/result.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const ResultView = ({
3939
hideSummary=false,
4040
hideTestObject=false,
4141
hideTestHistory=false,
42-
testResult
42+
testResult,
43+
skipHash=false
4344
}) => {
4445
const context = useContext(IbutsuContext);
4546
const { darkTheme } = context;
@@ -83,7 +84,8 @@ const ResultView = ({
8384
// Tab state and navigation hooks/effects
8485
const {activeTab, onTabSelect} = useTabHook(
8586
['summary', 'testHistory', 'testObject', ...artifactKeys()],
86-
defaultTab
87+
defaultTab,
88+
skipHash
8789
);
8890

8991
useEffect(() => {
@@ -419,6 +421,7 @@ ResultView.propTypes = {
419421
hideTestObject: PropTypes.bool,
420422
hideTestHistory: PropTypes.bool,
421423
testResult: PropTypes.object,
424+
skipHash: PropTypes.bool,
422425
};
423426

424427
export default ResultView;

frontend/src/components/tabHook.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
import { useState, useEffect } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { useLocation, useNavigate } from 'react-router-dom';
33

44
// Assisted by watsonx Code Assistant
55
// Code generated by WCA@IBM in this programming language is not approved for use in IBM product development.
66

7-
export const useTabHook = (validTabIndicies=[], defaultTab) => {
7+
export const useTabHook = (validTabIndicies=[], defaultTab='summary', skipHash=false) => {
88

99
const location = useLocation();
1010
const navigate = useNavigate();
11-
const currentHash = location.hash.slice(1);
12-
const activeTab = validTabIndicies.includes(currentHash) ? currentHash : defaultTab;
11+
const [activeTab, setActiveTab] = useState(defaultTab);
12+
13+
useEffect(() => {
14+
if (!skipHash) {
15+
const currentHash = location.hash.slice(1);
16+
setActiveTab(validTabIndicies.includes(currentHash) ? currentHash : defaultTab);
17+
}
18+
}, [defaultTab, location.hash, skipHash, validTabIndicies]);
1319

1420
useEffect(() => {
1521
// catch a bad tab index and use the default
16-
if (!validTabIndicies.includes(location.hash.slice(1))) {navigate(`#${defaultTab}`);}
17-
}, [defaultTab, location.hash, navigate, validTabIndicies]);
22+
if (!skipHash && !validTabIndicies.includes(location.hash.slice(1))) {navigate(`#${defaultTab}`);}
23+
}, [defaultTab, location.hash, navigate, skipHash, validTabIndicies]);
1824

1925
const onTabSelect = (_, tabIndex) => {
20-
navigate(`#${tabIndex}`);
26+
if (!skipHash) {navigate(`#${tabIndex}`);};
27+
setActiveTab(tabIndex);
2128
};
2229

2330
return {activeTab, onTabSelect};

frontend/src/run.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import ClassifyFailuresTable from './components/classify-failures';
5555
import ArtifactTab from './components/artifact-tab';
5656
import { IbutsuContext } from './services/context';
5757
import { useTabHook } from './components/tabHook';
58+
import PropTypes from 'prop-types';
5859

5960

6061
const COLUMNS = ['Test', 'Run', 'Result', 'Duration', 'Started'];
@@ -238,7 +239,7 @@ const Run = ({defaultTab='summary'}) => {
238239
}
239240
{isRunValid &&
240241
<Tabs activeKey={activeTab} onSelect={onTabSelect} isBox>
241-
<Tab eventKey="summary" title={<TabTitle icon={<InfoCircleIcon />} text="Summary" />}>
242+
<Tab key= "summary" eventKey="summary" title={<TabTitle icon={<InfoCircleIcon />} text="Summary" />}>
242243
<Card>
243244
<CardBody style={{ padding: 0 }} id="run-detail">
244245
<Grid>
@@ -432,7 +433,7 @@ const Run = ({defaultTab='summary'}) => {
432433
</CardBody>
433434
</Card>
434435
</Tab>
435-
<Tab eventKey="results-list" title={<TabTitle icon={<CatalogIcon />} text="Results List" />}>
436+
<Tab key="results-list" eventKey="results-list" title={<TabTitle icon={<CatalogIcon />} text="Results List" />}>
436437
<Card className="pf-u-mt-lg">
437438
<CardHeader>
438439
<Flex style={{ width: '100%' }}>
@@ -489,7 +490,7 @@ const Run = ({defaultTab='summary'}) => {
489490
}
490491
</CardHeader>
491492
<CardBody style={{ backgroundColor: 'var(--pf-v5-c-card--BackgroundColor)', paddingTop: '1.2em' }}>
492-
<ResultView testResult={testResult} defaultTab='summary' />
493+
<ResultView testResult={testResult} skipHash={true} />
493494
</CardBody>
494495
</Card>
495496
}

0 commit comments

Comments
 (0)