diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 09e3bb27a95b..d85ee68da468 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -6506,23 +6506,33 @@ function getAllAncestorReportActions(report: Report | null | undefined): Ancesto let parentReportID = report.parentReportID; let parentReportActionID = report.parentReportActionID; + // Store the child of parent report + let currentReport = report; + while (parentReportID) { const parentReport = getReport(parentReportID); const parentReportAction = ReportActionsUtils.getReportAction(parentReportID, parentReportActionID ?? '0'); - if (!parentReport || !parentReportAction || ReportActionsUtils.isTransactionThread(parentReportAction) || ReportActionsUtils.isReportPreviewAction(parentReportAction)) { + if (!parentReportAction || ReportActionsUtils.isTransactionThread(parentReportAction) || ReportActionsUtils.isReportPreviewAction(parentReportAction)) { break; } const isParentReportActionUnread = ReportActionsUtils.isCurrentActionUnread(parentReport ?? {}, parentReportAction); allAncestors.push({ - report: parentReport, + report: currentReport, reportAction: parentReportAction, shouldDisplayNewMarker: isParentReportActionUnread, }); + if (!parentReport) { + break; + } + parentReportID = parentReport?.parentReportID; parentReportActionID = parentReport?.parentReportActionID; + if (!isEmptyObject(parentReport)) { + currentReport = parentReport; + } } return allAncestors.reverse(); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index 1e822f9d2cbb..81d8bd8357f0 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -812,10 +812,10 @@ describe('ReportUtils', () => { it('should return correctly all ancestors of a thread report', () => { const resultAncestors = [ - {report: reports[0], reportAction: reportActions[0], shouldDisplayNewMarker: false}, - {report: reports[1], reportAction: reportActions[1], shouldDisplayNewMarker: false}, - {report: reports[2], reportAction: reportActions[2], shouldDisplayNewMarker: false}, - {report: reports[3], reportAction: reportActions[3], shouldDisplayNewMarker: false}, + {report: reports[1], reportAction: reportActions[0], shouldDisplayNewMarker: false}, + {report: reports[2], reportAction: reportActions[1], shouldDisplayNewMarker: false}, + {report: reports[3], reportAction: reportActions[2], shouldDisplayNewMarker: false}, + {report: reports[4], reportAction: reportActions[3], shouldDisplayNewMarker: false}, ]; expect(ReportUtils.getAllAncestorReportActions(reports[4])).toEqual(resultAncestors);