Skip to content

Commit 2d128c8

Browse files
fix: re-add openshift project query param to avoid conflict with dashboard namespace
1 parent f027d44 commit 2d128c8

2 files changed

Lines changed: 94 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ import ErrorAlert from './error';
1010
import { DashboardSkeletonLegacy } from './dashboard-skeleton-legacy';
1111
import { useLegacyDashboards } from './useLegacyDashboards';
1212
import { MonitoringProvider } from '../../../contexts/MonitoringContext';
13-
import { useMonitoringNamespace } from '../../hooks/useMonitoringNamespace';
1413
import { useMonitoring } from '../../../hooks/useMonitoring';
1514
import { StringParam, useQueryParam } from 'use-query-params';
1615
import { QueryParams } from '../../../components/query-params';
16+
import { useOpenshiftProject } from './useOpenshiftProject';
1717

1818
type LegacyDashboardsPageProps = {
1919
urlBoard: string;
2020
};
2121

2222
const LegacyDashboardsPage_: FC<LegacyDashboardsPageProps> = ({ urlBoard }) => {
23-
const { namespace, setNamespace } = useMonitoringNamespace();
23+
const { project, setProject } = useOpenshiftProject();
2424
const {
2525
legacyDashboardsError,
2626
legacyRows,
2727
legacyDashboardsLoading,
2828
legacyDashboardsMetadata,
2929
changeLegacyDashboard,
3030
legacyDashboard,
31-
} = useLegacyDashboards(namespace, urlBoard);
31+
} = useLegacyDashboards(project, urlBoard);
3232
const { perspective } = usePerspective();
3333
const { displayNamespaceSelector } = useMonitoring();
3434
const { t } = useTranslation(process.env.I18N_NAMESPACE);
3535

3636
return (
3737
<>
38-
{displayNamespaceSelector && <NamespaceBar onNamespaceChange={(ns) => setNamespace(ns)} />}
38+
{displayNamespaceSelector && <NamespaceBar onNamespaceChange={(ns) => setProject(ns)} />}
3939
<DashboardSkeletonLegacy
4040
boardItems={legacyDashboardsMetadata}
4141
changeBoard={changeLegacyDashboard}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk';
2+
import { useCallback, useEffect } from 'react';
3+
import { QueryParams } from '../../query-params';
4+
import { StringParam, useQueryParam } from 'use-query-params';
5+
import { useDispatch, useSelector } from 'react-redux';
6+
import { dashboardsPatchVariable } from '../../../store/actions';
7+
import { MonitoringState } from '../../../store/store';
8+
import { getObserveState, usePerspective } from '../../hooks/usePerspective';
9+
import { useMonitoring } from '../../../hooks/useMonitoring';
10+
import { ALL_NAMESPACES_KEY } from '../../utils';
11+
import { useParams } from 'react-router';
12+
13+
export const useOpenshiftProject = () => {
14+
const { perspective } = usePerspective();
15+
const [activeNamespace, setActiveNamespace] = useActiveNamespace();
16+
const { ns: routeNamespace } = useParams<{ ns?: string }>();
17+
const [openshiftProject, setOpenshiftProject] = useQueryParam(
18+
QueryParams.OpenshiftProject,
19+
StringParam,
20+
);
21+
const { plugin } = useMonitoring();
22+
const variableNamespace = useSelector(
23+
(state: MonitoringState) =>
24+
getObserveState(plugin, state).dashboards.variables['namespace']?.value ?? '',
25+
);
26+
const dispatch = useDispatch();
27+
28+
useEffect(() => {
29+
if (perspective !== 'dev') {
30+
// If the URL parameter is set, but the activeNamespace doesn't match it, then
31+
// set the activeNamespace to match the URL parameter
32+
if (openshiftProject && openshiftProject !== activeNamespace) {
33+
setActiveNamespace(openshiftProject);
34+
if (variableNamespace !== openshiftProject && openshiftProject !== ALL_NAMESPACES_KEY) {
35+
dispatch(
36+
dashboardsPatchVariable('namespace', {
37+
// Dashboards space variable shouldn't use the ALL_NAMESPACES_KEY
38+
value: openshiftProject,
39+
}),
40+
);
41+
}
42+
return;
43+
}
44+
if (!openshiftProject) {
45+
setOpenshiftProject(activeNamespace);
46+
if (variableNamespace !== activeNamespace && openshiftProject !== ALL_NAMESPACES_KEY) {
47+
// Dashboards space variable shouldn't use the ALL_NAMESPACES_KEY
48+
dispatch(
49+
dashboardsPatchVariable('namespace', {
50+
value: activeNamespace,
51+
}),
52+
);
53+
}
54+
return;
55+
}
56+
} else {
57+
// perform useMonitoringNamespace when in dev perspective
58+
if (routeNamespace && activeNamespace !== routeNamespace) {
59+
setActiveNamespace(routeNamespace);
60+
}
61+
}
62+
}, [
63+
activeNamespace,
64+
setActiveNamespace,
65+
openshiftProject,
66+
setOpenshiftProject,
67+
dispatch,
68+
variableNamespace,
69+
perspective,
70+
routeNamespace,
71+
]);
72+
73+
const setProject = useCallback(
74+
(namespace: string) => {
75+
if (perspective === 'dev') {
76+
setActiveNamespace(namespace);
77+
} else {
78+
setActiveNamespace(namespace);
79+
setOpenshiftProject(namespace);
80+
dispatch(dashboardsPatchVariable('namespace', { value: namespace }));
81+
}
82+
},
83+
[setActiveNamespace, setOpenshiftProject, dispatch, perspective],
84+
);
85+
86+
return {
87+
project: openshiftProject,
88+
setProject,
89+
};
90+
};

0 commit comments

Comments
 (0)