Skip to content

Commit 93fc19e

Browse files
committed
address PERF-11: fold snapshot guard into selector
1 parent cb11ba2 commit 93fc19e

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

src/components/MoneyRequestReportView/MoneyRequestReportNavigation.tsx

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,41 @@ type MoneyRequestReportNavigationProps = {
1919
shouldDisplayNarrowVersion: boolean;
2020
};
2121

22-
const EMPTY_REPORT_IDS: string[] = [];
22+
type SnapshotGuard = {
23+
hasMultiple: boolean;
24+
includesReport: boolean;
25+
};
26+
27+
const EMPTY_GUARD: SnapshotGuard = {hasMultiple: false, includesReport: false};
2328

2429
const selectIsExpenseReportSearch = (lastSearchQuery: OnyxEntry<LastSearchParams>): boolean => lastSearchQuery?.queryJSON?.type === CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT;
2530

2631
const selectQueryHash = (lastSearchQuery: OnyxEntry<LastSearchParams>): number | undefined => lastSearchQuery?.queryJSON?.hash;
2732

28-
const selectSnapshotReportIDs = (snapshot: OnyxEntry<SearchResults>): string[] => {
29-
const data = snapshot?.data;
30-
if (!data) {
31-
return EMPTY_REPORT_IDS;
32-
}
33-
const ids: string[] = [];
34-
for (const key of Object.keys(data)) {
35-
if (key.startsWith(ONYXKEYS.COLLECTION.REPORT) && !key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS) && !key.startsWith(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS)) {
36-
ids.push(key.slice(ONYXKEYS.COLLECTION.REPORT.length));
33+
const buildSnapshotGuardSelector =
34+
(reportID: string | undefined) =>
35+
(snapshot: OnyxEntry<SearchResults>): SnapshotGuard => {
36+
const data = snapshot?.data;
37+
if (!data || !reportID) {
38+
return EMPTY_GUARD;
3739
}
38-
}
39-
return ids;
40-
};
40+
const prefix = ONYXKEYS.COLLECTION.REPORT;
41+
let count = 0;
42+
let includesReport = false;
43+
for (const key of Object.keys(data)) {
44+
if (!key.startsWith(prefix)) {
45+
continue;
46+
}
47+
count++;
48+
if (!includesReport && key.slice(prefix.length) === reportID) {
49+
includesReport = true;
50+
}
51+
if (count > 1 && includesReport) {
52+
break;
53+
}
54+
}
55+
return {hasMultiple: count > 1, includesReport};
56+
};
4157

4258
function MoneyRequestReportNavigationInner({reportID, shouldDisplayNarrowVersion}: MoneyRequestReportNavigationProps) {
4359
const {allReports, isSearchLoading, lastSearchQuery} = useSearchSections();
@@ -142,9 +158,10 @@ function MoneyRequestReportNavigationInner({reportID, shouldDisplayNarrowVersion
142158
function MoneyRequestReportNavigation({reportID, shouldDisplayNarrowVersion}: MoneyRequestReportNavigationProps) {
143159
const [isExpenseReportSearch] = useOnyx(ONYXKEYS.REPORT_NAVIGATION_LAST_SEARCH_QUERY, {selector: selectIsExpenseReportSearch});
144160
const [hash] = useOnyx(ONYXKEYS.REPORT_NAVIGATION_LAST_SEARCH_QUERY, {selector: selectQueryHash});
145-
const [snapshotReportIDs = EMPTY_REPORT_IDS] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`, {selector: selectSnapshotReportIDs});
161+
const snapshotGuardSelector = buildSnapshotGuardSelector(reportID);
162+
const [snapshotGuard = EMPTY_GUARD] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`, {selector: snapshotGuardSelector});
146163

147-
const shouldMount = isExpenseReportSearch && !!reportID && snapshotReportIDs.length > 1 && snapshotReportIDs.includes(reportID);
164+
const shouldMount = isExpenseReportSearch && snapshotGuard.hasMultiple && snapshotGuard.includesReport;
148165

149166
if (!shouldMount) {
150167
return null;

0 commit comments

Comments
 (0)