@@ -174,6 +174,7 @@ import {
174174import { buildOptimisticSnapshotData , getCurrentSearchQueryJSON } from '@libs/SearchQueryUtils' ;
175175import playSound , { SOUNDS } from '@libs/Sound' ;
176176import { getAmount , getCurrency , getNegatedAmountTransaction , isOnHold , recalculateUnreportedTransactionDetails , shouldClearConvertedAmount } from '@libs/TransactionUtils' ;
177+ import { getSearchParamFromPath } from '@libs/Url' ;
177178import { buildSecureDownloadURL } from '@libs/UrlUtils' ;
178179import Visibility from '@libs/Visibility' ;
179180import { cacheAttachment , removeCachedAttachment } from '@userActions/Attachment' ;
@@ -201,6 +202,7 @@ import type {OnboardingAccounting} from '@src/CONST';
201202import CONST from '@src/CONST' ;
202203import ONYXKEYS from '@src/ONYXKEYS' ;
203204import ROUTES , { DYNAMIC_ROUTES } from '@src/ROUTES' ;
205+ import type { Route } from '@src/ROUTES' ;
204206import INPUT_IDS from '@src/types/form/NewRoomForm' ;
205207import type {
206208 AnyRequest ,
@@ -4701,6 +4703,21 @@ function navigateToMostRecentReport(
47014703 }
47024704}
47034705
4706+ function getSearchThreadLeaveRoute ( report : Report , activeRoute : string ) : Route | undefined {
4707+ if ( ! isSearchTopmostFullScreenRoute ( ) || ! isChatThreadReportUtils ( report ) || ! report . parentReportID || ! report . parentReportActionID ) {
4708+ return undefined ;
4709+ }
4710+
4711+ const parentBackTo = getSearchParamFromPath ( activeRoute , 'backTo' ) ;
4712+ const nestedBackTo = parentBackTo ? getSearchParamFromPath ( parentBackTo , 'backTo' ) : undefined ;
4713+
4714+ return ROUTES . SEARCH_REPORT . getRoute ( {
4715+ reportID : report . parentReportID ,
4716+ reportActionID : report . parentReportActionID ,
4717+ backTo : nestedBackTo ?? parentBackTo ?? undefined ,
4718+ } ) ;
4719+ }
4720+
47044721function getMostRecentReportID ( currentReport : OnyxEntry < Report > , conciergeReportID : string | undefined ) {
47054722 const lastAccessedReportID = findLastAccessedReport ( false , false , currentReport ?. reportID ) ?. reportID ;
47064723 return lastAccessedReportID ?? conciergeReportID ;
@@ -4793,6 +4810,7 @@ function leaveRoom(
47934810) {
47944811 const reportID = report . reportID ;
47954812 const isChatThread = isChatThreadReportUtils ( report ) ;
4813+ const activeRoute = Navigation . getActiveRoute ( ) ;
47964814
47974815 // Pusher's leavingStatus should be sent earlier.
47984816 // Place the broadcast before calling the LeaveRoom API to prevent a race condition
@@ -4887,6 +4905,12 @@ function leaveRoom(
48874905
48884906 API . write ( WRITE_COMMANDS . LEAVE_ROOM , parameters , { optimisticData, successData, failureData} ) ;
48894907
4908+ const searchThreadLeaveRoute = getSearchThreadLeaveRoute ( report , activeRoute ) ;
4909+ if ( searchThreadLeaveRoute ) {
4910+ Navigation . goBack ( searchThreadLeaveRoute , { compareParams : false } ) ;
4911+ return ;
4912+ }
4913+
48904914 // If this is the leave action from a workspace room, simply dismiss the modal, i.e., allow the user to view the room and join again immediately.
48914915 // If this is the leave action from a chat thread (even if the chat thread is in a room), do not allow the user to stay in the thread after leaving.
48924916 if ( isWorkspaceMemberLeavingWorkspaceRoom && ! isChatThread ) {
0 commit comments