Skip to content

Commit 0786a21

Browse files
committed
Updates for tab history handling
Using WCA@IBM! Add default tab hashes to result paths trying to update the hash after navigation via effect within ResultView ends up making the history ugly regardless of use of navigation hooks on tab selection. Just set the hash on the initial target for summary
1 parent 6485809 commit 0786a21

File tree

4 files changed

+44
-67
lines changed

4 files changed

+44
-67
lines changed

frontend/src/components/classify-failures.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const resultToClassificationRow = (result, index, filterFunc) => {
7676
'isOpen': false,
7777
'result': result,
7878
'cells': [
79-
{title: <React.Fragment><Link to={`../results/${result.id}`} relative="Path">{result.test_id}</Link> {markers}</React.Fragment>},
79+
{title: <React.Fragment><Link to={`../results/${result.id}#summary`} relative="Path">{result.test_id}</Link> {markers}</React.Fragment>},
8080
{title: <span className={result.result}>{resultIcon} {toTitleCase(result.result)}</span>},
8181
{title: <React.Fragment>{exceptionBadge}</React.Fragment>},
8282
{title: <ClassificationDropdown testResult={result} />},

frontend/src/components/last-passed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const LastPassed = (props) => {
3939
return (
4040
<React.Fragment>
4141
{resultData &&
42-
<Link target="_blank" rel="noopener noreferrer" to={`../results/${resultData.id}`} relative="Path">
42+
<Link target="_blank" rel="noopener noreferrer" to={`../results/${resultData.id}#summary`} relative="Path">
4343
<Badge isRead>
4444
{new Date(resultData.start_time).toLocaleString()}
4545
</Badge>

frontend/src/components/result.js

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useCallback, useContext, useMemo } from 'react';
1+
import React, { useState, useEffect, useContext, useMemo } from 'react';
22
import PropTypes from 'prop-types';
33

44
import {
@@ -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,
@@ -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

frontend/src/utilities.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export function resultToRow (result, filterFunc) {
206206
}
207207
return {
208208
'cells': [
209-
{title: <React.Fragment><Link to={`../results/${result.id}`} relative="Path" key={result.id}>{result.test_id}</Link> {markers}</React.Fragment>},
209+
{title: <React.Fragment><Link to={`../results/${result.id}#summary`} relative="Path" key={result.id}>{result.test_id}</Link> {markers}</React.Fragment>},
210210
{title: runLink},
211211
{title: <React.Fragment><span className={result.result}>{resultIcon} {toTitleCase(result.result)}</span> {classification}</React.Fragment>},
212212
{title: round(result.duration) + 's'},
@@ -235,7 +235,7 @@ export function resultToComparisonRow (result, index) {
235235
}
236236

237237
let cells = [];
238-
cells.push({title: <React.Fragment><Link to={`../results/${result[0].id}`} relative="Path">{result[0].test_id}</Link> {markers}</React.Fragment>});
238+
cells.push({title: <React.Fragment><Link to={`../results/${result[0].id}#summary`} relative="Path">{result[0].test_id}</Link> {markers}</React.Fragment>});
239239
result.forEach((result, index) => {
240240
cells.push({title: <span className={result.result}>{resultIcons[index]} {toTitleCase(result.result)}</span>});
241241
});

0 commit comments

Comments
 (0)