diff --git a/src/ROUTES.ts b/src/ROUTES.ts index b839de81ab24..46cff27c6e03 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -365,7 +365,7 @@ const ROUTES = { REPORT: 'r', REPORT_WITH_ID: { route: 'r/:reportID?/:reportActionID?', - getRoute: (reportID: string | undefined, reportActionID?: string, referrer?: string, moneyRequestReportActionID?: string, transactionID?: string) => { + getRoute: (reportID: string | undefined, reportActionID?: string, referrer?: string, moneyRequestReportActionID?: string, transactionID?: string, backTo?: string) => { if (!reportID) { Log.warn('Invalid reportID is used to build the REPORT_WITH_ID route'); } @@ -384,7 +384,7 @@ const ROUTES = { const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; - return `${baseRoute}${queryString}` as const; + return getUrlWithBackToParam(`${baseRoute}${queryString}` as const, backTo); }, }, REPORT_AVATAR: { diff --git a/src/libs/Navigation/Navigation.ts b/src/libs/Navigation/Navigation.ts index 4fbfbff6ae33..b82f64e037bb 100644 --- a/src/libs/Navigation/Navigation.ts +++ b/src/libs/Navigation/Navigation.ts @@ -475,20 +475,25 @@ type NavigateToReportWithPolicyCheckPayload = MergeExclusive<{report: OnyxEntry< reportActionID?: string; referrer?: string; policyIDToCheck?: string; + backTo?: string; }; /** * Navigates to a report passed as a param (as an id or report object) and checks whether the target object belongs to the currently selected workspace. * If not, the current workspace is set to global. */ -function navigateToReportWithPolicyCheck({report, reportID, reportActionID, referrer, policyIDToCheck}: NavigateToReportWithPolicyCheckPayload, forceReplace = false, ref = navigationRef) { +function navigateToReportWithPolicyCheck( + {report, reportID, reportActionID, referrer, policyIDToCheck, backTo}: NavigateToReportWithPolicyCheckPayload, + forceReplace = false, + ref = navigationRef, +) { const targetReport = reportID ? {reportID, ...allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]} : report; const policyID = policyIDToCheck ?? getPolicyIDFromState(navigationRef.getRootState() as State); const policyMemberAccountIDs = getPolicyEmployeeAccountIDs(policyID); const shouldOpenAllWorkspace = isEmptyObject(targetReport) ? true : !doesReportBelongToWorkspace(targetReport, policyMemberAccountIDs, policyID); if ((shouldOpenAllWorkspace && !policyID) || !shouldOpenAllWorkspace) { - linkTo(ref.current, ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID, reportActionID, referrer), {forceReplace: !!forceReplace}); + linkTo(ref.current, ROUTES.REPORT_WITH_ID.getRoute(targetReport?.reportID, reportActionID, referrer, undefined, undefined, backTo), {forceReplace: !!forceReplace}); return; } @@ -515,6 +520,9 @@ function navigateToReportWithPolicyCheck({report, reportID, reportActionID, refe return; } + if (backTo) { + params.backTo = backTo; + } ref.dispatch( StackActions.push(NAVIGATORS.REPORTS_SPLIT_NAVIGATOR, { policyID: undefined, diff --git a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts index 418a2afeee07..a33328944f8f 100644 --- a/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts +++ b/src/libs/Notification/PushNotification/subscribeToPushNotifications.ts @@ -143,7 +143,7 @@ function navigateToReport({reportID}: PushNotificationData): Promise { } Log.info('[PushNotification] onSelected() - Navigation is ready. Navigating...', false, {reportID}); - Navigation.navigateToReportWithPolicyCheck({reportID: String(reportID), policyIDToCheck: policyID}); + Navigation.navigateToReportWithPolicyCheck({reportID: String(reportID), policyIDToCheck: policyID, backTo: Navigation.getActiveRoute()}); updateLastVisitedPath(ROUTES.REPORT_WITH_ID.getRoute(String(reportID))); } catch (error) { let errorMessage = String(error); diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 987113414537..79aea8f32b17 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -80,6 +80,7 @@ import { } from '@userActions/Report'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; @@ -318,6 +319,10 @@ function ReportScreen({route, navigation}: ReportScreenProps) { Navigation.dismissModal(); return; } + if (backTo) { + Navigation.goBack(backTo as Route, {shouldPopToTop: true}); + return; + } Navigation.goBack(undefined, {shouldPopToTop: true}); }, [isInNarrowPaneModal, backTo]);