diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index b52a9d7b254a..f371268b1311 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -7,7 +7,7 @@ import type {OnyxEntry} from 'react-native-onyx'; import type {Writable} from 'type-fest'; import getIsNarrowLayout from '@libs/getIsNarrowLayout'; import Log from '@libs/Log'; -import {isSplitNavigatorName} from '@libs/NavigationUtils'; +import {isFullScreenName, isSplitNavigatorName} from '@libs/NavigationUtils'; import {shallowCompare} from '@libs/ObjectUtils'; import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils'; import * as ReportConnection from '@libs/ReportConnection'; @@ -494,6 +494,28 @@ function removeScreenFromNavigationState(screen: Screen) { }); } +/** + * The function redirects the user to the report screen, taking into account the currently open tab. + * When a redirect is performed from the Search tab, a report screen will be opened in RHP, otherwise the chat will be displayed in the Inbox tab. + */ +function redirectToReportBasedOnCurrentTab(reportID: string) { + const lastFullScreenRoute = navigationRef + .getRootState() + .routes.filter((route) => isFullScreenName(route.name)) + .at(-1); + + if (!lastFullScreenRoute) { + return; + } + + if (lastFullScreenRoute.name === SCREENS.SEARCH.CENTRAL_PANE) { + navigate(ROUTES.SEARCH_REPORT.getRoute({reportID}), {forceReplace: true}); + return; + } + + dismissModal(reportID); +} + export default { setShouldPopAllStateOnUP, navigate, @@ -519,6 +541,7 @@ export default { navigateToReportWithPolicyCheck, goUp, removeScreenFromNavigationState, + redirectToReportBasedOnCurrentTab, }; export {navigationRef}; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 823e74fafe32..15b11b6a595d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3680,10 +3680,13 @@ function requestMoney( } } - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID); - if (activeReportID) { - Report.notifyNewAction(activeReportID, payeeAccountID); + if (!activeReportID) { + Navigation.dismissModal(); + return; } + + Navigation.redirectToReportBasedOnCurrentTab(activeReportID); + Report.notifyNewAction(activeReportID, payeeAccountID); } function sendInvoice( @@ -3738,7 +3741,7 @@ function sendInvoice( API.write(WRITE_COMMANDS.SEND_INVOICE, parameters, onyxData); if (isSearchTopmostFullScreenRoute()) { - Navigation.dismissModal(); + Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: invoiceRoom.reportID}), {forceReplace: true}); } else { Navigation.dismissModalWithReport(invoiceRoom); } @@ -3931,7 +3934,7 @@ function trackExpense( API.write(WRITE_COMMANDS.TRACK_EXPENSE, parameters, onyxData); } } - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID); + Navigation.redirectToReportBasedOnCurrentTab(activeReportID); if (action === CONST.IOU.ACTION.SHARE) { Navigation.setNavigationActionToMicrotaskQueue(() => Navigation.navigate(ROUTES.ROOM_INVITE.getRoute(activeReportID ?? '-1', CONST.IOU.SHARE.ROLE.ACCOUNTANT))); @@ -4502,7 +4505,7 @@ function splitBill({ API.write(WRITE_COMMANDS.SPLIT_BILL, parameters, onyxData); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : existingSplitChatReportID); + Navigation.redirectToReportBasedOnCurrentTab(existingSplitChatReportID); Report.notifyNewAction(splitData.chatReportID, currentUserAccountID); } @@ -4569,7 +4572,7 @@ function splitBillAndOpenReport({ API.write(WRITE_COMMANDS.SPLIT_BILL_AND_OPEN_REPORT, parameters, onyxData); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : splitData.chatReportID); + Navigation.redirectToReportBasedOnCurrentTab(splitData.chatReportID); Report.notifyNewAction(splitData.chatReportID, currentUserAccountID); } @@ -5138,7 +5141,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA }; API.write(WRITE_COMMANDS.COMPLETE_SPLIT_BILL, parameters, {optimisticData, successData, failureData}); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : chatReportID); + Navigation.redirectToReportBasedOnCurrentTab(chatReportID); Report.notifyNewAction(chatReportID, sessionAccountID); } @@ -5316,7 +5319,7 @@ function createDistanceRequest( API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); const activeReportID = isMoneyRequestReport ? report?.reportID ?? '-1' : parameters.chatReportID; - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : activeReportID); + Navigation.redirectToReportBasedOnCurrentTab(activeReportID); Report.notifyNewAction(activeReportID, userAccountID); } @@ -6720,7 +6723,7 @@ function sendMoneyElsewhere(report: OnyxEntry, amount: number, API.write(WRITE_COMMANDS.SEND_MONEY_ELSEWHERE, params, {optimisticData, successData, failureData}); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : params.chatReportID); + Navigation.redirectToReportBasedOnCurrentTab(params.chatReportID); Report.notifyNewAction(params.chatReportID, managerID); } @@ -6733,7 +6736,7 @@ function sendMoneyWithWallet(report: OnyxEntry, amount: number API.write(WRITE_COMMANDS.SEND_MONEY_WITH_WALLET, params, {optimisticData, successData, failureData}); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : params.chatReportID); + Navigation.redirectToReportBasedOnCurrentTab(params.chatReportID); Report.notifyNewAction(params.chatReportID, managerID); } diff --git a/src/pages/EditReportFieldPage.tsx b/src/pages/EditReportFieldPage.tsx index 55e0207c0244..543049441781 100644 --- a/src/pages/EditReportFieldPage.tsx +++ b/src/pages/EditReportFieldPage.tsx @@ -12,7 +12,6 @@ import ScreenWrapper from '@components/ScreenWrapper'; import useLocalize from '@hooks/useLocalize'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; -import isSearchTopmostFullScreenRoute from '@libs/Navigation/isSearchTopmostFullScreenRoute'; import Navigation from '@libs/Navigation/Navigation'; import type {EditRequestNavigatorParamList} from '@libs/Navigation/types'; import * as ReportUtils from '@libs/ReportUtils'; @@ -69,14 +68,14 @@ function EditReportFieldPage({route}: EditReportFieldPageProps) { goBack(); } else { ReportActions.updateReportField(report.reportID, {...reportField, value: value === '' ? null : value}, reportField); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : report?.reportID); + Navigation.redirectToReportBasedOnCurrentTab(report?.reportID); } }; const handleReportFieldDelete = () => { ReportActions.deleteReportField(report.reportID, reportField); setIsDeleteModalVisible(false); - Navigation.dismissModal(isSearchTopmostFullScreenRoute() ? undefined : report?.reportID); + Navigation.redirectToReportBasedOnCurrentTab(report?.reportID); }; const fieldValue = isReportFieldTitle ? report.reportName ?? '' : reportField.value ?? reportField.defaultValue;