diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 9a42af23accb..7aa6b02c3170 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -8569,11 +8569,25 @@ function isMoneyRequestReportPendingDeletion(reportOrID: OnyxEntry | str } function navigateToLinkedReportAction(ancestor: Ancestor, isInNarrowPaneModal: boolean, canUserPerformWrite: boolean | undefined, isOffline: boolean) { + const parentReport = getReportOrDraftReport(ancestor.report.parentReportID); + const parentReportAction = getReportAction(ancestor.report.parentReportID, ancestor.report.parentReportActionID); + + let newAncestor = ancestor; + // If `parentReport` is an IOU or Expense report, navigate directly to `parentReport`, + // preventing redundant navigation when threading back to the parent chat thread + if (parentReport && parentReportAction && (isIOUReport(parentReport) || isExpenseReport(parentReport))) { + newAncestor = { + ...ancestor, + report: parentReport, + reportAction: parentReportAction, + }; + } + if (isInNarrowPaneModal) { Navigation.navigate( ROUTES.SEARCH_REPORT.getRoute({ - reportID: ancestor.report.reportID, - reportActionID: ancestor.reportAction.reportActionID, + reportID: newAncestor.report.reportID, + reportActionID: newAncestor.reportAction.reportActionID, backTo: SCREENS.SEARCH.REPORT_RHP, }), ); @@ -8581,13 +8595,13 @@ function navigateToLinkedReportAction(ancestor: Ancestor, isInNarrowPaneModal: b } // Pop the thread report screen before navigating to the chat report. - Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(ancestor.report.reportID)); + Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(newAncestor.report.reportID)); - const isVisibleAction = shouldReportActionBeVisible(ancestor.reportAction, ancestor.reportAction.reportActionID, canUserPerformWrite); + const isVisibleAction = shouldReportActionBeVisible(newAncestor.reportAction, newAncestor.reportAction.reportActionID, canUserPerformWrite); if (isVisibleAction && !isOffline) { // Pop the chat report screen before navigating to the linked report action. - Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(ancestor.report.reportID, ancestor.reportAction.reportActionID)); + Navigation.goBack(ROUTES.REPORT_WITH_ID.getRoute(newAncestor.report.reportID, newAncestor.reportAction.reportActionID)); } } diff --git a/src/pages/home/HeaderView.tsx b/src/pages/home/HeaderView.tsx index 936ed8514289..c504eee9c1fa 100644 --- a/src/pages/home/HeaderView.tsx +++ b/src/pages/home/HeaderView.tsx @@ -54,8 +54,10 @@ import { isChatUsedForOnboarding as isChatUsedForOnboardingReportUtils, isCurrentUserSubmitter, isDeprecatedGroupDM, + isExpenseReport, isExpenseRequest, isGroupChat as isGroupChatReportUtils, + isIOUReport, isOpenTaskReport, isPolicyExpenseChat as isPolicyExpenseChatReportUtils, isSelfDM as isSelfDMReportUtils, @@ -136,7 +138,11 @@ function HeaderView({report, parentReportAction, onNavigationMenuButtonClicked, const isChatRoom = isChatRoomReportUtils(report); const isPolicyExpenseChat = isPolicyExpenseChatReportUtils(report); const isTaskReport = isTaskReportReportUtils(report); - const reportHeaderData = !isTaskReport && !isChatThread && report?.parentReportID ? parentReport : report; + const [parentOfParentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.parentReportID}`, {canBeMissing: true}); + const reportHeaderData = + ((!isTaskReport && !isChatThread) || (parentOfParentReport && (isIOUReport(parentOfParentReport) || isExpenseReport(parentOfParentReport)))) && report?.parentReportID + ? parentReport + : report; // Use sorted display names for the title for group chats on native small screen widths const title = getReportName(reportHeaderData, policy, parentReportAction, personalDetails, invoiceReceiverPolicy); const subtitle = getChatRoomSubtitle(reportHeaderData); @@ -302,8 +308,8 @@ function HeaderView({report, parentReportAction, onNavigationMenuButtonClicked, {!isEmptyObject(parentNavigationSubtitleData) && ( )}