Skip to content
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {cloneDeep} from 'lodash';
import lodashGet from 'lodash/get';
import React from 'react';
import {TNodeChildrenRenderer} from 'react-native-render-html';
Expand All @@ -16,6 +17,7 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import personalDetailsPropType from '@pages/personalDetailsPropType';
import CONST from '@src/CONST';
import * as LoginUtils from '@src/libs/LoginUtils';
import ROUTES from '@src/ROUTES';
import htmlRendererPropTypes from './htmlRendererPropTypes';

Expand All @@ -31,21 +33,41 @@ function MentionUserRenderer(props) {
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const defaultRendererProps = _.omit(props, ['TDefaultRenderer', 'style']);
const htmlAttribAccountID = lodashGet(props.tnode.attributes, 'accountid');
const htmlAttributeAccountID = lodashGet(props.tnode.attributes, 'accountid');
const personalDetails = usePersonalDetails() || CONST.EMPTY_OBJECT;

let accountID;
let displayNameOrLogin;
let navigationRoute;
const tnode = cloneDeep(props.tnode);

if (!_.isEmpty(htmlAttribAccountID)) {
const user = lodashGet(personalDetails, htmlAttribAccountID);
accountID = parseInt(htmlAttribAccountID, 10);
const getMentionDisplayText = (displayText, userAccountID, 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 (!LoginUtils.areEmailsFromSamePrivateDomain(displayText, props.currentUserPersonalDetails.login)) {
return displayText;
}

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

if (!_.isEmpty(htmlAttributeAccountID)) {
const user = lodashGet(personalDetails, htmlAttributeAccountID);
accountID = parseInt(htmlAttributeAccountID, 10);
displayNameOrLogin = LocalePhoneNumber.formatPhoneNumber(lodashGet(user, 'login', '')) || lodashGet(user, 'displayName', '') || translate('common.hidden');
navigationRoute = ROUTES.PROFILE.getRoute(htmlAttribAccountID);
} else if (!_.isEmpty(props.tnode.data)) {
displayNameOrLogin = getMentionDisplayText(displayNameOrLogin, htmlAttributeAccountID, lodashGet(user, 'login', ''));
navigationRoute = ROUTES.PROFILE.getRoute(htmlAttributeAccountID);
} else if (!_.isEmpty(tnode.data)) {
// We need to remove the LTR unicode and leading @ from data as it is not part of the login
displayNameOrLogin = props.tnode.data.replace(CONST.UNICODE.LTR, '').slice(1);
Copy link
Contributor

@sobitneupane sobitneupane Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will probably cause issue. We are passing displayNameOrLogin to tooltip and and is also used in navigationRoute. As mentioned in the comment above, we need to remove the LTR unicode and leading @ from data as it is not part of the login.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. I updated the PR

displayNameOrLogin = tnode.data.replace(CONST.UNICODE.LTR, '').slice(1);
// We need to replace tnode.data here because we will pass it to TNodeChildrenRenderer below
tnode.data = tnode.data.replace(displayNameOrLogin, getMentionDisplayText(displayNameOrLogin, htmlAttributeAccountID));

accountID = _.first(PersonalDetailsUtils.getAccountIDsByLogins([displayNameOrLogin]));
navigationRoute = ROUTES.DETAILS.getRoute(displayNameOrLogin);
Expand Down Expand Up @@ -83,7 +105,7 @@ function MentionUserRenderer(props) {
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
{!_.isEmpty(htmlAttribAccountID) ? `@${displayNameOrLogin}` : <TNodeChildrenRenderer tnode={props.tnode} />}
{!_.isEmpty(htmlAttributeAccountID) ? `@${displayNameOrLogin}` : <TNodeChildrenRenderer tnode={tnode} />}
</Text>
</UserDetailsTooltip>
</Text>
Expand Down
12 changes: 11 additions & 1 deletion src/libs/LoginUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ function getPhoneLogin(partnerUserID: string): string {
return appendCountryCode(getPhoneNumberWithoutSpecialChars(partnerUserID));
}

export {getPhoneNumberWithoutSpecialChars, appendCountryCode, isEmailPublicDomain, validateNumber, getPhoneLogin};
/**
* Check whether 2 emails have the same private domain
*/
function areEmailsFromSamePrivateDomain(email1: string, email2: string): boolean {
if (isEmailPublicDomain(email1) || isEmailPublicDomain(email2)) {
return false;
}
return Str.extractEmailDomain(email1).toLowerCase() === Str.extractEmailDomain(email2).toLowerCase();
}

export {getPhoneNumberWithoutSpecialChars, appendCountryCode, isEmailPublicDomain, validateNumber, getPhoneLogin, areEmailsFromSamePrivateDomain};