|
1 | 1 | import React, {useEffect} from 'react'; |
2 | 2 | import {View} from 'react-native'; |
| 3 | +import type {OnyxEntry} from 'react-native-onyx'; |
3 | 4 | import PrevNextButtons from '@components/PrevNextButtons'; |
4 | 5 | import Text from '@components/Text'; |
| 6 | +import useOnyx from '@hooks/useOnyx'; |
5 | 7 | import useSearchSections from '@hooks/useSearchSections'; |
6 | 8 | import useThemeStyles from '@hooks/useThemeStyles'; |
7 | 9 | import Navigation from '@navigation/Navigation'; |
8 | 10 | import {saveLastSearchParams} from '@userActions/ReportNavigation'; |
9 | 11 | import {search} from '@userActions/Search'; |
10 | 12 | 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'; |
11 | 16 |
|
12 | 17 | type MoneyRequestReportNavigationProps = { |
13 | 18 | reportID?: string; |
14 | 19 | shouldDisplayNarrowVersion: boolean; |
15 | 20 | }; |
16 | 21 |
|
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) { |
18 | 43 | const {allReports, isSearchLoading, lastSearchQuery} = useSearchSections(); |
| 44 | + const styles = useThemeStyles(); |
19 | 45 |
|
20 | | - const type = lastSearchQuery?.queryJSON?.type; |
21 | 46 | const currentIndex = allReports.indexOf(reportID); |
22 | 47 | const allReportsCount = lastSearchQuery?.previousLengthOfResults ?? 0; |
23 | | - |
24 | 48 | const hideNextButton = !lastSearchQuery?.hasMoreResults && currentIndex === allReports.length - 1; |
25 | 49 | 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; |
29 | 51 |
|
30 | 52 | useEffect(() => { |
31 | 53 | if (!lastSearchQuery?.queryJSON) { |
@@ -100,18 +122,39 @@ function MoneyRequestReportNavigation({reportID, shouldDisplayNarrowVersion}: Mo |
100 | 122 | goToReportId(allReports.at(prevIndex)); |
101 | 123 | }; |
102 | 124 |
|
| 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 | + |
103 | 153 | 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 | + /> |
115 | 158 | ); |
116 | 159 | } |
117 | 160 |
|
|
0 commit comments