|
| 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