Skip to content

Commit a45cb50

Browse files
committed
do not render MoneyRequestReportNavigation when unnecessary
1 parent 5f60c77 commit a45cb50

1 file changed

Lines changed: 60 additions & 17 deletions

File tree

src/components/MoneyRequestReportView/MoneyRequestReportNavigation.tsx

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
import React, {useEffect} from 'react';
22
import {View} from 'react-native';
3+
import type {OnyxEntry} from 'react-native-onyx';
34
import PrevNextButtons from '@components/PrevNextButtons';
45
import Text from '@components/Text';
6+
import useOnyx from '@hooks/useOnyx';
57
import useSearchSections from '@hooks/useSearchSections';
68
import useThemeStyles from '@hooks/useThemeStyles';
79
import Navigation from '@navigation/Navigation';
810
import {saveLastSearchParams} from '@userActions/ReportNavigation';
911
import {search} from '@userActions/Search';
1012
import CONST from '@src/CONST';
13+
import ONYXKEYS from '@src/ONYXKEYS';
14+
import type {SearchResults} from '@src/types/onyx';
15+
import type LastSearchParams from '@src/types/onyx/ReportNavigation';
1116

1217
type MoneyRequestReportNavigationProps = {
1318
reportID?: string;
1419
shouldDisplayNarrowVersion: boolean;
1520
};
1621

17-
function MoneyRequestReportNavigation({reportID, shouldDisplayNarrowVersion}: MoneyRequestReportNavigationProps) {
22+
const EMPTY_REPORT_IDS: string[] = [];
23+
24+
const selectIsExpenseReportSearch = (lastSearchQuery: OnyxEntry<LastSearchParams>): boolean => lastSearchQuery?.queryJSON?.type === CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT;
25+
26+
const selectQueryHash = (lastSearchQuery: OnyxEntry<LastSearchParams>): number | undefined => lastSearchQuery?.queryJSON?.hash;
27+
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));
37+
}
38+
}
39+
return ids;
40+
};
41+
42+
function MoneyRequestReportNavigationInner({reportID, shouldDisplayNarrowVersion}: MoneyRequestReportNavigationProps) {
1843
const {allReports, isSearchLoading, lastSearchQuery} = useSearchSections();
44+
const styles = useThemeStyles();
1945

20-
const type = lastSearchQuery?.queryJSON?.type;
2146
const currentIndex = allReports.indexOf(reportID);
2247
const allReportsCount = lastSearchQuery?.previousLengthOfResults ?? 0;
23-
2448
const hideNextButton = !lastSearchQuery?.hasMoreResults && currentIndex === allReports.length - 1;
2549
const hidePrevButton = currentIndex === 0;
26-
const styles = useThemeStyles();
27-
const isExpenseReportSearch = type === CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT;
28-
const shouldDisplayNavigationArrows = isExpenseReportSearch && allReports && allReports.length > 1 && currentIndex !== -1 && !!lastSearchQuery?.queryJSON;
50+
const shouldDisplayNavigationArrows = allReports.length > 1 && currentIndex !== -1 && !!lastSearchQuery?.queryJSON;
2951

3052
useEffect(() => {
3153
if (!lastSearchQuery?.queryJSON) {
@@ -100,18 +122,39 @@ function MoneyRequestReportNavigation({reportID, shouldDisplayNarrowVersion}: Mo
100122
goToReportId(allReports.at(prevIndex));
101123
};
102124

125+
if (!shouldDisplayNavigationArrows) {
126+
return null;
127+
}
128+
129+
return (
130+
<View style={[styles.flexRow, styles.alignItemsCenter, styles.gap2]}>
131+
{!shouldDisplayNarrowVersion && <Text style={styles.mutedTextLabel}>{`${currentIndex + 1} of ${allReportsCount}`}</Text>}
132+
<PrevNextButtons
133+
isPrevButtonDisabled={hidePrevButton}
134+
isNextButtonDisabled={hideNextButton}
135+
onNext={goToNextReport}
136+
onPrevious={goToPrevReport}
137+
/>
138+
</View>
139+
);
140+
}
141+
142+
function MoneyRequestReportNavigation({reportID, shouldDisplayNarrowVersion}: MoneyRequestReportNavigationProps) {
143+
const [isExpenseReportSearch] = useOnyx(ONYXKEYS.REPORT_NAVIGATION_LAST_SEARCH_QUERY, {selector: selectIsExpenseReportSearch});
144+
const [hash] = useOnyx(ONYXKEYS.REPORT_NAVIGATION_LAST_SEARCH_QUERY, {selector: selectQueryHash});
145+
const [snapshotReportIDs = EMPTY_REPORT_IDS] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`, {selector: selectSnapshotReportIDs});
146+
147+
const shouldMount = isExpenseReportSearch && !!reportID && snapshotReportIDs.length > 1 && snapshotReportIDs.includes(reportID);
148+
149+
if (!shouldMount) {
150+
return null;
151+
}
152+
103153
return (
104-
shouldDisplayNavigationArrows && (
105-
<View style={[styles.flexRow, styles.alignItemsCenter, styles.gap2]}>
106-
{!shouldDisplayNarrowVersion && <Text style={styles.mutedTextLabel}>{`${currentIndex + 1} of ${allReportsCount}`}</Text>}
107-
<PrevNextButtons
108-
isPrevButtonDisabled={hidePrevButton}
109-
isNextButtonDisabled={hideNextButton}
110-
onNext={goToNextReport}
111-
onPrevious={goToPrevReport}
112-
/>
113-
</View>
114-
)
154+
<MoneyRequestReportNavigationInner
155+
reportID={reportID}
156+
shouldDisplayNarrowVersion={shouldDisplayNarrowVersion}
157+
/>
115158
);
116159
}
117160

0 commit comments

Comments
 (0)