Skip to content

Commit 0577883

Browse files
Merge pull request #191 from alanconway/hook-refactor
NO-JIRA: refactor: Separate custom hooks for re-use
2 parents 1c3fa3f + 78c4f19 commit 0577883

7 files changed

Lines changed: 70 additions & 54 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ ORG ?= openshift-observability-ui
55
IMAGE ?= quay.io/${ORG}/troubleshooting-panel-console-plugin:${VERSION}
66
TAG ?= $(VERSION)
77

8+
.PHONY: all
9+
all: build-frontend build-backend test-frontend
10+
811
.PHONY: test
912
test: test-frontend
1013

web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"lint": "eslint ./src ./integration-tests --fix && stylelint \"src/**/*.css\" --allow-empty-input --fix",
1717
"lint:tsc": "tsc --noEmit",
1818
"test:e2e": "cd ./integration-tests && cypress open --env openshift=true",
19-
"test:unit": "jest --runInBand --config jest.config.js",
19+
"test:unit": "jest --runInBand --logHeapUsage --config jest.config.js",
2020
"test:unit:watch": "jest --config jest.config.js --watch",
2121
"test:unit:coverage": "jest --config jest.config.js --coverage"
2222
},

web/src/components/Korrel8rPanel.tsx

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import { useDispatch, useSelector } from 'react-redux';
2424
import { useLocationQuery } from '../hooks/useLocationQuery';
2525
import { usePluginAvailable } from '../hooks/usePluginAvailable';
2626
import { getGoalsGraph, getNeighborsGraph } from '../korrel8r-client';
27-
import { AlertDomain } from '../korrel8r/alert';
28-
import { allDomains } from '../korrel8r/all-domains';
2927
import * as api from '../korrel8r/client';
3028
import * as korrel8r from '../korrel8r/types';
3129
import {
@@ -51,16 +49,7 @@ export default function Korrel8rPanel() {
5149
});
5250
const dispatch = useDispatch();
5351

54-
const alertRules = useSelector((state: State) => state.observe?.get('rules'));
55-
const alertIDs = React.useMemo(() => {
56-
if (!alertRules) return new Map<string, string>();
57-
return new Map<string, string>(alertRules.map(({ id, name }) => [id, name]));
58-
}, [alertRules]);
59-
const domains = React.useMemo(
60-
() => new korrel8r.Domains(...allDomains, new AlertDomain(alertIDs)),
61-
[alertIDs],
62-
);
63-
const locationQuery = useLocationQuery(domains);
52+
const locationQuery = useLocationQuery();
6453

6554
// Search parameters.
6655
const [search, setSearch] = React.useState<Search>({
@@ -232,13 +221,7 @@ export default function Korrel8rPanel() {
232221
);
233222

234223
const topologySection = (
235-
<Topology
236-
domains={domains}
237-
result={result}
238-
constraint={search.constraint}
239-
t={t}
240-
setSearch={setSearch}
241-
/>
224+
<Topology result={result} constraint={search.constraint} t={t} setSearch={setSearch} />
242225
);
243226

244227
return (
@@ -260,14 +243,13 @@ export default function Korrel8rPanel() {
260243
}
261244

262245
interface TopologyProps {
263-
domains: korrel8r.Domains;
264246
result?: Result;
265247
constraint: korrel8r.Constraint;
266248
t: TFunction;
267249
setSearch: (search: Search) => void;
268250
}
269251

270-
const Topology: React.FC<TopologyProps> = ({ domains, result, t, constraint }) => {
252+
const Topology: React.FC<TopologyProps> = ({ result, t, constraint }) => {
271253
const [loggingAvailable, loggingAvailableLoading] = usePluginAvailable('logging-view-plugin');
272254
const [netobserveAvailable, netobserveAvailableLoading] = usePluginAvailable('netobserv-plugin');
273255

@@ -280,7 +262,6 @@ const Topology: React.FC<TopologyProps> = ({ domains, result, t, constraint }) =
280262
// Non-empty graph
281263
return (
282264
<Korrel8rTopology
283-
domains={domains}
284265
graph={result.graph}
285266
loggingAvailable={loggingAvailable}
286267
netobserveAvailable={netobserveAvailable}

web/src/components/topology/Korrel8rTopology.tsx

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from '@patternfly/react-topology';
3434
import * as React from 'react';
3535
import { useTranslation } from 'react-i18next';
36-
import { useNavigate } from 'react-router-dom-v5-compat';
36+
import { useNavigateToQuery } from '../../hooks/useNavigateToQuery';
3737
import * as korrel8r from '../../korrel8r/types';
3838
import { getIcon } from '../icons';
3939
import './korrel8rtopology.css';
@@ -93,14 +93,13 @@ const NODE_DIAMETER = 75;
9393
const PADDING = 30;
9494

9595
export const Korrel8rTopology: React.FC<{
96-
domains: korrel8r.Domains;
9796
graph: korrel8r.Graph;
9897
loggingAvailable: boolean;
9998
netobserveAvailable: boolean;
10099
constraint: korrel8r.Constraint;
101-
}> = ({ domains, graph, loggingAvailable, netobserveAvailable, constraint }) => {
100+
}> = ({ graph, loggingAvailable, netobserveAvailable, constraint }) => {
102101
const { t } = useTranslation('plugin__troubleshooting-panel-console-plugin');
103-
const navigate = useNavigate();
102+
const navigateToQuery = useNavigateToQuery();
104103
const [selectedIds, setSelectedIds] = React.useState<string[]>([]);
105104

106105
const nodes = React.useMemo(
@@ -141,31 +140,6 @@ export const Korrel8rTopology: React.FC<{
141140
[graph],
142141
);
143142

144-
const navigateToQuery = React.useCallback(
145-
(query: korrel8r.Query, constraint: korrel8r.Constraint) => {
146-
try {
147-
let link = domains.queryToLink(query, constraint)?.toString();
148-
if (!link) return;
149-
if (!link.startsWith('/')) link = '/' + link;
150-
// eslint-disable-next-line no-console
151-
console.debug(
152-
'korrel8r navigate',
153-
'\nquery',
154-
query,
155-
'\nconstraint',
156-
constraint,
157-
'\nlink',
158-
link,
159-
);
160-
navigate(link);
161-
} catch (e) {
162-
// eslint-disable-next-line no-console
163-
console.error(`korrel8r navigateToQuery: ${e} `, '\nquery', query);
164-
}
165-
},
166-
[navigate, domains],
167-
);
168-
169143
const selectionAction = React.useCallback(
170144
(selected: Array<string>) => {
171145
const id = selected?.[0]; // Select only one at a time.

web/src/hooks/useDomains.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react';
2+
import { useSelector } from 'react-redux';
3+
import { AlertDomain } from '../korrel8r/alert';
4+
import { allDomains } from '../korrel8r/all-domains';
5+
import { Domains } from '../korrel8r/types';
6+
import { State } from '../redux-reducers';
7+
8+
// The alert domain is dependent on alert Rules state , so we need a hook for domains
9+
export const useDomains = () => {
10+
const alertRules = useSelector((state: State) => state.observe?.get('rules'));
11+
12+
const alertIDs = React.useMemo(() => {
13+
if (!alertRules) return new Map<string, string>();
14+
return new Map<string, string>(alertRules.map(({ id, name }) => [id, name]));
15+
}, [alertRules]);
16+
17+
const domains = React.useMemo(() => {
18+
// Alert domain with IDs loaded from state.
19+
const alert = new AlertDomain(alertIDs);
20+
// Replace the default alert domain.
21+
return new Domains(...allDomains.filter((d) => d.name !== alert.name), alert);
22+
}, [alertIDs]);
23+
return domains;
24+
};

web/src/hooks/useLocationQuery.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useLocation } from 'react-router-dom-v5-compat';
2-
import { Domains, Query, URIRef } from '../korrel8r/types';
2+
import { Query, URIRef } from '../korrel8r/types';
3+
import { useDomains } from './useDomains';
34

45
/** Returns the Korrel8r query for the current browser location or undefined */
5-
export const useLocationQuery = (domains: Domains): Query | undefined => {
6+
export const useLocationQuery = (): Query | undefined => {
7+
const domains = useDomains();
68
const location = useLocation();
79
try {
810
const link = new URIRef(location.pathname + location.search);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useCallback } from 'react';
2+
import { useNavigate } from 'react-router-dom-v5-compat';
3+
import * as korrel8r from '../korrel8r/types';
4+
import { useDomains } from './useDomains';
5+
6+
/**
7+
* Custom hook for navigating from Korrel8r queries to console web pages.
8+
* @returns A function to navigate to a query with given constraints
9+
*/
10+
export const useNavigateToQuery = () => {
11+
const navigate = useNavigate();
12+
const domains = useDomains();
13+
14+
const navigateToQuery = useCallback(
15+
(query: korrel8r.Query, constraint: korrel8r.Constraint) => {
16+
try {
17+
let link = domains.queryToLink(query, constraint)?.toString();
18+
// eslint-disable-next-line no-console
19+
console.debug(`korrel8r navigateToQuery ${query}, ${constraint} => ${link}`);
20+
if (!link) return;
21+
if (!link.startsWith('/')) link = '/' + link;
22+
navigate(link);
23+
} catch (e) {
24+
// eslint-disable-next-line no-console
25+
console.error(`korrel8r navigateToQuery ${query}, ${constraint}: error: ${e}`);
26+
}
27+
},
28+
[navigate, domains],
29+
);
30+
31+
return navigateToQuery;
32+
};

0 commit comments

Comments
 (0)