@@ -17,31 +17,28 @@ type PolicyExpenseParticipant = {
1717
1818/**
1919 * Resolves the expense reports for the policy expense chat participants without subscribing the consumer to the
20- * entire reports collection. Returns a `getReportByID` resolver so callers only re-render when one of the relevant
21- * reports changes.
20+ * entire reports collection, so callers only re-render when one of the relevant reports changes.
21+ *
22+ * Returns a `getReportByID` resolver (rather than the raw reports collection) because it matches the resolver
23+ * contract expected by `formatSectionsFromSearchTerm` and keeps the Onyx key construction encapsulated here
24+ * instead of being duplicated at every call site.
2225 */
2326export default function useSelectedExpenseReports ( participants : readonly PolicyExpenseParticipant [ ] ) {
2427 const expenseReportIDs = participants
2528 . filter ( ( participant ) : participant is PolicyExpenseParticipant & { reportID : string } => ! ! participant . isPolicyExpenseChat && ! ! participant . reportID )
2629 . map ( ( participant ) => participant . reportID ) ;
2730
28- // Serialized key used to refresh the selector only when the set of relevant report IDs actually changes.
29- const expenseReportIDsKey = expenseReportIDs . join ( ',' ) ;
30-
31- const [ expenseReports ] = useOnyx (
32- ONYXKEYS . COLLECTION . REPORT ,
33- {
34- selector : ( reports : OnyxCollection < Report > ) => {
35- const result : OnyxCollection < Report > = { } ;
36- for ( const reportID of expenseReportIDs ) {
37- const key = `${ ONYXKEYS . COLLECTION . REPORT } ${ getNonEmptyStringOnyxID ( reportID ) } ` as const ;
38- result [ key ] = reports ?. [ key ] ;
39- }
40- return result ;
41- } ,
31+ // React Compiler memoizes the selector on `expenseReportIDs`, so it re-runs whenever the set of relevant report IDs changes.
32+ const [ expenseReports ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT , {
33+ selector : ( reports : OnyxCollection < Report > ) => {
34+ const result : OnyxCollection < Report > = { } ;
35+ for ( const reportID of expenseReportIDs ) {
36+ const key = `${ ONYXKEYS . COLLECTION . REPORT } ${ getNonEmptyStringOnyxID ( reportID ) } ` as const ;
37+ result [ key ] = reports ?. [ key ] ;
38+ }
39+ return result ;
4240 } ,
43- [ expenseReportIDsKey ] ,
44- ) ;
41+ } ) ;
4542
4643 return ( reportID : string | undefined ) : OnyxEntry < Report > => expenseReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ getNonEmptyStringOnyxID ( reportID ) } ` ] ;
4744}
0 commit comments