Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix endless loading thread report header #42504

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@
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 @@ -3065,20 +3070,64 @@
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 '';
}
if (ReportActionsUtils.isApprovedOrSubmittedReportAction(reportAction)) {
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 All @@ -3090,7 +3139,7 @@
const invoiceReceiverAccountID = isIndividual ? invoiceReceiver.accountID : -1;
const invoiceReceiverPolicyID = isIndividual ? '' : invoiceReceiver?.policyID ?? '';
const isCurrentUserReceiver =
(isIndividual && invoiceReceiverAccountID === currentUserAccountID) || (!isIndividual && PolicyUtils.isPolicyEmployee(invoiceReceiverPolicyID, allPolicies));

Check failure on line 3142 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / typecheck

Object literal may only specify known properties, but 'reportIDToName' does not exist in type 'ExtrasObject'. Did you mean to write 'reportIdToName'?

if (isCurrentUserReceiver) {
return getPolicyName(report);
Expand Down Expand Up @@ -3124,7 +3173,7 @@
}

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 @@ -6746,6 +6795,7 @@
navigateToDetailsPage,
navigateToPrivateNotes,
parseReportRouteParams,
parseReportActionHtmlToText,
reportFieldsEnabled,
requiresAttentionFromCurrentUser,
shouldAutoFocusOnKeyPress,
Expand Down
Loading