diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 8d337df4feba..91a0ce5da930 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -59,7 +59,7 @@ import * as Environment from '@libs/Environment/Environment'; import * as ErrorUtils from '@libs/ErrorUtils'; import Log from '@libs/Log'; import * as LoginUtils from '@libs/LoginUtils'; -import Navigation from '@libs/Navigation/Navigation'; +import Navigation, {navigationRef} from '@libs/Navigation/Navigation'; import type {NetworkStatus} from '@libs/NetworkConnection'; import LocalNotification from '@libs/Notification/LocalNotification'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; @@ -2530,10 +2530,11 @@ function navigateToMostRecentReport(currentReport: OnyxEntry) { const isChatThread = ReportUtils.isChatThread(currentReport); if (lastAccessedReportID) { const lastAccessedReportRoute = ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID ?? ''); + // We are using index 1 here because on index 0, we'll always have the bottomTabNavigator. + const isFirstRoute = navigationRef?.current?.getState()?.index === 1; // If it is not a chat thread we should call Navigation.goBack to pop the current route first before navigating to last accessed report. - if (!isChatThread) { - // Fallback to the lastAccessedReportID route, if this is first route in the navigator - Navigation.goBack(lastAccessedReportRoute); + if (!isChatThread && !isFirstRoute) { + Navigation.goBack(); } Navigation.navigate(lastAccessedReportRoute, CONST.NAVIGATION.TYPE.FORCED_UP); } else { diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 31fdcfcab6db..8620d8d4866e 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -517,7 +517,6 @@ function ReportScreen({ const didReportClose = wasReportRemoved && prevReport.statusNum === CONST.REPORT.STATUS_NUM.OPEN && report.statusNum === CONST.REPORT.STATUS_NUM.CLOSED; const isTopLevelPolicyRoomWithNoStatus = !report.statusNum && !prevReport.parentReportID && prevReport.chatType === CONST.REPORT.CHAT_TYPE.POLICY_ROOM; const isClosedTopLevelPolicyRoom = wasReportRemoved && prevReport.statusNum === CONST.REPORT.STATUS_NUM.OPEN && isTopLevelPolicyRoomWithNoStatus; - // Navigate to the Concierge chat if the room was removed from another device (e.g. user leaving a room or removed from a room) if ( // non-optimistic case @@ -526,6 +525,10 @@ function ReportScreen({ isRemovalExpectedForReportType || isClosedTopLevelPolicyRoom ) { + // Early return if the report we're passing isn't in a focused state. We only want to navigate to Concierge if the user leaves the room from another device or gets removed from the room while the report is in a focused state. + if (!isFocused) { + return; + } Navigation.dismissModal(); if (Navigation.getTopmostReportId() === prevOnyxReportID) { Navigation.setShouldPopAllStateOnUP(); @@ -540,6 +543,7 @@ function ReportScreen({ Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(prevReport.parentReportID)); return; } + Report.navigateToConciergeChat(); return; } @@ -567,6 +571,7 @@ function ReportScreen({ prevReport.chatType, prevReport, reportIDFromRoute, + isFocused, ]); useEffect(() => {