1- import type { OnyxEntry } from 'react-native-onyx' ;
2- import { selectFilteredReportActions } from '@libs/ReportUtils' ;
1+ import type { OnyxCollection , OnyxEntry } from 'react-native-onyx' ;
2+ import { isReportPendingDelete , selectFilteredReportActions } from '@libs/ReportUtils' ;
33import { getSections , getSortedSections } from '@libs/SearchUIUtils' ;
4+ import CONST from '@src/CONST' ;
45import ONYXKEYS from '@src/ONYXKEYS' ;
6+ import type { Report } from '@src/types/onyx' ;
57import type LastSearchParams from '@src/types/onyx/ReportNavigation' ;
68import useActionLoadingReportIDs from './useActionLoadingReportIDs' ;
79import useArchivedReportsIdSet from './useArchivedReportsIdSet' ;
810import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails' ;
911import useLocalize from './useLocalize' ;
1012import useOnyx from './useOnyx' ;
1113
14+ /**
15+ * Returns sorted keys of reports pending deletion.
16+ * Sorted string[] keeps Onyx comparison cheap (PERF-11).
17+ */
18+ const selectPendingDeleteReportKeys = ( reports : OnyxCollection < Report > ) : string [ ] => {
19+ const keys : string [ ] = [ ] ;
20+ for ( const [ key , report ] of Object . entries ( reports ?? { } ) ) {
21+ if ( isReportPendingDelete ( report ) ) {
22+ keys . push ( key ) ;
23+ }
24+ }
25+ return keys . sort ( ) ;
26+ } ;
27+
1228type UseSearchSectionsResult = {
1329 allReports : Array < string | undefined > ;
1430 isSearchLoading : boolean ;
@@ -18,6 +34,8 @@ type UseSearchSectionsResult = {
1834function useSearchSections ( ) : UseSearchSectionsResult {
1935 const [ lastSearchQuery ] = useOnyx ( ONYXKEYS . REPORT_NAVIGATION_LAST_SEARCH_QUERY ) ;
2036 const [ currentSearchResults ] = useOnyx ( `${ ONYXKEYS . COLLECTION . SNAPSHOT } ${ lastSearchQuery ?. queryJSON ?. hash } ` ) ;
37+ const [ pendingDeleteReportKeys = CONST . EMPTY_ARRAY ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT , { selector : selectPendingDeleteReportKeys } ) ;
38+ const pendingDeleteReportKeysSet = new Set ( pendingDeleteReportKeys ) ;
2139 const currentUserDetails = useCurrentUserPersonalDetails ( ) ;
2240 const { localeCompare, formatPhoneNumber, translate} = useLocalize ( ) ;
2341 const isActionLoadingSet = useActionLoadingReportIDs ( ) ;
@@ -42,7 +60,7 @@ function useSearchSections(): UseSearchSectionsResult {
4260 const currentUserEmail = currentUserDetails . email ?? '' ;
4361 const searchKey = lastSearchQuery ?. searchKey ;
4462
45- let allReports : Array < string | undefined > = [ ] ;
63+ let results : Array < string | undefined > = [ ] ;
4664 if ( ! ! type && ! ! searchResultsData && ! ! searchResultsSearch ) {
4765 const [ searchData ] = getSections ( {
4866 type,
@@ -62,10 +80,18 @@ function useSearchSections(): UseSearchSectionsResult {
6280 cardList,
6381 conciergeReportID,
6482 } ) ;
65- allReports = getSortedSections ( type , status ?? '' , searchData , localeCompare , translate , sortBy , sortOrder , groupBy ) . map ( ( value ) => value . reportID ) ;
83+ results = getSortedSections ( type , status ?? '' , searchData , localeCompare , translate , sortBy , sortOrder , groupBy ) . map ( ( value ) => value . reportID ) ;
6684 }
6785
86+ const allReports = results . filter ( ( id ) => {
87+ if ( ! id ) {
88+ return false ;
89+ }
90+ return ! pendingDeleteReportKeysSet . has ( `${ ONYXKEYS . COLLECTION . REPORT } ${ id } ` ) ;
91+ } ) ;
92+
6893 return { allReports, isSearchLoading : ! ! currentSearchResults ?. search ?. isLoading , lastSearchQuery} ;
6994}
7095
96+ export { selectPendingDeleteReportKeys } ;
7197export default useSearchSections ;
0 commit comments