Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import MenuItem from '@components/MenuItem';
import Modal from '@components/Modal';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import openSearchReport from '@components/Search/openSearchReport';
import {useSearchContext} from '@components/Search/SearchContext';
import type {SearchColumnType, SortOrder} from '@components/Search/types';
import Text from '@components/Text';
Expand Down Expand Up @@ -49,7 +50,6 @@ import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import MoneyRequestReportTableHeader from './MoneyRequestReportTableHeader';
Expand Down Expand Up @@ -266,7 +266,7 @@ function MoneyRequestReportTransactionList({
if (reportIDToNavigate) {
markReportIDAsExpense(reportIDToNavigate);
}
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute(routeParams));
openSearchReport(routeParams.reportID, backTo);
});
},
[report, reportActions, sortedTransactions, markReportIDAsExpense],
Expand Down
7 changes: 5 additions & 2 deletions src/components/Navigation/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import React, {useContext} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import LoadingBar from '@components/LoadingBar';
import {PressableWithoutFeedback} from '@components/Pressable';
import SearchButton from '@components/Search/SearchRouter/SearchButton';
import HelpButton from '@components/SidePanel/HelpComponents/HelpButton';
import Text from '@components/Text';
import {WideRHPContext} from '@components/WideRHPContextProvider';
import useLoadingBarVisibility from '@hooks/useLoadingBarVisibility';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
Expand All @@ -32,6 +33,8 @@ function TopBar({breadcrumbLabel, shouldDisplaySearch = true, shouldDisplayHelpB
const [session] = useOnyx(ONYXKEYS.SESSION, {selector: authTokenTypeSelector, canBeMissing: true});
const shouldShowLoadingBarForReports = useLoadingBarVisibility();
const isAnonymousUser = isAnonymousUserUtil(session);
const {wideRHPRouteKeys} = useContext(WideRHPContext);
const isWideRhpVisible = !!wideRHPRouteKeys.length;

const displaySignIn = isAnonymousUser;
const displaySearch = !isAnonymousUser && shouldDisplaySearch;
Expand Down Expand Up @@ -68,7 +71,7 @@ function TopBar({breadcrumbLabel, shouldDisplaySearch = true, shouldDisplayHelpB
{shouldDisplayHelpButton && <HelpButton />}
{displaySearch && <SearchButton />}
</View>
<LoadingBar shouldShow={shouldShowLoadingBarForReports || shouldShowLoadingBar} />
<LoadingBar shouldShow={!isWideRhpVisible && (shouldShowLoadingBarForReports || shouldShowLoadingBar)} />
</View>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import type {SearchTransaction} from '@src/types/onyx/SearchResults';
import type {TransactionViolation} from '@src/types/onyx/TransactionViolation';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import arraysEqual from '@src/utils/arraysEqual';
import openSearchReport from './openSearchReport';
import {useSearchContext} from './SearchContext';
import SearchList from './SearchList';
import {SearchScopeProvider} from './SearchScopeProvider';
Expand Down Expand Up @@ -712,7 +713,7 @@ function Search({
setOptimisticDataForTransactionThreadPreview(item, transactionPreviewData);
}

requestAnimationFrame(() => Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID, backTo})));
openSearchReport(reportID, backTo);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see the ESLint warning?

React Hook useCallback has an unnecessary dependency: 'type'. Either exclude it or remove the dependency array

Seems like type is not used so you could remove it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've missed that, thanks for pointing out

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, thanks!

},
[isMobileSelectionModeEnabled, type, toggleTransaction, hash, queryJSON, handleSearch, searchKey, markReportIDAsExpense],
);
Expand Down
6 changes: 6 additions & 0 deletions src/components/Search/openSearchReport/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Navigation from '@libs/Navigation/Navigation';
import ROUTES from '@src/ROUTES';

export default function openSearchReport(reportID: string | undefined, backTo: string) {
requestAnimationFrame(() => Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID, backTo})));
}
17 changes: 17 additions & 0 deletions src/components/Search/openSearchReport/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {CommonActions} from '@react-navigation/native';
import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';

// The search report screen is preloaded before navigating to avoid lag when opening the page. If the content is rendered in the background and then navigated, the opening experience is smoother.
export default function openSearchReport(reportID: string | undefined, backTo: string) {
navigationRef.dispatch({

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment on why we are preloading?

...CommonActions.preload(NAVIGATORS.RIGHT_MODAL_NAVIGATOR, {
name: SCREENS.RIGHT_MODAL.SEARCH_REPORT,
params: {reportID, backTo},
}),
target: navigationRef.getRootState().key,
});
requestAnimationFrame(() => Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID, backTo})));
}
36 changes: 18 additions & 18 deletions src/components/WideRHPContextProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ function extractNavigationKeys(state: NavigationState | PartialState<NavigationS
return keys;
}

/**
* Returns the route key of the last visible RHP navigator,
* or undefined if RHP is not currently visible.
*/
function getLastVisibleRHPRouteKey(state: NavigationState): string | undefined {
const lastFullScreenRouteIndex = state.routes.findLastIndex((route) => isFullScreenName(route.name));
const lastRHPRouteIndex = state.routes.findLastIndex((route) => route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR);

// Both routes have to be present and the RHP have to be after last full screen for it to be visible.
if (lastFullScreenRouteIndex === -1 || lastRHPRouteIndex === -1 || lastFullScreenRouteIndex > lastRHPRouteIndex) {
return undefined;
}

return state?.routes.at(lastRHPRouteIndex)?.key;
}

/**
* Calculates the optimal width for the receipt pane RHP based on window width.
* Ensures the RHP doesn't exceed maximum width and maintains minimum responsive width.
Expand All @@ -81,30 +97,14 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) {
const [shouldRenderSecondaryOverlay, setShouldRenderSecondaryOverlay] = useState(false);
const [expenseReportIDs, setExpenseReportIDs] = useState<Set<string>>(new Set());

// Return undefined if RHP is not the last route
const lastVisibleRHPRouteKey = useRootNavigationState((state) => {
// Safe handling when navigation is not yet initialized
if (!state) {
return undefined;
}
const lastFullScreenRouteIndex = state?.routes.findLastIndex((route) => isFullScreenName(route.name));
const lastRHPRouteIndex = state?.routes.findLastIndex((route) => route.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR);

// Both routes have to be present and the RHP have to be after last full screen for it to be visible.
if (lastFullScreenRouteIndex === -1 || lastRHPRouteIndex === -1 || lastFullScreenRouteIndex > lastRHPRouteIndex) {
return undefined;
}

return state?.routes.at(lastRHPRouteIndex)?.key;
});

const wideRHPRouteKeys = useMemo(() => {
const rootState = navigationRef.getRootState();

if (!rootState) {
return [];
}

const lastVisibleRHPRouteKey = getLastVisibleRHPRouteKey(rootState);
const lastRHPRoute = rootState.routes.find((route) => route.key === lastVisibleRHPRouteKey);

if (!lastRHPRoute) {
Expand All @@ -115,7 +115,7 @@ function WideRHPContextProvider({children}: React.PropsWithChildren) {
const currentKeys = allWideRHPRouteKeys.filter((key) => lastRHPKeys.has(key));

return currentKeys;
}, [allWideRHPRouteKeys, lastVisibleRHPRouteKey]);
}, [allWideRHPRouteKeys]);

/**
* Determines whether the secondary overlay should be displayed.
Expand Down
3 changes: 2 additions & 1 deletion src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type DotLottieAnimation from '@components/LottieAnimations/types';
import type {MenuItemWithLink} from '@components/MenuItemList';
import type {MultiSelectItem} from '@components/Search/FilterDropdowns/MultiSelectPopup';
import type {SingleSelectItem} from '@components/Search/FilterDropdowns/SingleSelectPopup';
import openSearchReport from '@components/Search/openSearchReport';
import type {
SearchAction,
SearchColumnType,
Expand Down Expand Up @@ -1343,7 +1344,7 @@ function createAndOpenSearchTransactionThread(item: TransactionListItemType, has
}

if (shouldNavigate) {
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: transactionThreadReport?.reportID, backTo}));
openSearchReport(transactionThreadReport?.reportID, backTo);
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Search/SearchUIUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import openSearchReport from '@components/Search/openSearchReport';
import ChatListItem from '@components/SelectionListWithSections/ChatListItem';
import TransactionGroupListItem from '@components/SelectionListWithSections/Search/TransactionGroupListItem';
import TransactionListItem from '@components/SelectionListWithSections/Search/TransactionListItem';
Expand All @@ -12,7 +13,6 @@ import type {
TransactionReportGroupListItemType,
TransactionWithdrawalIDGroupListItemType,
} from '@components/SelectionListWithSections/types';
import Navigation from '@navigation/Navigation';
// eslint-disable-next-line no-restricted-syntax
import type * as ReportUserActions from '@userActions/Report';
import {createTransactionThreadReport} from '@userActions/Report';
Expand All @@ -25,7 +25,6 @@ import IntlStore from '@src/languages/IntlStore';
import type {CardFeedForDisplay} from '@src/libs/CardFeedUtils';
import * as SearchUIUtils from '@src/libs/SearchUIUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
import type {Connections} from '@src/types/onyx/Policy';
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
Expand All @@ -45,6 +44,7 @@ jest.mock('@userActions/Search', () => ({
...jest.requireActual<typeof SearchUtils>('@userActions/Search'),
updateSearchResultsWithTransactionThreadReportID: jest.fn(),
}));
jest.mock('@components/Search/openSearchReport', () => jest.fn());

const adminAccountID = 18439984;
const adminEmail = 'admin@policy.com';
Expand Down Expand Up @@ -2876,12 +2876,12 @@ describe('SearchUIUtils', () => {

test('Should not navigate if shouldNavigate = false', () => {
SearchUIUtils.createAndOpenSearchTransactionThread(transactionListItem, hash, backTo, undefined, false);
expect(Navigation.navigate).not.toHaveBeenCalled();
expect(openSearchReport).not.toHaveBeenCalled();
});

test('Should handle navigation if shouldNavigate = true', () => {
SearchUIUtils.createAndOpenSearchTransactionThread(transactionListItem, hash, backTo, undefined, true);
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.SEARCH_REPORT.getRoute({reportID: threadReportID, backTo}));
expect(openSearchReport).toHaveBeenCalledWith(threadReportID, backTo);
});
});

Expand Down