Skip to content

Commit 7635ba3

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

2 files changed

Lines changed: 102 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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 (
35+
variableNamespace &&
36+
variableNamespace !== openshiftProject &&
37+
openshiftProject !== ALL_NAMESPACES_KEY
38+
) {
39+
dispatch(
40+
dashboardsPatchVariable('namespace', {
41+
// Dashboards space variable shouldn't use the ALL_NAMESPACES_KEY
42+
value: openshiftProject,
43+
}),
44+
);
45+
}
46+
return;
47+
}
48+
if (!openshiftProject) {
49+
setOpenshiftProject(activeNamespace);
50+
if (
51+
variableNamespace &&
52+
variableNamespace !== activeNamespace &&
53+
openshiftProject !== ALL_NAMESPACES_KEY
54+
) {
55+
// Dashboards space variable shouldn't use the ALL_NAMESPACES_KEY
56+
dispatch(
57+
dashboardsPatchVariable('namespace', {
58+
value: activeNamespace,
59+
}),
60+
);
61+
}
62+
return;
63+
}
64+
} else {
65+
// perform useMonitoringNamespace when in dev perspective
66+
if (routeNamespace && activeNamespace !== routeNamespace) {
67+
setActiveNamespace(routeNamespace);
68+
}
69+
}
70+
}, [
71+
activeNamespace,
72+
setActiveNamespace,
73+
openshiftProject,
74+
setOpenshiftProject,
75+
dispatch,
76+
variableNamespace,
77+
perspective,
78+
routeNamespace,
79+
]);
80+
81+
const setProject = useCallback(
82+
(namespace: string) => {
83+
if (perspective === 'dev') {
84+
setActiveNamespace(namespace);
85+
} else {
86+
setActiveNamespace(namespace);
87+
setOpenshiftProject(namespace);
88+
dispatch(dashboardsPatchVariable('namespace', { value: namespace }));
89+
}
90+
},
91+
[setActiveNamespace, setOpenshiftProject, dispatch, perspective],
92+
);
93+
94+
return {
95+
project: openshiftProject,
96+
setProject,
97+
};
98+
};

0 commit comments

Comments
 (0)