Skip to content

Commit 577701b

Browse files
committed
perf: fix PERF-11 in useUpcomingTravelReservations
1 parent 9c594fd commit 577701b

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import useOnyx from '@hooks/useOnyx';
2+
import {isTripRoom} from '@libs/ReportUtils';
3+
import ONYXKEYS from '@src/ONYXKEYS';
4+
import type {Report} from '@src/types/onyx';
5+
6+
function useTripRoomReports(): Report[] {
7+
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
8+
9+
return Object.values(reports ?? {}).filter((report): report is Report => !!report && isTripRoom(report));
10+
}
11+
12+
export default useTripRoomReports;

src/pages/home/UpcomingTravelSection/useUpcomingTravelReservations.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
import {accountIDSelector} from '@selectors/Session';
22
import {useMemo} from 'react';
3-
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
43
import useOnyx from '@hooks/useOnyx';
5-
import {isTripRoom} from '@libs/ReportUtils';
64
import type {ReservationData} from '@libs/TripReservationUtils';
75
import {getReservationsFromTripReport} from '@libs/TripReservationUtils';
86
import CONST from '@src/CONST';
97
import ONYXKEYS from '@src/ONYXKEYS';
10-
import type {Report} from '@src/types/onyx';
11-
import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems';
8+
import useTripRoomReports from './useTripRoomReports';
129

1310
type UpcomingReservation = ReservationData & {
1411
reportID: string;
1512
};
1613

17-
const tripRoomSelector = (report: OnyxEntry<Report>): Report | undefined => {
18-
if (!report || !isTripRoom(report)) {
19-
return;
20-
}
21-
return report;
22-
};
23-
24-
const allTripRoomsSelector = (reports: OnyxCollection<Report>) => mapOnyxCollectionItems(reports, tripRoomSelector);
25-
2614
function useUpcomingTravelReservations(): UpcomingReservation[] {
27-
const [tripRoomReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: allTripRoomsSelector});
15+
const tripRoomReports = useTripRoomReports();
2816
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector});
2917

3018
return useMemo(() => {
3119
const now = new Date();
3220
const windowEnd = new Date(now);
3321
windowEnd.setDate(windowEnd.getDate() + CONST.UPCOMING_TRAVEL_WINDOW_DAYS);
3422

35-
const reports = Object.values(tripRoomReports ?? {});
3623
const upcoming: UpcomingReservation[] = [];
3724

38-
for (const report of reports) {
25+
for (const report of tripRoomReports) {
3926
// Only include reservations where the current user is the traveler
40-
if (!report || report.ownerAccountID !== accountID) {
27+
if (report.ownerAccountID !== accountID) {
4128
continue;
4229
}
4330
const reservations = getReservationsFromTripReport(report);

0 commit comments

Comments
 (0)