Skip to content

Commit c4b6870

Browse files
committed
feat: enhance CommandBarBackdrop with recent actions update and error logging
1 parent 91a4772 commit c4b6870

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/Pages/Shared/CommandBar/CommandBarBackdrop.tsx

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ import {
77
GenericFilterEmptyState,
88
getUserPreferences,
99
KeyboardShortcut,
10+
logExceptionToSentry,
1011
ResponseType,
1112
SearchBar,
1213
stopPropagation,
14+
updateUserPreferences,
1315
useQuery,
1416
useRegisterShortcut,
17+
UserPreferencesType,
1518
} from '@devtron-labs/devtron-fe-common-lib'
1619

1720
import CommandGroup from './CommandGroup'
1821
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'
2023

2124
const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
2225
const history = useHistory()
@@ -38,7 +41,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
3841
queryKey: ['recentNavigationActions'],
3942
select: ({ result }) =>
4043
result.commandBar.recentNavigationActions.reduce<CommandBarGroupType>((acc, action) => {
41-
const requiredGroup = NAVIGATION_GROUPS.find((group) =>
44+
const requiredGroup = structuredClone(NAVIGATION_GROUPS).find((group) =>
4245
group.items.some((item) => item.id === action.id),
4346
)
4447

@@ -169,12 +172,40 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
169172
}
170173
}, [itemFlatList])
171174

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
175184
}
176185

186+
history.push(item.href)
177187
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+
})
178209
}
179210

180211
const renderNavigationGroups = (baseIndex: number) => {

src/Pages/Shared/CommandBar/constants.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,14 @@ export const NAVIGATION_LIST: NavigationGroupType[] = [
407407
},
408408
]
409409

410-
export const NAVIGATION_GROUPS: CommandBarGroupType[] = NAVIGATION_LIST.map((group) => ({
410+
export const NAVIGATION_GROUPS: CommandBarGroupType[] = NAVIGATION_LIST.map<CommandBarGroupType>((group) => ({
411411
title: group.title,
412412
id: group.id,
413413
items: group.items.flatMap(({ hasSubMenu, subItems, title, href, id, icon }) => {
414-
if (hasSubMenu && subItems) {
415-
return subItems.map((subItem) => ({
414+
if (hasSubMenu && subItems?.length) {
415+
return subItems.map<CommandBarGroupType['items'][number]>((subItem) => ({
416416
title: `${title} / ${subItem.title}`,
417-
id,
418-
dataTestId: subItem.dataTestId,
417+
id: subItem.id,
419418
// Since icon is not present for some subItems, using from group
420419
icon: group.icon,
421420
// TODO: No href present for some subItems
@@ -439,7 +438,7 @@ export const RECENT_ACTIONS_GROUP: CommandBarGroupType = {
439438
title: 'Recent Navigation',
440439
}
441440

442-
export const RECENT_NAVIGATION_ITEM_ID_PREFIX = 'recent-navigation-'
441+
export const RECENT_NAVIGATION_ITEM_ID_PREFIX = 'recent-navigation-' as const
443442

444443
export const SHORT_CUTS: Record<
445444
'OPEN_COMMAND_BAR' | 'FOCUS_SEARCH_BAR' | 'NAVIGATE_UP' | 'NAVIGATE_DOWN',

src/Pages/Shared/CommandBar/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import {
55
NavigationSubMenuItemID,
66
Never,
77
URLS as CommonURLS,
8+
UserPreferencesType,
89
} from '@devtron-labs/devtron-fe-common-lib'
910

1011
import { URLS } from '@Config/routes'
1112

13+
import { RECENT_NAVIGATION_ITEM_ID_PREFIX } from './constants'
14+
1215
export type NavigationRootItemID =
1316
| 'application-management'
1417
| 'infrastructure-management'
@@ -52,8 +55,10 @@ export interface NavigationGroupType extends Pick<CommonNavigationItemType, 'tit
5255
items: NavigationItemType[]
5356
}
5457

58+
export type CommandBarActionIdType = UserPreferencesType['commandBar']['recentNavigationActions'][number]['id']
59+
5560
export type CommandBarItemType = {
56-
id: string
61+
id: CommandBarActionIdType | `${typeof RECENT_NAVIGATION_ITEM_ID_PREFIX}${CommandBarActionIdType}`
5762
title: string
5863
icon: IconsProps['name']
5964
} & (

0 commit comments

Comments
 (0)