diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 7448ee05d772..470d9f3392d3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -432,6 +432,51 @@ function getChatType(report: OnyxEntry): ValueOf | EmptyObject { + /** + * Using typical string concatenation here due to performance issues + * with template literals. + */ + if (!allReports) { + return {}; + } + + return allReports?.[ONYXKEYS.COLLECTION.REPORT + reportID] ?? {}; +} + +/** + * Returns the parentReport if the given report is a thread. + */ +function getParentReport(report: OnyxEntry): OnyxEntry | EmptyObject { + if (!report?.parentReportID) { + return {}; + } + return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`] ?? {}; +} + +/** + * Returns the root parentReport if the given report is nested. + * Uses recursion to iterate any depth of nested reports. + */ +function getRootParentReport(report: OnyxEntry | undefined | EmptyObject): OnyxEntry | EmptyObject { + if (!report) { + return {}; + } + + // Returns the current report as the root report, because it does not have a parentReportID + if (!report?.parentReportID) { + return report; + } + + const parentReport = getReport(report?.parentReportID); + + // Runs recursion to iterate a parent report + return getRootParentReport(isNotEmptyObject(parentReport) ? parentReport : null); +} + function getPolicy(policyID: string): Policy | EmptyObject { if (!allPolicies || !policyID) { return {}; @@ -461,10 +506,12 @@ function getPolicyName(report: OnyxEntry | undefined | EmptyObject, retu } const finalPolicy = policy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]; + const parentReport = getRootParentReport(report); + // Public rooms send back the policy name with the reportSummary, // since they can also be accessed by people who aren't in the workspace // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const policyName = finalPolicy?.name || report?.policyName || report?.oldPolicyName || noPolicyFound; + const policyName = finalPolicy?.name || report?.policyName || report?.oldPolicyName || parentReport?.oldPolicyName || noPolicyFound; return policyName; } @@ -1019,21 +1066,6 @@ function isOneOnOneChat(report: OnyxEntry): boolean { ); } -/** - * Get the report given a reportID - */ -function getReport(reportID: string | undefined): OnyxEntry | EmptyObject { - /** - * Using typical string concatenation here due to performance issues - * with template literals. - */ - if (!allReports) { - return {}; - } - - return allReports?.[ONYXKEYS.COLLECTION.REPORT + reportID] ?? {}; -} - /** * Get the notification preference given a report */ @@ -2132,36 +2164,6 @@ function getModifiedExpenseOriginalMessage(oldTransaction: OnyxEntry): OnyxEntry | EmptyObject { - if (!report?.parentReportID) { - return {}; - } - return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`] ?? {}; -} - -/** - * Returns the root parentReport if the given report is nested. - * Uses recursion to iterate any depth of nested reports. - */ -function getRootParentReport(report: OnyxEntry): OnyxEntry | EmptyObject { - if (!report) { - return {}; - } - - // Returns the current report as the root report, because it does not have a parentReportID - if (!report?.parentReportID) { - return report; - } - - const parentReport = getReport(report?.parentReportID); - - // Runs recursion to iterate a parent report - return getRootParentReport(isNotEmptyObject(parentReport) ? parentReport : null); -} - /** * Get the title for a report. */