Skip to content

Commit

Permalink
fix: remove unnecessary regex, DRY code
Browse files Browse the repository at this point in the history
  • Loading branch information
daledah committed Feb 11, 2025
1 parent ed76cb9 commit 83c8a52
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentU
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import {formatPhoneNumber} from '@libs/LocalePhoneNumber';
import {areEmailsFromSamePrivateDomain} from '@libs/LoginUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getAccountIDsByLogins, getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import {getAccountIDsByLogins, getDisplayNameOrDefault, getShortMentionIfFound} from '@libs/PersonalDetailsUtils';
import {isArchivedNonExpenseReport} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -41,33 +40,20 @@ function MentionUserRenderer({style, tnode, TDefaultRenderer, currentUserPersona

const tnodeClone = cloneDeep(tnode);

const getShortMentionIfFound = (displayText: string, userAccountID: string, userLogin = '') => {
// 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);
};

if (!isEmpty(htmlAttribAccountID) && personalDetails?.[htmlAttribAccountID]) {
const user = personalDetails[htmlAttribAccountID];
accountID = parseInt(htmlAttribAccountID, 10);
mentionDisplayText = formatPhoneNumber(user?.login ?? '') || getDisplayNameOrDefault(user);
mentionDisplayText = getShortMentionIfFound(mentionDisplayText, htmlAttributeAccountID, user?.login ?? '') ?? '';
mentionDisplayText = getShortMentionIfFound(mentionDisplayText, htmlAttributeAccountID, currentUserPersonalDetails, user?.login ?? '') ?? '';
navigationRoute = ROUTES.PROFILE.getRoute(accountID, Navigation.getReportRHPActiveRoute());
} else if ('data' in tnodeClone && !isEmptyObject(tnodeClone.data)) {
// We need to remove the LTR unicode and leading @ from data as it is not part of the login
mentionDisplayText = tnodeClone.data.replace(CONST.UNICODE.LTR, '').slice(1);
// We need to replace tnode.data here because we will pass it to TNodeChildrenRenderer below
asMutable(tnodeClone).data = tnodeClone.data.replace(mentionDisplayText, Str.removeSMSDomain(getShortMentionIfFound(mentionDisplayText, htmlAttributeAccountID) ?? ''));
asMutable(tnodeClone).data = tnodeClone.data.replace(
mentionDisplayText,
Str.removeSMSDomain(getShortMentionIfFound(mentionDisplayText, htmlAttributeAccountID, currentUserPersonalDetails) ?? ''),
);

accountID = getAccountIDsByLogins([mentionDisplayText])?.at(0) ?? -1;
navigationRoute = ROUTES.PROFILE.getRoute(accountID, Navigation.getReportRHPActiveRoute(), mentionDisplayText);
Expand Down
18 changes: 18 additions & 0 deletions src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {OnyxData} from '@src/types/onyx/Request';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as LocalePhoneNumber from './LocalePhoneNumber';

Check failure on line 10 in src/libs/PersonalDetailsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports are not allowed. Use named imports instead. Example: import { method } from "./libs/module"
import * as Localize from './Localize';

Check failure on line 11 in src/libs/PersonalDetailsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports are not allowed. Use named imports instead. Example: import { method } from "./libs/module"
import {areEmailsFromSamePrivateDomain} from './LoginUtils';
import * as UserUtils from './UserUtils';

Check failure on line 13 in src/libs/PersonalDetailsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports are not allowed. Use named imports instead. Example: import { method } from "./libs/module"

type FirstAndLastName = {
Expand Down Expand Up @@ -370,6 +371,22 @@ function getUserNameByEmail(email: string, nameToDisplay: 'firstName' | 'display
return email;
}

const getShortMentionIfFound = (displayText: string, userAccountID: string, currentUserPersonalDetails: OnyxEntry<PersonalDetails>, userLogin = '') => {
// 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);
};

export {
isPersonalDetailsEmpty,
getDisplayNameOrDefault,
Expand All @@ -388,4 +405,5 @@ export {
getNewAccountIDsAndLogins,
getPersonalDetailsLength,
getUserNameByEmail,
getShortMentionIfFound,
};
32 changes: 12 additions & 20 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,23 @@ import localeCompare from './LocaleCompare';
import {formatPhoneNumber} from './LocalePhoneNumber';
import {translateLocal} from './Localize';
import Log from './Log';
import {areEmailsFromSamePrivateDomain, isEmailPublicDomain} from './LoginUtils';
import {isEmailPublicDomain} from './LoginUtils';
// eslint-disable-next-line import/no-cycle
import ModifiedExpenseMessage from './ModifiedExpenseMessage';
import {linkingConfig} from './Navigation/linkingConfig';
import Navigation from './Navigation/Navigation';
import {rand64} from './NumberUtils';
import Parser from './Parser';
import Permissions from './Permissions';
import {getAccountIDsByLogins, getDisplayNameOrDefault, getEffectiveDisplayName, getLoginsByAccountIDs, getPersonalDetailByEmail, getPersonalDetailsByIDs} from './PersonalDetailsUtils';
import {
getAccountIDsByLogins,
getDisplayNameOrDefault,
getEffectiveDisplayName,
getLoginsByAccountIDs,
getPersonalDetailByEmail,
getPersonalDetailsByIDs,
getShortMentionIfFound,
} from './PersonalDetailsUtils';
import {addSMSDomainIfPhoneNumber} from './PhoneNumber';
import {
arePaymentsEnabled,
Expand Down Expand Up @@ -4190,22 +4198,6 @@ function getInvoicePayerName(report: OnyxEntry<Report>, invoiceReceiverPolicy?:
return getPolicyName({report, policy: invoiceReceiverPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiver?.policyID}`]});
}

const getShortMentionIfFound = (displayText: string, userAccountID: string, userLogin = '') => {
// 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 Down Expand Up @@ -4236,15 +4228,15 @@ function parseReportActionHtmlToText(reportAction: OnyxEntry<ReportAction>, repo
}
}

const mentionUserRegex = /(?:<mention-user accountID="?(\d+)"?(?: *\/>|><\/mention-user>))|(?:<mention-user>(.*?)<\/mention-user>)/gi;
const mentionUserRegex = /(?:<mention-user accountID="?(\d+)"?(?: *\/>|><\/mention-user>))/gi;
const accountIDToName: Record<string, string> = {};
const accountIDs = Array.from(html.matchAll(mentionUserRegex), (mention) => Number(mention[1]));
const logins = getLoginsByAccountIDs(accountIDs);
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) ?? '';
accountIDToName[id] = getShortMentionIfFound(displayName, id.toString(), currentUserPersonalDetails, login) ?? '';
});

const textMessage = Str.removeSMSDomain(Parser.htmlToText(html, {reportIDToName, accountIDToName}));
Expand Down

0 comments on commit 83c8a52

Please sign in to comment.