Skip to content

Commit 2df9454

Browse files
committed
Create custom useTabHook
Move buildTree Convert Run to useTabHook Use #summary hash for run hrefs
1 parent 0786a21 commit 2df9454

File tree

6 files changed

+194
-223
lines changed

6 files changed

+194
-223
lines changed

frontend/src/components/fileupload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const FileUpload = (props) => {
5050
let action = null;
5151
if (data.metadata.run_id) {
5252
const RunButton = () => (
53-
<AlertActionLink component='a' href={'/project/' + (data.metadata.project_id || primaryObject.id) + '/runs/' + data.metadata.run_id}>
53+
<AlertActionLink component='a' href={`/project/${(data.metadata.project_id || primaryObject.id)}/runs/${data.metadata.run_id}#summary`}>
5454
Go to Run
5555
</AlertActionLink>
5656
);

frontend/src/components/result.js

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useContext, useMemo } from 'react';
1+
import React, { useState, useEffect, useContext, useMemo, useCallback } 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, useLocation, useNavigate } from 'react-router-dom';
21+
import { Link } from 'react-router-dom';
2222
import Linkify from 'react-linkify';
2323

2424
import * as http from '../services/http';
@@ -30,6 +30,7 @@ import TabTitle from './tabs';
3030
import TestHistoryTable from './test-history';
3131
import ArtifactTab from './artifact-tab';
3232
import { IbutsuContext } from '../services/context';
33+
import { useTabHook } from './tabHook';
3334

3435
const ResultView = ({
3536
comparisonResults,
@@ -46,12 +47,7 @@ const ResultView = ({
4647
// State
4748
const [artifacts, setArtifacts] = useState([]);
4849

49-
// Hooks
50-
const location = useLocation();
51-
const navigate = useNavigate();
52-
5350
// https://v5-archive.patternfly.org/components/tabs#tabs-linked-to-nav-elements
54-
const [activeTab, setActiveTab] = useState(defaultTab);
5551

5652
const testHistoryTable = useMemo(() => {
5753
if (comparisonResults !== undefined) {
@@ -79,31 +75,16 @@ const ResultView = ({
7975
)
8076
), [artifacts]);
8177

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-
};
78+
const artifactKeys = useCallback(() => {
79+
if (artifactTabs && artifactTabs?.length !== 0) {return(artifactTabs.map((tab) => tab.key));}
80+
else {return([]);}
81+
}, [artifactTabs]);
9482

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
83+
// Tab state and navigation hooks/effects
84+
const {activeTab, onTabSelect} = useTabHook(
85+
['summary', 'testHistory', 'testObject', ...artifactKeys()],
86+
defaultTab
87+
);
10788

10889
useEffect(() => {
10990
// Get artifacts when the test result changes
@@ -125,7 +106,7 @@ const ResultView = ({
125106
resultIcon = getIconForResult(testResult.result);
126107
startTime = new Date(testResult.start_time);
127108
parameters = Object.keys(testResult.params).map((key) => <div key={key}>{key} = {testResult.params[key]}</div>);
128-
runLink = <Link to={`../runs/${testResult.run_id}`} relative="Path">{testResult.run_id}</Link>;
109+
runLink = <Link to={`../runs/${testResult.run_id}#summary`} relative="Path">{testResult.run_id}</Link>;
129110
}
130111

131112
const testJson = useMemo(() => JSON.stringify(testResult, null, '\t'), [testResult]);

frontend/src/components/tabHook.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useState, useEffect } from 'react';
2+
import { useLocation, useNavigate } from 'react-router-dom';
3+
4+
// Assisted by watsonx Code Assistant
5+
// Code generated by WCA@IBM in this programming language is not approved for use in IBM product development.
6+
7+
export const useTabHook = (validTabIndicies=[], defaultTab) => {
8+
9+
const location = useLocation();
10+
const navigate = useNavigate();
11+
const currentHash = location.hash.slice(1);
12+
const activeTab = validTabIndicies.includes(currentHash) ? currentHash : defaultTab;
13+
14+
useEffect(() => {
15+
// 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]);
18+
19+
const onTabSelect = (_, tabIndex) => {
20+
navigate(`#${tabIndex}`);
21+
};
22+
23+
return {activeTab, onTabSelect};
24+
};

frontend/src/run-list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const runToRow = (run, filterFunc) => {
7777
}
7878
return {
7979
'cells': [
80-
{title: <React.Fragment><Link to={`${run.id}`}>{run.id}</Link> {badges}</React.Fragment>},
80+
{title: <React.Fragment><Link to={`${run.id}#summary`}>{run.id}</Link> {badges}</React.Fragment>},
8181
{title: round(run.duration) + 's'},
8282
{title: <RunSummary summary={run.summary} />},
8383
{title: created.toLocaleString()},
@@ -442,7 +442,7 @@ const RunList = () => {
442442
onApplyFilter={applyFilter}
443443
onRemoveFilter={removeFilter}
444444
onClearFilters={clearFilters}
445-
onApplyReport={() => navigate('/project/' + params.project_id + '/reports?' + buildParams(filters).join('&'))}
445+
onApplyReport={() => navigate(`/project/${params.project_id}/reports?${buildParams(filters).join('&')}`)}
446446
onSetPage={setPage}
447447
onSetPageSize={setPageSize}
448448
hideFilters={['project_id']}

0 commit comments

Comments
 (0)