@@ -21,7 +21,8 @@ import {
2121import { CombinedDashboardMetadata } from '../perses/hooks/useDashboardsData' ;
2222import { useNavigate } from 'react-router-dom-v5-compat' ;
2323import { QueryParams } from '../../query-params' ;
24- import { NumberParam , StringParam , useQueryParam } from 'use-query-params' ;
24+ import { NumberParam , useQueryParam } from 'use-query-params' ;
25+ import { ALL_NAMESPACES_KEY } from '../../utils' ;
2526
2627export const useLegacyDashboards = ( namespace : string , urlBoard : string ) => {
2728 const { t } = useTranslation ( 'plugin__monitoring-plugin' ) ;
@@ -31,26 +32,19 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
3132 const safeFetch = useCallback ( useSafeFetch ( ) , [ ] ) ;
3233 const [ legacyDashboards , setLegacyDashboards ] = useState < Board [ ] > ( [ ] ) ;
3334 const [ legacyDashboardsError , setLegacyDashboardsError ] = useState < string > ( ) ;
34- const [ dashboardParam ] = useQueryParam ( QueryParams . Dashboard , StringParam ) ;
3535 const [ refreshInterval ] = useQueryParam ( QueryParams . RefreshInterval , NumberParam ) ;
3636 const [ legacyDashboardsLoading , , , setLegacyDashboardsLoaded ] = useBoolean ( true ) ;
37- const [ initialLoad , , , setInitialLoaded ] = useBoolean ( true ) ;
37+ const [ initialLoad , , setInitialUnloaded , setInitialLoaded ] = useBoolean ( true ) ;
3838 const dispatch = useDispatch ( ) ;
3939 const navigate = useNavigate ( ) ;
40- const legacyDashboard = useMemo ( ( ) => {
41- if ( perspective === 'dev' ) {
42- return dashboardParam ;
43- }
44- return urlBoard ;
45- } , [ perspective , dashboardParam , urlBoard ] ) ;
4640
4741 useEffect ( ( ) => {
4842 safeFetch < any > ( '/api/console/monitoring-dashboard-config' )
4943 . then ( ( response ) => {
5044 setLegacyDashboardsLoaded ( ) ;
5145 setLegacyDashboardsError ( undefined ) ;
5246 let items = response . items ;
53- if ( namespace ) {
47+ if ( namespace !== ALL_NAMESPACES_KEY ) {
5448 items = _ . filter (
5549 items ,
5650 ( item ) => item . metadata ?. labels [ 'console.openshift.io/odc-dashboard' ] === 'true' ,
@@ -83,7 +77,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
8377 } , [ namespace , safeFetch , setLegacyDashboardsLoaded , t ] ) ;
8478
8579 const legacyRows = useMemo ( ( ) => {
86- const data = _ . find ( legacyDashboards , { name : legacyDashboard } ) ?. data ;
80+ const data = _ . find ( legacyDashboards , { name : urlBoard } ) ?. data ;
8781
8882 return data ?. rows ?. length
8983 ? data . rows
@@ -101,17 +95,17 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
10195 }
10296 return acc ;
10397 } , [ ] ) ?? [ ] ;
104- } , [ legacyDashboard , legacyDashboards ] ) ;
98+ } , [ urlBoard , legacyDashboards ] ) ;
10599
106100 useEffect ( ( ) => {
107101 // Dashboard query argument is only set in dev perspective, so skip for admin
108102 if ( perspective !== 'dev' ) {
109103 return ;
110104 }
111- const allVariables = getAllVariables ( legacyDashboards , legacyDashboard , namespace ) ;
105+ const allVariables = getAllVariables ( legacyDashboards , urlBoard , namespace ) ;
112106 dispatch ( dashboardsPatchAllVariables ( allVariables ) ) ;
113107 // eslint-disable-next-line react-hooks/exhaustive-deps
114- } , [ namespace , legacyDashboard ] ) ;
108+ } , [ namespace , urlBoard ] ) ;
115109
116110 // Homogenize data needed for dashboards dropdown between legacy and perses dashboards
117111 // to enable both to use the same component
@@ -143,7 +137,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
143137 let url = getLegacyDashboardsUrl ( perspective , newBoard , namespace ) ;
144138 url = `${ url } ${ perspective === 'dev' ? '&' : '?' } ${ params . toString ( ) } ` ;
145139
146- if ( newBoard !== legacyDashboard || initialLoad ) {
140+ if ( newBoard !== urlBoard || initialLoad ) {
147141 if ( params . get ( QueryParams . Dashboard ) !== newBoard ) {
148142 navigate ( url , { replace : true } ) ;
149143 }
@@ -165,7 +159,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
165159 } ,
166160 [
167161 perspective ,
168- legacyDashboard ,
162+ urlBoard ,
169163 dispatch ,
170164 navigate ,
171165 namespace ,
@@ -177,15 +171,24 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
177171
178172 useEffect ( ( ) => {
179173 if (
180- ( ! legacyDashboard ||
181- ! legacyDashboards . some ( ( legacyBoard ) => legacyBoard . name === legacyDashboard ) ||
174+ ( ! urlBoard ||
175+ ! legacyDashboards . some ( ( legacyBoard ) => legacyBoard . name === urlBoard ) ||
182176 initialLoad ) &&
183177 ! _ . isEmpty ( legacyDashboards )
184178 ) {
185- changeLegacyDashboard ( legacyDashboard || legacyDashboards ?. [ 0 ] ?. name ) ;
179+ changeLegacyDashboard ( urlBoard || legacyDashboards ?. [ 0 ] ?. name ) ;
186180 setInitialLoaded ( ) ;
187181 }
188- } , [ legacyDashboards , changeLegacyDashboard , initialLoad , setInitialLoaded , legacyDashboard ] ) ;
182+ } , [ legacyDashboards , changeLegacyDashboard , initialLoad , setInitialLoaded , urlBoard ] ) ;
183+
184+ useEffect ( ( ) => {
185+ // Basically perform a full reload when changing a namespace to force the variables and the
186+ // dashboard to reset. This is needed for when we transition between ALL_NS and a normal
187+ // namespace, but is performed quickly and should help insure consistency when transitioning
188+ // between any namespaces
189+ setInitialUnloaded ( ) ;
190+ /* eslint-disable react-hooks/exhaustive-deps */
191+ } , [ namespace ] ) ;
189192
190193 // Clear variables on unmount
191194 useEffect ( ( ) => {
@@ -201,7 +204,7 @@ export const useLegacyDashboards = (namespace: string, urlBoard: string) => {
201204 legacyRows,
202205 legacyDashboardsMetadata,
203206 changeLegacyDashboard,
204- legacyDashboard,
207+ legacyDashboard : urlBoard ,
205208 } ;
206209} ;
207210
@@ -227,11 +230,14 @@ const getAllVariables = (boards: Board[], newBoardName: string, namespace: strin
227230 allVariables [ v . name ] = {
228231 datasource : v . datasource ,
229232 includeAll : ! ! v . includeAll ,
230- isHidden : namespace && v . name === 'namespace' ? true : v . hide !== 0 ,
231- isLoading : namespace ? v . type === 'query' && ! namespace : v . type === 'query' ,
233+ isHidden : v . name === 'namespace' && namespace !== ALL_NAMESPACES_KEY ? true : v . hide !== 0 ,
234+ isLoading : v . name === 'namespace' ? false : v . type === 'query' ,
232235 options : _ . map ( v . options , 'value' ) ,
233236 query : v . type === 'query' ? v . query : undefined ,
234- value : namespace && v . name === 'namespace' ? namespace : value || v . options ?. [ 0 ] ?. value ,
237+ value :
238+ v . name === 'namespace' && namespace !== ALL_NAMESPACES_KEY
239+ ? namespace
240+ : value || v . options ?. [ 0 ] ?. value ,
235241 } ;
236242 }
237243 } ) ;
0 commit comments