@@ -16,6 +16,8 @@ import usePermissions from '@hooks/usePermissions';
16
16
import useSingleExecution from '@hooks/useSingleExecution' ;
17
17
import useThemeStyles from '@hooks/useThemeStyles' ;
18
18
import useWaitForNavigation from '@hooks/useWaitForNavigation' ;
19
+ import type { WorkspaceListItem } from '@hooks/useWorkspaceList' ;
20
+ import useWorkspaceList from '@hooks/useWorkspaceList' ;
19
21
import { clearAllFilters , saveSearch } from '@libs/actions/Search' ;
20
22
import { createCardFeedKey , getCardFeedKey , getCardFeedNamesWithType , getWorkspaceCardFeedKey } from '@libs/CardFeedUtils' ;
21
23
import { getCardDescription , mergeCardListWithWorkspaceFeeds } from '@libs/CardUtils' ;
@@ -160,7 +162,7 @@ const baseFilterConfig = {
160
162
*/
161
163
const typeFiltersKeys : Record < string , Array < Array < ValueOf < typeof CONST . SEARCH . SYNTAX_FILTER_KEYS > > > > = {
162
164
[ CONST . SEARCH . DATA_TYPES . EXPENSE ] : [
163
- [ CONST . SEARCH . SYNTAX_FILTER_KEYS . KEYWORD , CONST . SEARCH . SYNTAX_FILTER_KEYS . FROM , CONST . SEARCH . SYNTAX_FILTER_KEYS . TO ] ,
165
+ [ CONST . SEARCH . SYNTAX_FILTER_KEYS . KEYWORD , CONST . SEARCH . SYNTAX_FILTER_KEYS . FROM , CONST . SEARCH . SYNTAX_FILTER_KEYS . TO , CONST . SEARCH . SYNTAX_FILTER_KEYS . POLICY_ID ] ,
164
166
[
165
167
CONST . SEARCH . SYNTAX_FILTER_KEYS . EXPENSE_TYPE ,
166
168
CONST . SEARCH . SYNTAX_FILTER_KEYS . MERCHANT ,
@@ -231,9 +233,8 @@ const typeFiltersKeys: Record<string, Array<Array<ValueOf<typeof CONST.SEARCH.SY
231
233
] ,
232
234
} ;
233
235
234
- function getFilterWorkspaceDisplayTitle ( filters : Partial < SearchAdvancedFiltersForm > , policies : OnyxCollection < Policy > ) {
235
- const workspaceFilter = filters [ CONST . SEARCH . SYNTAX_FILTER_KEYS . POLICY_ID ] ;
236
- return policies ?. [ `${ ONYXKEYS . COLLECTION . POLICY } ${ workspaceFilter } ` ] ?. name ?? workspaceFilter ;
236
+ function getFilterWorkspaceDisplayTitle ( filters : SearchAdvancedFiltersForm , policies : WorkspaceListItem [ ] ) {
237
+ return policies . filter ( ( value ) => value . policyID === filters . policyID ) . at ( 0 ) ?. text ;
237
238
}
238
239
239
240
function getFilterCardDisplayTitle ( filters : Partial < SearchAdvancedFiltersForm > , cards : CardList , translate : LocaleContextProps [ 'translate' ] ) {
@@ -433,24 +434,6 @@ function AdvancedSearchFilters() {
433
434
const personalDetails = usePersonalDetails ( ) ;
434
435
const { canUseLeftHandBar} = usePermissions ( ) ;
435
436
436
- // If users have access to the leftHandBar beta, then the workspace filter is displyed in the first section of the advanced search filters
437
- const typeFiltersKeysWithOptionalPolicy = useMemo (
438
- ( ) =>
439
- canUseLeftHandBar
440
- ? Object . fromEntries (
441
- Object . entries ( typeFiltersKeys ) . map ( ( [ key , arrays ] ) => {
442
- const firstFiltersSection = arrays . at ( 0 ) ;
443
- if ( ! firstFiltersSection ) {
444
- return [ key , arrays ] ;
445
- }
446
-
447
- const modifiedFirstFiltersSection = [ ...firstFiltersSection , CONST . SEARCH . SYNTAX_FILTER_KEYS . POLICY_ID ] ;
448
- return [ key , [ modifiedFirstFiltersSection , ...arrays . slice ( 1 ) ] ] ;
449
- } ) ,
450
- )
451
- : typeFiltersKeys ,
452
- [ canUseLeftHandBar ] ,
453
- ) ;
454
437
const [ policies = { } ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY ) ;
455
438
const [ allPolicyCategories = { } ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY_CATEGORIES , {
456
439
selector : ( policyCategories ) =>
@@ -469,6 +452,16 @@ function AdvancedSearchFilters() {
469
452
. map ( getTagNamesFromTagsLists )
470
453
. flat ( ) ;
471
454
455
+ const [ currentUserLogin ] = useOnyx ( ONYXKEYS . SESSION , { selector : ( session ) => session ?. email } ) ;
456
+
457
+ const { sections : workspaces } = useWorkspaceList ( {
458
+ policies,
459
+ currentUserLogin,
460
+ shouldShowPendingDeletePolicy : false ,
461
+ selectedPolicyID : undefined ,
462
+ searchTerm : '' ,
463
+ } ) ;
464
+
472
465
// When looking if a user has any categories to display, we want to ignore the policies that are of type PERSONAL
473
466
const nonPersonalPolicyCategoryIds = Object . values ( policies )
474
467
. filter ( ( policy ) : policy is NonNullable < Policy > => ! ! ( policy && policy . type !== CONST . POLICY . TYPE . PERSONAL ) )
@@ -486,9 +479,12 @@ function AdvancedSearchFilters() {
486
479
const shouldDisplayTagFilter = shouldDisplayFilter ( tagListsUnpacked . length , areTagsEnabled , ! ! singlePolicyTagLists ) ;
487
480
const shouldDisplayCardFilter = shouldDisplayFilter ( Object . keys ( allCards ) . length , areCardsEnabled ) ;
488
481
const shouldDisplayTaxFilter = shouldDisplayFilter ( Object . keys ( taxRates ) . length , areTaxEnabled ) ;
482
+ const shouldDisplayWorkspaceFilter = useMemo ( ( ) => {
483
+ return canUseLeftHandBar && workspaces . some ( ( section ) => section . data . length !== 0 ) ;
484
+ } , [ canUseLeftHandBar , workspaces ] ) ;
489
485
490
486
let currentType = searchAdvancedFilters ?. type ?? CONST . SEARCH . DATA_TYPES . EXPENSE ;
491
- if ( ! Object . keys ( typeFiltersKeysWithOptionalPolicy ) . includes ( currentType ) ) {
487
+ if ( ! Object . keys ( typeFiltersKeys ) . includes ( currentType ) ) {
492
488
currentType = CONST . SEARCH . DATA_TYPES . EXPENSE ;
493
489
}
494
490
@@ -520,7 +516,7 @@ function AdvancedSearchFilters() {
520
516
applyFiltersAndNavigate ( ) ;
521
517
} ;
522
518
523
- const filters = typeFiltersKeysWithOptionalPolicy [ currentType ]
519
+ const filters = typeFiltersKeys [ currentType ]
524
520
. map ( ( section ) => {
525
521
return section
526
522
. map ( ( key ) => {
@@ -578,7 +574,11 @@ function AdvancedSearchFilters() {
578
574
} else if ( key === CONST . SEARCH . SYNTAX_FILTER_KEYS . REIMBURSABLE || key === CONST . SEARCH . SYNTAX_FILTER_KEYS . BILLABLE ) {
579
575
filterTitle = baseFilterConfig [ key ] . getTitle ( searchAdvancedFilters , key , translate ) ;
580
576
} else if ( key === CONST . SEARCH . SYNTAX_FILTER_KEYS . POLICY_ID ) {
581
- filterTitle = baseFilterConfig [ key ] . getTitle ( searchAdvancedFilters , policies ) ;
577
+ if ( ! shouldDisplayWorkspaceFilter ) {
578
+ return ;
579
+ }
580
+ const workspacesData = workspaces . flatMap ( ( value ) => value . data ) ;
581
+ filterTitle = baseFilterConfig [ key ] . getTitle ( searchAdvancedFilters , workspacesData ) ;
582
582
}
583
583
return {
584
584
key,
0 commit comments