Skip to content

Commit a01e0ef

Browse files
committed
resolved comments from dylan
1 parent 2cada59 commit a01e0ef

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

src/hooks/useSelectedExpenseReports.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
2326
export 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
}

tests/unit/OptionsListUtilsTest.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7948,6 +7948,7 @@ describe('OptionsListUtils', () => {
79487948
privateIsArchivedMap,
79497949
CURRENT_USER_ACCOUNT_ID,
79507950
undefined,
7951+
translateLocal,
79517952
formatPersonalDetails,
79527953
true,
79537954
undefined,

0 commit comments

Comments
 (0)