1- import { useEffect , useMemo , useRef , useState } from 'react'
1+ import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
22import { useHistory } from 'react-router-dom'
33
44import {
@@ -30,10 +30,13 @@ import {
3030 getNavigationGroups ,
3131 getNewSelectedIndex ,
3232 parseAppListToNavItems ,
33+ parseChartListToNavItems ,
34+ parseClusterListToNavItems ,
35+ parseHelmAppListToNavItems ,
3336 sanitizeItemId ,
3437} from './utils'
3538
36- const CommandBarBackdrop = ( { handleClose, isLoadingAppList , appList } : CommandBarBackdropProps ) => {
39+ const CommandBarBackdrop = ( { handleClose, isLoadingResourceList , resourceList } : CommandBarBackdropProps ) => {
3740 const history = useHistory ( )
3841 const { registerShortcut, unregisterShortcut } = useRegisterShortcut ( )
3942
@@ -44,20 +47,16 @@ const CommandBarBackdrop = ({ handleClose, isLoadingAppList, appList }: CommandB
4447
4548 const navigationGroups = useMemo ( ( ) => getNavigationGroups ( serverMode , isSuperAdmin ) , [ serverMode , isSuperAdmin ] )
4649
47- const { data : recentActionsGroup , isLoading } = useQuery ( {
48- queryFn : ( { signal } ) =>
49- getUserPreferences ( signal ) . then ( ( response ) => {
50- const responseData : ResponseType < typeof response > = {
51- code : API_STATUS_CODES . OK ,
52- status : 'OK' ,
53- result : response ,
54- }
55- return responseData
56- } ) ,
57- queryKey : [ 'recentNavigationActions' ] ,
58- select : ( { result } ) =>
50+ const parseRecentActionsGroup = useCallback (
51+ ( { result } : ResponseType < Awaited < ReturnType < typeof getUserPreferences > > > ) =>
5952 result . commandBar . recentNavigationActions . reduce < CommandBarGroupType > ( ( acc , action ) => {
60- const allGroups = [ ...navigationGroups , ...parseAppListToNavItems ( appList ) ]
53+ const allGroups = [
54+ ...navigationGroups ,
55+ ...parseAppListToNavItems ( resourceList ?. appList ) ,
56+ ...parseHelmAppListToNavItems ( resourceList ?. helmAppList ) ,
57+ ...parseChartListToNavItems ( resourceList ?. chartList ) ,
58+ ...parseClusterListToNavItems ( resourceList ?. clusterList ) ,
59+ ]
6160
6261 const requiredGroup = allGroups . find ( ( group ) => group . items . some ( ( item ) => item . id === action . id ) )
6362
@@ -70,6 +69,22 @@ const CommandBarBackdrop = ({ handleClose, isLoadingAppList, appList }: CommandB
7069 }
7170 return acc
7271 } , structuredClone ( RECENT_ACTIONS_GROUP ) ) ,
72+ [ resourceList ] ,
73+ )
74+
75+ const { data : recentActionsGroup , isLoading } = useQuery ( {
76+ queryFn : ( { signal } ) =>
77+ getUserPreferences ( signal ) . then ( ( response ) => {
78+ const responseData : ResponseType < typeof response > = {
79+ code : API_STATUS_CODES . OK ,
80+ status : 'OK' ,
81+ result : response ,
82+ }
83+ return responseData
84+ } ) ,
85+ queryKey : [ 'recentNavigationActions' , isLoadingResourceList ] ,
86+ enabled : ! isLoadingResourceList ,
87+ select : parseRecentActionsGroup ,
7388 } )
7489
7590 const areFiltersApplied = ! ! searchText
@@ -95,7 +110,7 @@ const CommandBarBackdrop = ({ handleClose, isLoadingAppList, appList }: CommandB
95110 return navigationGroups
96111 }
97112
98- const additionalGroups = getAdditionalNavGroups ( searchText , appList )
113+ const additionalGroups = getAdditionalNavGroups ( searchText , resourceList )
99114 const parsedGroups = navigationGroups . reduce < typeof navigationGroups > ( ( acc , group ) => {
100115 const filteredItems = group . items . filter (
101116 ( item ) =>
@@ -150,25 +165,17 @@ const CommandBarBackdrop = ({ handleClose, isLoadingAppList, appList }: CommandB
150165 const item = itemFlatList [ newIndex ]
151166 const itemElement = itemRefMap . current [ item . id ]
152167 if ( itemElement ) {
153- itemElement . scrollIntoView ( { block : 'nearest ' , inline : 'nearest' } )
168+ itemElement . scrollIntoView ( { block : 'center ' , inline : 'nearest' } )
154169 }
155170 return newIndex
156171 } )
157172 }
158173
159- const onItemClick = async ( item : CommandBarGroupType [ 'items' ] [ number ] ) => {
160- if ( ! item . href ) {
161- logExceptionToSentry ( new Error ( `CommandBar item with id ${ item . id } does not have a valid href` ) )
162- ToastManager . showToast ( {
163- variant : ToastVariantType . error ,
164- description : `CommandBar item with id ${ item . id } does not have a valid href` ,
165- } )
174+ const pushItemToRecent = async ( item : CommandBarGroupType [ 'items' ] [ number ] ) => {
175+ if ( item . excludeFromRecent ) {
166176 return
167177 }
168178
169- history . push ( item . href )
170- handleClose ( )
171-
172179 const currentItemId = sanitizeItemId ( item )
173180
174181 // In this now we will put the id as first item in the list and keep first 5 items then
@@ -190,6 +197,22 @@ const CommandBarBackdrop = ({ handleClose, isLoadingAppList, appList }: CommandB
190197 } )
191198 }
192199
200+ const onItemClick = async ( item : CommandBarGroupType [ 'items' ] [ number ] ) => {
201+ if ( ! item . href ) {
202+ logExceptionToSentry ( new Error ( `CommandBar item with id ${ item . id } does not have a valid href` ) )
203+ ToastManager . showToast ( {
204+ variant : ToastVariantType . error ,
205+ description : `CommandBar item with id ${ item . id } does not have a valid href` ,
206+ } )
207+ return
208+ }
209+
210+ history . push ( item . href )
211+ handleClose ( )
212+
213+ await pushItemToRecent ( item )
214+ }
215+
193216 const handleEnterSelectedItem = async ( ) => {
194217 const selectedItem = itemFlatList [ selectedItemIndex ]
195218
@@ -330,7 +353,7 @@ const CommandBarBackdrop = ({ handleClose, isLoadingAppList, appList }: CommandB
330353 handleSearchChange = { handleSearchChange }
331354 noBackgroundAndBorder
332355 shouldDebounce
333- isLoading = { isLoadingAppList }
356+ isLoading = { isLoadingResourceList }
334357 />
335358 </ div >
336359
0 commit comments