@@ -7,16 +7,19 @@ import {
7
7
GenericFilterEmptyState ,
8
8
getUserPreferences ,
9
9
KeyboardShortcut ,
10
+ logExceptionToSentry ,
10
11
ResponseType ,
11
12
SearchBar ,
12
13
stopPropagation ,
14
+ updateUserPreferences ,
13
15
useQuery ,
14
16
useRegisterShortcut ,
17
+ UserPreferencesType ,
15
18
} from '@devtron-labs/devtron-fe-common-lib'
16
19
17
20
import CommandGroup from './CommandGroup'
18
21
import { NAVIGATION_GROUPS , RECENT_ACTIONS_GROUP , RECENT_NAVIGATION_ITEM_ID_PREFIX , SHORT_CUTS } from './constants'
19
- import { CommandBarBackdropProps , CommandBarGroupType } from './types'
22
+ import { CommandBarActionIdType , CommandBarBackdropProps , CommandBarGroupType , CommandBarItemType } from './types'
20
23
21
24
const CommandBarBackdrop = ( { handleClose } : CommandBarBackdropProps ) => {
22
25
const history = useHistory ( )
@@ -38,7 +41,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
38
41
queryKey : [ 'recentNavigationActions' ] ,
39
42
select : ( { result } ) =>
40
43
result . commandBar . recentNavigationActions . reduce < CommandBarGroupType > ( ( acc , action ) => {
41
- const requiredGroup = NAVIGATION_GROUPS . find ( ( group ) =>
44
+ const requiredGroup = structuredClone ( NAVIGATION_GROUPS ) . find ( ( group ) =>
42
45
group . items . some ( ( item ) => item . id === action . id ) ,
43
46
)
44
47
@@ -169,12 +172,40 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
169
172
}
170
173
} , [ itemFlatList ] )
171
174
172
- const onItemClick = ( item : CommandBarGroupType [ 'items' ] [ number ] ) => {
173
- if ( item . href ) {
174
- history . push ( item . href )
175
+ const sanitizeItemId = ( item : CommandBarItemType ) =>
176
+ ( item . id . startsWith ( RECENT_NAVIGATION_ITEM_ID_PREFIX )
177
+ ? item . id . replace ( RECENT_NAVIGATION_ITEM_ID_PREFIX , '' )
178
+ : item . id ) as CommandBarActionIdType
179
+
180
+ const onItemClick = async ( item : CommandBarGroupType [ 'items' ] [ number ] ) => {
181
+ if ( ! item . href ) {
182
+ logExceptionToSentry ( new Error ( `CommandBar item with id ${ item . id } does not have a valid href` ) )
183
+ return
175
184
}
176
185
186
+ history . push ( item . href )
177
187
handleClose ( )
188
+
189
+ const currentItemId = sanitizeItemId ( item )
190
+
191
+ // const updatedRecentActions = recentActionsGroup?.items.filter((action) => action.id !== currentItemId)
192
+ // In this now we will put the id as first item in the list and keep first 5 items then
193
+ const updatedRecentActions : UserPreferencesType [ 'commandBar' ] [ 'recentNavigationActions' ] = [
194
+ {
195
+ id : currentItemId as CommandBarActionIdType ,
196
+ } ,
197
+ ...( recentActionsGroup ?. items
198
+ . filter ( ( action ) => sanitizeItemId ( action ) !== currentItemId )
199
+ . slice ( 0 , 4 )
200
+ . map ( ( action ) => ( {
201
+ id : sanitizeItemId ( action ) ,
202
+ } ) ) || [ ] ) ,
203
+ ]
204
+
205
+ await updateUserPreferences ( {
206
+ path : 'commandBar.recentNavigationActions' ,
207
+ value : updatedRecentActions ,
208
+ } )
178
209
}
179
210
180
211
const renderNavigationGroups = ( baseIndex : number ) => {
0 commit comments