Skip to content

Commit 5e3cb04

Browse files
fix: all namespace/project issues
1 parent c17b6d1 commit 5e3cb04

8 files changed

Lines changed: 85 additions & 61 deletions

web/src/components/dashboards/legacy/custom-time-range-modal.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { useDispatch } from 'react-redux';
2121
import { dashboardsSetEndTime, dashboardsSetTimespan } from '../../../store/actions';
2222

2323
import { QueryParams } from '../../query-params';
24-
import { useSearchParams } from 'react-router';
24+
import { StringParam, useQueryParam } from 'use-query-params';
2525

2626
const zeroPad = (number: number) => (number < 10 ? `0${number}` : number);
2727

@@ -49,7 +49,8 @@ const CustomTimeRangeModal: FC<CustomTimeRangeModalProps> = ({
4949
endTime,
5050
}) => {
5151
const { t } = useTranslation(process.env.I18N_NAMESPACE);
52-
const [, setQueryParams] = useSearchParams();
52+
const [, setEndTimeParam] = useQueryParam(QueryParams.EndTime, StringParam);
53+
const [, setTimeRangeParam] = useQueryParam(QueryParams.TimeRange, StringParam);
5354

5455
const dispatch = useDispatch();
5556

@@ -68,10 +69,8 @@ const CustomTimeRangeModal: FC<CustomTimeRangeModalProps> = ({
6869
if (_.isInteger(from) && _.isInteger(to)) {
6970
dispatch(dashboardsSetEndTime(to));
7071
dispatch(dashboardsSetTimespan(to - from));
71-
setQueryParams([
72-
[QueryParams.EndTime, to.toString()],
73-
[QueryParams.TimeRange, (to - from).toString()],
74-
]);
72+
setEndTimeParam(to.toString());
73+
setTimeRangeParam((to - from).toString());
7574
setClosed();
7675
}
7776
};

web/src/components/dashboards/legacy/dashboard-skeleton-legacy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const HeaderTop: FC = memo(() => {
2929

3030
type MonitoringDashboardsLegacyPageProps = PropsWithChildren<{
3131
boardItems: CombinedDashboardMetadata[];
32-
changeBoard: (dashboardName: string) => void;
32+
changeBoard: (params: { newBoard?: string; initialLoad?: boolean; newProject?: string }) => void;
3333
dashboardName: string;
3434
}>;
3535

@@ -39,7 +39,7 @@ export const DashboardSkeletonLegacy: FC<MonitoringDashboardsLegacyPageProps> =
3939

4040
const onChangeBoard = useCallback(
4141
(selectedDashboard: string) => {
42-
changeBoard(selectedDashboard);
42+
changeBoard({ newBoard: selectedDashboard });
4343
},
4444
[changeBoard],
4545
);

web/src/components/dashboards/legacy/legacy-dashboard-page.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,33 @@ import { MonitoringProvider } from '../../../contexts/MonitoringContext';
1313
import { useMonitoring } from '../../../hooks/useMonitoring';
1414
import { StringParam, useQueryParam } from 'use-query-params';
1515
import { QueryParams } from '../../../components/query-params';
16-
import { useOpenshiftProject } from './useOpenshiftProject';
1716

1817
type LegacyDashboardsPageProps = {
1918
urlBoard: string;
2019
};
2120

2221
const LegacyDashboardsPage_: FC<LegacyDashboardsPageProps> = ({ urlBoard }) => {
23-
const { project, setProject } = useOpenshiftProject();
2422
const {
2523
legacyDashboardsError,
2624
legacyRows,
2725
legacyDashboardsLoading,
2826
legacyDashboardsMetadata,
2927
changeLegacyDashboard,
3028
legacyDashboard,
31-
} = useLegacyDashboards(project, urlBoard);
29+
} = useLegacyDashboards(urlBoard);
3230
const { perspective } = usePerspective();
3331
const { displayNamespaceSelector } = useMonitoring();
3432
const { t } = useTranslation(process.env.I18N_NAMESPACE);
3533

3634
return (
3735
<>
38-
{displayNamespaceSelector && <NamespaceBar onNamespaceChange={(ns) => setProject(ns)} />}
36+
{displayNamespaceSelector && (
37+
<NamespaceBar
38+
onNamespaceChange={(ns) => {
39+
changeLegacyDashboard({ newProject: ns });
40+
}}
41+
/>
42+
)}
3943
<DashboardSkeletonLegacy
4044
boardItems={legacyDashboardsMetadata}
4145
changeBoard={changeLegacyDashboard}

web/src/components/dashboards/legacy/legacy-dashboard.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type { FC } from 'react';
2121
import { memo, useRef, useState, useCallback, useEffect, useMemo } from 'react';
2222
import { useTranslation } from 'react-i18next';
2323
import { useSelector } from 'react-redux';
24-
import { Link, useSearchParams } from 'react-router';
24+
import { Link } from 'react-router';
2525

2626
import { Perspective } from '../../../store/actions';
2727
import BarChart from '../legacy/bar-chart';
@@ -50,6 +50,7 @@ import { t_global_font_size_heading_h2 } from '@patternfly/react-tokens';
5050
import { GraphUnits } from '../../../components/metrics/units';
5151
import { LegacyDashboardPageTestIDs } from '../../../components/data-test';
5252
import { useMonitoring } from '../../../hooks/useMonitoring';
53+
import { StringParam, useQueryParam } from 'use-query-params';
5354

5455
const QueryBrowserLink = ({
5556
queries,
@@ -124,8 +125,9 @@ const Card: FC<CardProps> = memo(({ panel, perspective }) => {
124125
const [isChartLoading, setIsChartLoading] = useState<boolean>(panel.type === 'graph');
125126
const customDataSourceName = panel.datasource?.name;
126127
const [extensions, extensionsResolved] = useResolvedExtensions<DataSource>(isDataSource);
128+
const [, setEndTimeParam] = useQueryParam(QueryParams.EndTime, StringParam);
129+
const [, setTimeRangeParam] = useQueryParam(QueryParams.TimeRange, StringParam);
127130
const hasExtensions = !_.isEmpty(extensions);
128-
const [, setQueryParams] = useSearchParams();
129131

130132
const formatSeriesTitle = useCallback(
131133
(labels, i) => {
@@ -249,10 +251,8 @@ const Card: FC<CardProps> = memo(({ panel, perspective }) => {
249251
}, [extensions, extensionsResolved, customDataSourceName, hasExtensions]);
250252

251253
const handleZoom = (timeRange: number, endTime: number) => {
252-
setQueryParams([
253-
[QueryParams.EndTime, endTime.toString()],
254-
[QueryParams.TimeRange, timeRange.toString()],
255-
]);
254+
setEndTimeParam(endTime.toString());
255+
setTimeRangeParam(timeRange.toString());
256256
};
257257

258258
const panelBreakpoints = useMemo(() => {

web/src/components/dashboards/legacy/legacy-variable-dropdowns.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { useSafeFetch } from '../../console/utils/safe-fetch-hook';
2828

2929
import { dashboardsPatchVariable, dashboardsVariableOptionsLoaded } from '../../../store/actions';
3030
import { getTimeRanges, isTimeoutError, QUERY_CHUNK_SIZE } from '../../utils';
31-
import { getObserveState } from '../../hooks/usePerspective';
31+
import { getObserveState, usePerspective } from '../../hooks/usePerspective';
3232
import { MonitoringState } from '../../../store/store';
3333
import { DEFAULT_GRAPH_SAMPLES, MONITORING_DASHBOARDS_VARIABLE_ALL_OPTION_KEY } from './utils';
3434
import {
@@ -38,7 +38,6 @@ import {
3838
import { useMonitoring } from '../../../hooks/useMonitoring';
3939
import { useDeepMemo } from '../../hooks/useDeepMemo';
4040
import { StringParam, useQueryParam } from 'use-query-params';
41-
import { QueryParams } from '../../query-params';
4241

4342
const intervalVariableRegExps = ['__interval', '__rate_interval', '__auto_interval_[a-z]+'];
4443

@@ -109,6 +108,7 @@ const LegacyDashboardsVariableOption = ({ value, isSelected, ...rest }) =>
109108
const LegacyDashboardsVariableDropdown: FC<VariableDropdownProps> = ({ id, name }) => {
110109
const { t } = useTranslation(process.env.I18N_NAMESPACE);
111110
const { plugin, accessCheckLoading, useMetricsTenancy } = useMonitoring();
111+
const { perspective } = usePerspective();
112112
const [namespace] = useActiveNamespace();
113113
const [queryParam, setQueryParam] = useQueryParam(name, StringParam);
114114

@@ -138,6 +138,9 @@ const LegacyDashboardsVariableDropdown: FC<VariableDropdownProps> = ({ id, name
138138
const [extensions, extensionsResolved] = useResolvedExtensions<DataSource>(isDataSource);
139139
const hasExtensions = !_.isEmpty(extensions);
140140

141+
// Don't set namespace param while in dev perspective
142+
const shouldSetQueryParam = !(perspective === 'dev' && name === 'namespace');
143+
141144
const getURL = useCallback(
142145
async (prometheusProps) => {
143146
try {
@@ -254,25 +257,25 @@ const LegacyDashboardsVariableDropdown: FC<VariableDropdownProps> = ({ id, name
254257
]);
255258

256259
useEffect(() => {
257-
if (variable?.value !== queryParam && name !== QueryParams.Namespace) {
258-
// Default to using the query param to allow for sharable links, expect for namespace
260+
if (variable?.value !== queryParam) {
261+
// Default to using the query param to allow for sharable links
259262
if (queryParam) {
260263
dispatch(dashboardsPatchVariable(name, { value: queryParam }));
261264
// set the url if it isn't set
262-
} else if (variable?.value) {
265+
} else if (variable?.value && shouldSetQueryParam) {
263266
setQueryParam(variable?.value);
264267
}
265268
}
266-
}, [name, variable?.value, queryParam, setQueryParam, dispatch]);
269+
}, [name, variable?.value, queryParam, setQueryParam, dispatch, shouldSetQueryParam]);
267270

268271
const onChange = useCallback(
269272
(v: string) => {
270-
if (v !== variable?.value) {
273+
if (v !== variable?.value && shouldSetQueryParam) {
271274
setQueryParam(v);
272275
dispatch(dashboardsPatchVariable(name, { value: v }));
273276
}
274277
},
275-
[dispatch, name, variable?.value, setQueryParam],
278+
[dispatch, name, variable?.value, setQueryParam, shouldSetQueryParam],
276279
);
277280

278281
if (variable?.isHidden || (!isError && _.isEmpty(variable?.options))) {

web/src/components/dashboards/legacy/useLegacyDashboards.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import {
2222
MONITORING_DASHBOARDS_DEFAULT_TIMESPAN,
2323
MONITORING_DASHBOARDS_VARIABLE_ALL_OPTION_KEY,
2424
} from './utils';
25+
import { useOpenshiftProject } from './useOpenshiftProject';
2526

26-
export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
27+
export const useLegacyDashboards = (urlBoard: string) => {
2728
const { t } = useTranslation('plugin__monitoring-plugin');
2829
const { perspective } = usePerspective();
30+
const { project } = useOpenshiftProject();
2931

3032
// eslint-disable-next-line react-hooks/exhaustive-deps
3133
const safeFetch = useCallback(useSafeFetch(), []);
@@ -56,7 +58,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
5658
// Move namespace filtering out of the fetch response call to avoid race conditions
5759
const legacyDashboards = useMemo<Board[]>(() => {
5860
let items = unfilteredLegacyDashboards;
59-
if (namespace && namespace !== ALL_NAMESPACES_KEY) {
61+
if (project && project !== ALL_NAMESPACES_KEY) {
6062
items = _.filter(
6163
items,
6264
(item) => item.metadata?.labels['console.openshift.io/odc-dashboard'] === 'true',
@@ -78,7 +80,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
7880
}
7981
};
8082
return _.sortBy(_.map(items, getBoardData), (v) => _.toLower(v?.data?.title));
81-
}, [namespace, unfilteredLegacyDashboards, setLegacyDashboardsError, t]);
83+
}, [project, unfilteredLegacyDashboards, setLegacyDashboardsError, t]);
8284

8385
const legacyRows = useMemo(() => {
8486
const data = _.find(legacyDashboards, { name: urlBoard })?.data;
@@ -117,25 +119,49 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
117119
}, [legacyDashboards, legacyDashboardsLoading]);
118120

119121
const changeLegacyDashboard = useCallback(
120-
(newBoard: string, forceRefresh = false) => {
121-
if (!newBoard) {
122-
// If the board is being cleared then don't do anything
123-
return;
124-
}
122+
({
123+
newBoard,
124+
newProject,
125+
initialLoad = false,
126+
}: {
127+
newBoard?: string;
128+
newProject?: string;
129+
initialLoad?: boolean;
130+
}) => {
131+
const dashboardProject = newProject ? newProject : project;
125132

126-
const params = new URLSearchParams();
133+
const url = getLegacyDashboardsUrl(perspective, newBoard, dashboardProject);
127134

128-
const url = getLegacyDashboardsUrl(perspective, newBoard, namespace);
135+
let params: URLSearchParams;
136+
if (initialLoad) {
137+
params = new URLSearchParams(queryParams);
138+
if (perspective === 'dev') {
139+
params.delete(QueryParams.Namespace);
140+
params.delete(QueryParams.OpenshiftProject);
141+
}
142+
} else {
143+
params = new URLSearchParams();
144+
}
129145

130-
if (newBoard !== urlBoard || forceRefresh) {
131-
if (!params.has(QueryParams.Dashboard) || params.get(QueryParams.Dashboard) !== newBoard) {
146+
if (newBoard !== urlBoard || newProject !== project || initialLoad) {
147+
if (
148+
perspective === 'dev' &&
149+
(!params.has(QueryParams.Dashboard) || params.get(QueryParams.Dashboard) !== newBoard)
150+
) {
132151
params.set(QueryParams.Dashboard, newBoard);
133-
navigate(`${url}?${params.toString()}`, { replace: true });
134152
}
153+
if (perspective !== 'dev') {
154+
if (params.get(QueryParams.OpenshiftProject) !== ALL_NAMESPACES_KEY) {
155+
params.delete(QueryParams.Namespace);
156+
}
157+
params.set(QueryParams.OpenshiftProject, dashboardProject);
158+
}
159+
const srt = `${url}?${params.toString()}`;
160+
navigate(srt, { replace: true });
135161

136162
dispatch(
137163
dashboardsPatchAllVariables(
138-
getAllVariables(queryParams, legacyDashboards, namespace, newBoard),
164+
getAllVariables(params, legacyDashboards, dashboardProject, newBoard),
139165
),
140166
);
141167

@@ -157,7 +183,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
157183
urlBoard,
158184
dispatch,
159185
navigate,
160-
namespace,
186+
project,
161187
refreshInterval,
162188
queryParams,
163189
legacyDashboards,
@@ -171,10 +197,14 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
171197
initialLoad) &&
172198
!_.isEmpty(legacyDashboards)
173199
) {
174-
changeLegacyDashboard(urlBoard || legacyDashboards?.[0]?.name, initialLoad);
200+
changeLegacyDashboard({
201+
newBoard: urlBoard || legacyDashboards?.[0]?.name,
202+
initialLoad: initialLoad,
203+
newProject: project,
204+
});
175205
setInitialLoaded();
176206
}
177-
}, [legacyDashboards, changeLegacyDashboard, initialLoad, setInitialLoaded, urlBoard]);
207+
}, [legacyDashboards, changeLegacyDashboard, initialLoad, setInitialLoaded, urlBoard, project]);
178208

179209
useEffect(() => {
180210
if (initialLoad || _.isEmpty(legacyDashboards)) {
@@ -185,11 +215,11 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
185215
if (currentBoard) {
186216
dispatch(
187217
dashboardsPatchAllVariables(
188-
getAllVariables(queryParams, legacyDashboards, namespace, currentBoard),
218+
getAllVariables(queryParams, legacyDashboards, project, currentBoard),
189219
),
190220
);
191221
}
192-
}, [namespace, legacyDashboards, urlBoard, dispatch, initialLoad, queryParams]);
222+
}, [project, legacyDashboards, urlBoard, dispatch, initialLoad, queryParams]);
193223

194224
// Clear variables on unmount
195225
useEffect(() => {

web/src/components/dashboards/legacy/useOpenshiftProject.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk';
2-
import { useCallback, useEffect } from 'react';
2+
import { useEffect } from 'react';
33
import { QueryParams } from '../../query-params';
44
import { StringParam, useQueryParam } from 'use-query-params';
55
import { useDispatch, useSelector } from 'react-redux';
@@ -53,19 +53,7 @@ export const useOpenshiftProject = () => {
5353
routeNamespace,
5454
]);
5555

56-
const setProject = useCallback(
57-
(namespace: string) => {
58-
if (perspective === 'dev') {
59-
setActiveNamespace(namespace);
60-
} else {
61-
setOpenshiftProject(namespace);
62-
}
63-
},
64-
[setActiveNamespace, setOpenshiftProject, perspective],
65-
);
66-
6756
return {
6857
project: perspective === 'dev' ? routeNamespace || activeNamespace : openshiftProject,
69-
setProject,
7058
};
7159
};

web/src/components/hooks/usePerspective.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export const getMutlipleQueryBrowserUrl = (
277277

278278
export const getLegacyDashboardsUrl = (
279279
perspective: Perspective,
280-
boardName: string,
280+
boardName?: string,
281281
namespace?: string,
282282
) => {
283283
switch (perspective) {
@@ -286,10 +286,10 @@ export const getLegacyDashboardsUrl = (
286286
case 'dev':
287287
return `/dev-monitoring/ns/${namespace}`;
288288
case 'virtualization-perspective':
289-
return `/virt-monitoring/dashboards/${boardName}`;
289+
return `/virt-monitoring/dashboards` + (boardName ? `/${boardName}` : '');
290290
case 'admin':
291291
default:
292-
return `/monitoring/dashboards/${boardName}`;
292+
return `/monitoring/dashboards` + (boardName ? `/${boardName}` : '');
293293
}
294294
};
295295

0 commit comments

Comments
 (0)