@@ -12,6 +12,8 @@ import {
12
12
SearchBar ,
13
13
stopPropagation ,
14
14
SupportedKeyboardKeysType ,
15
+ ToastManager ,
16
+ ToastVariantType ,
15
17
updateUserPreferences ,
16
18
useQuery ,
17
19
useRegisterShortcut ,
@@ -20,7 +22,8 @@ import {
20
22
21
23
import CommandGroup from './CommandGroup'
22
24
import { NAVIGATION_GROUPS , RECENT_ACTIONS_GROUP , RECENT_NAVIGATION_ITEM_ID_PREFIX , SHORT_CUTS } from './constants'
23
- import { CommandBarActionIdType , CommandBarBackdropProps , CommandBarGroupType , CommandBarItemType } from './types'
25
+ import { CommandBarBackdropProps , CommandBarGroupType } from './types'
26
+ import { getNewSelectedIndex , sanitizeItemId } from './utils'
24
27
25
28
const CommandBarBackdrop = ( { handleClose } : CommandBarBackdropProps ) => {
26
29
const history = useHistory ( )
@@ -49,10 +52,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
49
52
if ( requiredGroup ) {
50
53
const requiredItem = requiredGroup . items . find ( ( item ) => item . id === action . id )
51
54
requiredItem . id = `${ RECENT_NAVIGATION_ITEM_ID_PREFIX } ${ action . id } `
52
-
53
- if ( requiredItem ) {
54
- acc . items . push ( structuredClone ( requiredItem ) )
55
- }
55
+ acc . items . push ( structuredClone ( requiredItem ) )
56
56
}
57
57
return acc
58
58
} , structuredClone ( RECENT_ACTIONS_GROUP ) ) ,
@@ -74,22 +74,26 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
74
74
itemRefMap . current [ id ] = el
75
75
}
76
76
77
- const lowerCaseSearchText = searchText . toLowerCase ( )
77
+ const filteredGroups = useMemo ( ( ) => {
78
+ const lowerCaseSearchText = searchText . toLowerCase ( )
78
79
79
- const filteredGroups = searchText
80
- ? NAVIGATION_GROUPS . reduce < typeof NAVIGATION_GROUPS > ( ( acc , group ) => {
81
- const filteredItems = group . items . filter ( ( item ) => item . title . toLowerCase ( ) . includes ( lowerCaseSearchText ) )
80
+ if ( ! searchText ) {
81
+ return NAVIGATION_GROUPS
82
+ }
82
83
83
- if ( filteredItems . length > 0 ) {
84
- acc . push ( {
85
- ...group ,
86
- items : filteredItems ,
87
- } )
88
- }
84
+ return NAVIGATION_GROUPS . reduce < typeof NAVIGATION_GROUPS > ( ( acc , group ) => {
85
+ const filteredItems = group . items . filter ( ( item ) => item . title . toLowerCase ( ) . includes ( lowerCaseSearchText ) )
89
86
90
- return acc
91
- } , [ ] )
92
- : NAVIGATION_GROUPS
87
+ if ( filteredItems . length > 0 ) {
88
+ acc . push ( {
89
+ ...group ,
90
+ items : filteredItems ,
91
+ } )
92
+ }
93
+
94
+ return acc
95
+ } , [ ] )
96
+ } , [ searchText ] )
93
97
94
98
const itemFlatList : CommandBarGroupType [ 'items' ] = useMemo ( ( ) => {
95
99
if ( areFiltersApplied ) {
@@ -120,21 +124,13 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
120
124
}
121
125
}
122
126
123
- const getNewSelectedIndex = ( prevIndex : number , type : 'up' | 'down' ) => {
124
- if ( type === 'up' ) {
125
- return prevIndex === 0 ? itemFlatList . length - 1 : prevIndex - 1
126
- }
127
- return prevIndex === itemFlatList . length - 1 ? 0 : prevIndex + 1
128
- }
129
-
130
127
const handleNavigation = ( type : 'up' | 'down' ) => {
131
128
if ( ! itemFlatList . length ) {
132
129
return
133
130
}
134
131
135
- // Want this to have cyclic navigation
136
132
setSelectedItemIndex ( ( prevIndex ) => {
137
- const newIndex = getNewSelectedIndex ( prevIndex , type )
133
+ const newIndex = getNewSelectedIndex ( prevIndex , type , itemFlatList . length )
138
134
const item = itemFlatList [ newIndex ]
139
135
const itemElement = itemRefMap . current [ item . id ]
140
136
if ( itemElement ) {
@@ -144,14 +140,13 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
144
140
} )
145
141
}
146
142
147
- const sanitizeItemId = ( item : CommandBarItemType ) =>
148
- ( item . id . startsWith ( RECENT_NAVIGATION_ITEM_ID_PREFIX )
149
- ? item . id . replace ( RECENT_NAVIGATION_ITEM_ID_PREFIX , '' )
150
- : item . id ) as CommandBarActionIdType
151
-
152
143
const onItemClick = async ( item : CommandBarGroupType [ 'items' ] [ number ] ) => {
153
144
if ( ! item . href ) {
154
145
logExceptionToSentry ( new Error ( `CommandBar item with id ${ item . id } does not have a valid href` ) )
146
+ ToastManager . showToast ( {
147
+ variant : ToastVariantType . error ,
148
+ description : `CommandBar item with id ${ item . id } does not have a valid href` ,
149
+ } )
155
150
return
156
151
}
157
152
@@ -187,6 +182,7 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
187
182
}
188
183
}
189
184
185
+ // Intention: To retain the selected item index when recent actions are loaded
190
186
useEffect ( ( ) => {
191
187
if ( ! isLoading && recentActionsGroup ?. items ?. length && ! areFiltersApplied ) {
192
188
if ( selectedItemIndex !== 0 ) {
@@ -203,11 +199,11 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
203
199
} , [ isLoading , recentActionsGroup ] )
204
200
205
201
useEffect ( ( ) => {
206
- const { keys } = SHORT_CUTS . FOCUS_SEARCH_BAR
202
+ const { keys, description } = SHORT_CUTS . FOCUS_SEARCH_BAR
207
203
208
204
registerShortcut ( {
209
205
keys,
210
- description : SHORT_CUTS . FOCUS_SEARCH_BAR . description ,
206
+ description,
211
207
callback : focusSearchBar ,
212
208
} )
213
209
@@ -217,10 +213,11 @@ const CommandBarBackdrop = ({ handleClose }: CommandBarBackdropProps) => {
217
213
} , [ ] )
218
214
219
215
useEffect ( ( ) => {
220
- const { keys } = SHORT_CUTS . ENTER_ITEM
216
+ const { keys, description } = SHORT_CUTS . ENTER_ITEM
217
+
221
218
registerShortcut ( {
222
219
keys,
223
- description : SHORT_CUTS . ENTER_ITEM . description ,
220
+ description,
224
221
callback : handleEnterSelectedItem ,
225
222
} )
226
223
0 commit comments