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: mentions are not shown in issued card page, add/remove user page #55699

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 2 additions & 9 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Locale, OnyxInputOrEntry, PersonalDetailsList, PrivatePersonalDetails} from '@src/types/onyx';
import type {Locale, OnyxInputOrEntry, PrivatePersonalDetails} from '@src/types/onyx';
import type {JoinWorkspaceResolution, OriginalMessageChangeLog, OriginalMessageExportIntegration} from '@src/types/onyx/OriginalMessage';
import type Report from '@src/types/onyx/Report';
import type ReportAction from '@src/types/onyx/ReportAction';
Expand Down Expand Up @@ -1834,13 +1834,11 @@ function getCardIssuedMessage({
shouldRenderHTML = false,
policyID = '-1',
shouldDisplayLinkToCard = false,
personalDetails,
}: {
reportAction: OnyxEntry<ReportAction>;
shouldRenderHTML?: boolean;
policyID?: string;
shouldDisplayLinkToCard?: boolean;
personalDetails?: Partial<PersonalDetailsList>;
}) {
const cardIssuedActionOriginalMessage = isActionOfType(
reportAction,
Expand All @@ -1854,13 +1852,8 @@ function getCardIssuedMessage({

const assigneeAccountID = cardIssuedActionOriginalMessage?.assigneeAccountID ?? CONST.DEFAULT_NUMBER_ID;
const cardID = cardIssuedActionOriginalMessage?.cardID ?? CONST.DEFAULT_NUMBER_ID;
const assigneeDetails = getPersonalDetailsByIDs({
accountIDs: [assigneeAccountID],
currentUserAccountID: currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID,
personalDetailsParam: personalDetails,
}).at(0);
const isPolicyAdmin = isPolicyAdminPolicyUtils(getPolicy(policyID));
const assignee = shouldRenderHTML ? `<mention-user accountID="${assigneeAccountID}"/>` : assigneeDetails?.firstName ?? assigneeDetails?.login ?? '';
const assignee = shouldRenderHTML ? `<mention-user accountID="${assigneeAccountID}"/>` : Parser.htmlToText(`<mention-user accountID="${assigneeAccountID}"/>`);
const navigateRoute = isPolicyAdmin ? ROUTES.EXPENSIFY_CARD_DETAILS.getRoute(policyID, String(cardID)) : ROUTES.SETTINGS_DOMAINCARD_DETAIL.getRoute(String(cardID));
const expensifyCardLink =
shouldRenderHTML && shouldDisplayLinkToCard
Expand Down
31 changes: 26 additions & 5 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import localeCompare from './LocaleCompare';
import {formatPhoneNumber} from './LocalePhoneNumber';
import {translateLocal} from './Localize';
import Log from './Log';
import {isEmailPublicDomain} from './LoginUtils';
import {areEmailsFromSamePrivateDomain, isEmailPublicDomain} from './LoginUtils';
// eslint-disable-next-line import/no-cycle
import ModifiedExpenseMessage from './ModifiedExpenseMessage';
import {linkingConfig} from './Navigation/linkingConfig';
Expand Down Expand Up @@ -4190,6 +4190,22 @@ function getInvoicePayerName(report: OnyxEntry<Report>, invoiceReceiverPolicy?:
return getPolicyName({report, policy: invoiceReceiverPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiver?.policyID}`]});
}

const getShortMentionIfFound = (displayText: string, userAccountID: string, userLogin = '') => {
daledah marked this conversation as resolved.
Show resolved Hide resolved
// If the userAccountID does not exist, this is an email-based mention so the displayText must be an email.
// If the userAccountID exists but userLogin is different from displayText, this means the displayText is either user display name, Hidden, or phone number, in which case we should return it as is.
if (userAccountID && userLogin !== displayText) {
return displayText;
}

// If the emails are not in the same private domain, we also return the displayText
if (!areEmailsFromSamePrivateDomain(displayText, currentUserPersonalDetails?.login ?? '')) {
return displayText;
}

// Otherwise, the emails must be of the same private domain, so we should remove the domain part
return displayText.split('@').at(0);
};

/**
* Parse html of reportAction into text
*/
Expand All @@ -4209,7 +4225,7 @@ function parseReportActionHtmlToText(reportAction: OnyxEntry<ReportAction>, repo
return text ?? '';
}

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

const reportIDToName: Record<string, string> = {};
Expand All @@ -4220,11 +4236,16 @@ function parseReportActionHtmlToText(reportAction: OnyxEntry<ReportAction>, repo
}
}

const mentionUserRegex = /<mention-user accountID="(\d+)" *\/>/gi;
const mentionUserRegex = /(?:<mention-user accountID="?(\d+)"?(?: *\/>|><\/mention-user>))|(?:<mention-user>(.*?)<\/mention-user>)/gi;
const accountIDToName: Record<string, string> = {};
daledah marked this conversation as resolved.
Show resolved Hide resolved
const accountIDs = Array.from(html.matchAll(mentionUserRegex), (mention) => Number(mention[1]));
const logins = getLoginsByAccountIDs(accountIDs);
accountIDs.forEach((id, index) => (accountIDToName[id] = logins.at(index) ?? ''));
accountIDs.forEach((id, index) => {
const login = logins.at(index);
const user = allPersonalDetails?.[id];
const displayName = formatPhoneNumber(login ?? '') || getDisplayNameOrDefault(user);
accountIDToName[id] = getShortMentionIfFound(displayName, id.toString(), login) ?? '';
});

const textMessage = Str.removeSMSDomain(Parser.htmlToText(html, {reportIDToName, accountIDToName}));
parsedReportActionMessageCache[key] = textMessage;
Expand Down Expand Up @@ -4470,7 +4491,7 @@ function getReportNameInternal({
}

if (isCardIssuedAction(parentReportAction)) {
return getCardIssuedMessage({reportAction: parentReportAction, personalDetails});
return getCardIssuedMessage({reportAction: parentReportAction});
}
return reportActionMessage;
}
Expand Down
Loading