Skip to content

Commit

Permalink
Merge pull request #42504 from tsa321/endless_loHeader
Browse files Browse the repository at this point in the history
Fix endless loading thread report header
  • Loading branch information
luacmartins authored Jun 6, 2024
2 parents 05bcf8a + 969e400 commit 679a82e
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ let currentUserPrivateDomain: string | undefined;
let currentUserAccountID: number | undefined;
let isAnonymousUser = false;

// This cache is used to save parse result of report action html message into text
// to prevent unnecessary parsing when the report action is not changed/modified.
// Example case: when we need to get a report name of a thread which is dependent on a report action message.
const parsedReportActionMessageCache: Record<string, string> = {};

const defaultAvatarBuildingIconTestID = 'SvgDefaultAvatarBuilding Icon';

Onyx.connect({
Expand Down Expand Up @@ -3148,10 +3153,53 @@ function getInvoicePayerName(report: OnyxEntry<Report>): string {
return getPolicyName(report, false, allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiver?.policyID}`]);
}

/**
* Parse html of reportAction into text
*/
function parseReportActionHtmlToText(reportAction: OnyxEntry<ReportAction>, reportID: string, childReportID?: string): string {
if (!reportAction) {
return '';
}
const key = `${reportID}_${reportAction.reportActionID}_${reportAction.lastModified}`;
const cachedText = parsedReportActionMessageCache[key];
if (cachedText !== undefined) {
return cachedText;
}

const {html, text} = reportAction?.message?.[0] ?? {};

if (!html) {
return text ?? '';
}

const mentionReportRegex = /<mention-report reportID="(\d+)" *\/>/gi;
const matches = html.matchAll(mentionReportRegex);

const reportIDToName: Record<string, string> = {};
for (const match of matches) {
if (match[1] !== childReportID) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
reportIDToName[match[1]] = getReportName(getReport(match[1])) ?? '';
}
}

const mentionUserRegex = /<mention-user accountID="(\d+)" *\/>/gi;
const accountIDToName: Record<string, string> = {};
const accountIDs = Array.from(html.matchAll(mentionUserRegex), (mention) => Number(mention[1]));
const logins = PersonalDetailsUtils.getLoginsByAccountIDs(accountIDs);
accountIDs.forEach((id, index) => (accountIDToName[id] = logins[index]));

const parser = new ExpensiMark();
const textMessage = Str.removeSMSDomain(parser.htmlToText(html, {reportIDToName, accountIDToName}));
parsedReportActionMessageCache[key] = textMessage;

return textMessage;
}

/**
* Get the report action message for a report action.
*/
function getReportActionMessage(reportAction: ReportAction | EmptyObject, parentReportID?: string) {
function getReportActionMessage(reportAction: ReportAction | EmptyObject, reportID?: string, childReportID?: string) {
if (isEmptyObject(reportAction)) {
return '';
}
Expand All @@ -3165,9 +3213,10 @@ function getReportActionMessage(reportAction: ReportAction | EmptyObject, parent
return ReportActionsUtils.getReportActionMessageText(reportAction);
}
if (ReportActionsUtils.isReimbursementQueuedAction(reportAction)) {
return getReimbursementQueuedActionMessage(reportAction, getReport(parentReportID), false);
return getReimbursementQueuedActionMessage(reportAction, getReport(reportID), false);
}
return Str.removeSMSDomain(reportAction?.message?.[0]?.text ?? '');

return parseReportActionHtmlToText(reportAction, reportID ?? '', childReportID);
}

/**
Expand Down Expand Up @@ -3213,7 +3262,7 @@ function getReportName(report: OnyxEntry<Report>, policy: OnyxEntry<Policy> = nu
}

const isAttachment = ReportActionsUtils.isReportActionAttachment(!isEmptyObject(parentReportAction) ? parentReportAction : null);
const parentReportActionMessage = getReportActionMessage(parentReportAction, report?.parentReportID).replace(/(\r\n|\n|\r)/gm, ' ');
const parentReportActionMessage = getReportActionMessage(parentReportAction, report?.parentReportID, report?.reportID ?? '').replace(/(\r\n|\n|\r)/gm, ' ');
if (isAttachment && parentReportActionMessage) {
return `[${Localize.translateLocal('common.attachment')}]`;
}
Expand Down Expand Up @@ -7055,6 +7104,7 @@ export {
navigateToDetailsPage,
navigateToPrivateNotes,
parseReportRouteParams,
parseReportActionHtmlToText,
reportFieldsEnabled,
requiresAttentionFromCurrentUser,
shouldAutoFocusOnKeyPress,
Expand Down

0 comments on commit 679a82e

Please sign in to comment.