diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3fd13aef5e266..c82ab298c8e23 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -6029,6 +6029,40 @@ function getReportName( function getSearchReportName(props: GetReportNameParams): string { const {report, policy} = props; if (isChatThread(report) && policy?.name) { + // Traverse up the parent chain to find the first expense report + // If found, return the expense report name instead of workspace name + let currentParent = getParentReport(report); + const visitedReportIDs = new Set(); + + while (currentParent) { + if (!currentParent.reportID) { + break; + } + if (visitedReportIDs.has(currentParent.reportID)) { + break; + } + visitedReportIDs.add(currentParent.reportID); + + if (isExpenseReport(currentParent)) { + // eslint-disable-next-line @typescript-eslint/no-deprecated + return getReportName( + currentParent, + policy, + props.parentReportActionParam, + props.personalDetails, + props.invoiceReceiverPolicy, + undefined, + props.transactions, + props.isReportArchived, + props.reports, + props.policies, + ); + } + + // Continue traversing up the parent chain + currentParent = getParentReport(currentParent); + } + return policy.name; } // This will be fixed as follow up https://github.com/Expensify/App/pull/75357