|
| 1 | +import {useFocusEffect} from '@react-navigation/native'; |
| 2 | +import React, {useCallback, useEffect, useState} from 'react'; |
| 3 | +import type {StyleProp, ViewStyle} from 'react-native'; |
| 4 | +import {useSession} from '@components/OnyxListItemProvider'; |
| 5 | +import SearchStaticList from '@components/Search/SearchStaticList'; |
| 6 | +import type {SearchQueryJSON} from '@components/Search/types'; |
| 7 | +import {hasDeferredWrite} from '@libs/deferredLayoutWrite'; |
| 8 | +import Navigation from '@libs/Navigation/Navigation'; |
| 9 | +import {isDefaultExpensesQuery} from '@libs/SearchQueryUtils'; |
| 10 | +import {getColumnsToShow, getValidGroupBy, isTransactionSearchType} from '@libs/SearchUIUtils'; |
| 11 | +import CONST from '@src/CONST'; |
| 12 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 13 | +import {columnsSelector} from '@src/selectors/AdvancedSearchFiltersForm'; |
| 14 | +import type {SearchResults} from '@src/types/onyx'; |
| 15 | +import useOnyx from './useOnyx'; |
| 16 | + |
| 17 | +const OVERLAY_SAFETY_TIMEOUT_MS = 5000; |
| 18 | + |
| 19 | +type UseSearchOverlayParams = { |
| 20 | + searchResults: SearchResults | undefined; |
| 21 | + queryJSON: SearchQueryJSON | undefined; |
| 22 | + shouldUseNarrowLayout: boolean; |
| 23 | + isMobileSelectionModeEnabled: boolean; |
| 24 | + currentSearchKey: string | undefined; |
| 25 | + /** FlatList content padding for narrow layout (accounts for filter bars). */ |
| 26 | + contentContainerStyle?: StyleProp<ViewStyle>; |
| 27 | +}; |
| 28 | + |
| 29 | +type UseSearchOverlayResult = { |
| 30 | + searchOverlayContent: React.ReactNode; |
| 31 | + onSearchContentReady: () => void; |
| 32 | + /** Whether the overlay lifecycle is active (armed but not yet ready). */ |
| 33 | + isOverlayActive: boolean; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Manages the SearchStaticList overlay shown above the Search content area |
| 38 | + * during expense-creation flows. The overlay is displayed when a deferred |
| 39 | + * write is pending or a fullscreen route has been pre-inserted under the RHP, |
| 40 | + * and dismissed once the real Search component signals readiness via |
| 41 | + * onContentReady, or after a safety timeout. |
| 42 | + */ |
| 43 | +function useSearchOverlay({ |
| 44 | + searchResults, |
| 45 | + queryJSON, |
| 46 | + shouldUseNarrowLayout, |
| 47 | + isMobileSelectionModeEnabled, |
| 48 | + currentSearchKey, |
| 49 | + contentContainerStyle, |
| 50 | +}: UseSearchOverlayParams): UseSearchOverlayResult { |
| 51 | + const session = useSession(); |
| 52 | + const accountID = session?.accountID ?? CONST.DEFAULT_NUMBER_ID; |
| 53 | + const [visibleColumns] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM, {selector: columnsSelector}); |
| 54 | + |
| 55 | + const [isSearchReady, setIsSearchReady] = useState(() => !hasDeferredWrite(CONST.DEFERRED_LAYOUT_WRITE_KEYS.SEARCH) && !Navigation.getIsFullscreenPreInsertedUnderRHP()); |
| 56 | + |
| 57 | + const onSearchContentReady = () => { |
| 58 | + setIsSearchReady(true); |
| 59 | + }; |
| 60 | + |
| 61 | + // Re-arm the overlay on focus when a new deferred write was registered |
| 62 | + // (e.g. a subsequent submit flow while Search stays mounted). |
| 63 | + useFocusEffect( |
| 64 | + useCallback(() => { |
| 65 | + if (!hasDeferredWrite(CONST.DEFERRED_LAYOUT_WRITE_KEYS.SEARCH) && !Navigation.getIsFullscreenPreInsertedUnderRHP()) { |
| 66 | + return; |
| 67 | + } |
| 68 | + setIsSearchReady(false); |
| 69 | + }, []), |
| 70 | + ); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + if (isSearchReady) { |
| 74 | + return; |
| 75 | + } |
| 76 | + const id = setTimeout(() => setIsSearchReady(true), OVERLAY_SAFETY_TIMEOUT_MS); |
| 77 | + return () => clearTimeout(id); |
| 78 | + }, [isSearchReady]); |
| 79 | + |
| 80 | + // When the overlay is dismissed, skip column computation and JSX creation. |
| 81 | + // The hook subscriptions (useSession, useOnyx) must remain unconditional per |
| 82 | + // rules-of-hooks, but the derived work below is the expensive part. |
| 83 | + if (isSearchReady) { |
| 84 | + return {searchOverlayContent: null, onSearchContentReady, isOverlayActive: false}; |
| 85 | + } |
| 86 | + |
| 87 | + const isTransaction = isTransactionSearchType(queryJSON?.type); |
| 88 | + const canSelectMultiple = isTransaction && (!shouldUseNarrowLayout || isMobileSelectionModeEnabled); |
| 89 | + |
| 90 | + const validGroupBy = queryJSON ? getValidGroupBy(queryJSON.groupBy) : undefined; |
| 91 | + const shouldUseStrictDefaultExpenseColumns = currentSearchKey === CONST.SEARCH.SEARCH_KEYS.EXPENSES && !!queryJSON && isDefaultExpensesQuery(queryJSON); |
| 92 | + |
| 93 | + const searchData = searchResults?.data; |
| 94 | + const overlayColumns = (() => { |
| 95 | + if (!searchData || !queryJSON) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + return getColumnsToShow({ |
| 99 | + currentAccountID: accountID, |
| 100 | + data: searchData, |
| 101 | + visibleColumns: visibleColumns ?? [], |
| 102 | + type: queryJSON.type, |
| 103 | + groupBy: validGroupBy, |
| 104 | + shouldUseStrictDefaultExpenseColumns, |
| 105 | + }); |
| 106 | + })(); |
| 107 | + |
| 108 | + // Narrow layout gets the custom contentContainerStyle (accounts for filter bars); |
| 109 | + // wide layout uses SearchStaticList's own internal padding (styles.pb3). |
| 110 | + const searchOverlayContent = |
| 111 | + isTransaction && searchData && queryJSON ? ( |
| 112 | + <SearchStaticList |
| 113 | + searchResults={searchResults} |
| 114 | + queryJSON={queryJSON} |
| 115 | + shouldUseNarrowLayout={shouldUseNarrowLayout} |
| 116 | + canSelectMultiple={canSelectMultiple} |
| 117 | + columns={overlayColumns} |
| 118 | + contentContainerStyle={shouldUseNarrowLayout ? contentContainerStyle : undefined} |
| 119 | + /> |
| 120 | + ) : null; |
| 121 | + |
| 122 | + return {searchOverlayContent, onSearchContentReady, isOverlayActive: true}; |
| 123 | +} |
| 124 | + |
| 125 | +export default useSearchOverlay; |
0 commit comments