Skip to content

Commit

Permalink
Merge pull request #47156 from callstack-internal/fix/add-cache-to-ge…
Browse files Browse the repository at this point in the history
…tReportName

Add cache to getReportName
  • Loading branch information
neil-marcellini authored Aug 13, 2024
2 parents 68b8a10 + 0766fc8 commit 5157ccc
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3582,6 +3582,13 @@ function getInvoicesChatName(report: OnyxEntry<Report>, receiverPolicy: OnyxEntr
return getPolicyName(report, false, invoiceReceiverPolicy);
}

const reportNameCache = new Map<string, {lastVisibleActionCreated: string; reportName: string}>();

/**
* Get a cache key for the report name.
*/
const getCacheKey = (report: OnyxEntry<Report>): string => `${report?.reportID}-${report?.lastVisibleActionCreated}-${report?.reportName}`;

/**
* Get the title for a report.
*/
Expand All @@ -3592,6 +3599,17 @@ function getReportName(
personalDetails?: Partial<PersonalDetailsList>,
invoiceReceiverPolicy?: OnyxEntry<Policy>,
): string {
const reportID = report?.reportID;
const cacheKey = getCacheKey(report);

if (reportID) {
const reportNameFromCache = reportNameCache.get(cacheKey);

if (reportNameFromCache && reportNameFromCache.reportName === report?.reportName) {
return reportNameFromCache.reportName;
}
}

let formattedName: string | undefined;
const parentReportAction = parentReportActionParam ?? ReportActionsUtils.getParentReportAction(report);
const parentReportActionMessage = ReportActionsUtils.getReportActionMessage(parentReportAction);
Expand Down Expand Up @@ -3683,6 +3701,10 @@ function getReportName(
}

if (formattedName) {
if (reportID) {
reportNameCache.set(cacheKey, {lastVisibleActionCreated: report?.lastVisibleActionCreated ?? '', reportName: formattedName});
}

return formatReportLastMessageText(formattedName);
}

Expand All @@ -3695,7 +3717,14 @@ function getReportName(
}
});
const isMultipleParticipantReport = participantsWithoutCurrentUser.length > 1;
return participantsWithoutCurrentUser.map((accountID) => getDisplayNameForParticipant(accountID, isMultipleParticipantReport, true, false, personalDetails)).join(', ');
const participantNames = participantsWithoutCurrentUser.map((accountID) => getDisplayNameForParticipant(accountID, isMultipleParticipantReport, true, false, personalDetails)).join(', ');
formattedName = participantNames;

if (reportID) {
reportNameCache.set(cacheKey, {lastVisibleActionCreated: report?.lastVisibleActionCreated ?? '', reportName: formattedName});
}

return formattedName;
}

/**
Expand Down

0 comments on commit 5157ccc

Please sign in to comment.