-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
MentionUserRenderer.js
98 lines (88 loc) · 4.51 KB
/
MentionUserRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import lodashGet from 'lodash/get';
import React from 'react';
import {TNodeChildrenRenderer} from 'react-native-render-html';
import _ from 'underscore';
import {usePersonalDetails} from '@components/OnyxProvider';
import {ShowContextMenuContext, showContextMenuForReport} from '@components/ShowContextMenuContext';
import Text from '@components/Text';
import UserDetailsTooltip from '@components/UserDetailsTooltip';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as LocalePhoneNumber from '@libs/LocalePhoneNumber';
import Navigation from '@libs/Navigation/Navigation';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import personalDetailsPropType from '@pages/personalDetailsPropType';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import htmlRendererPropTypes from './htmlRendererPropTypes';
const propTypes = {
...htmlRendererPropTypes,
/** Current user personal details */
currentUserPersonalDetails: personalDetailsPropType.isRequired,
};
function MentionUserRenderer(props) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const defaultRendererProps = _.omit(props, ['TDefaultRenderer', 'style']);
const htmlAttribAccountID = lodashGet(props.tnode.attributes, 'accountid');
const personalDetails = usePersonalDetails() || CONST.EMPTY_OBJECT;
let accountID;
let displayNameOrLogin;
let navigationRoute;
if (!_.isEmpty(htmlAttribAccountID)) {
const user = lodashGet(personalDetails, htmlAttribAccountID);
accountID = parseInt(htmlAttribAccountID, 10);
displayNameOrLogin = LocalePhoneNumber.formatPhoneNumber(lodashGet(user, 'login', '')) || lodashGet(user, 'displayName', '') || translate('common.hidden');
navigationRoute = ROUTES.PROFILE.getRoute(htmlAttribAccountID);
} else if (!_.isEmpty(props.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);
accountID = _.first(PersonalDetailsUtils.getAccountIDsByLogins([displayNameOrLogin]));
navigationRoute = ROUTES.DETAILS.getRoute(displayNameOrLogin);
} else {
// If neither an account ID or email is provided, don't render anything
return null;
}
const isOurMention = accountID === props.currentUserPersonalDetails.accountID;
return (
<ShowContextMenuContext.Consumer>
{({anchor, report, action, checkIfContextMenuActive}) => (
<Text
suppressHighlighting
onLongPress={(event) => showContextMenuForReport(event, anchor, report.reportID, action, checkIfContextMenuActive, ReportUtils.isArchivedRoom(report))}
onPress={(event) => {
event.preventDefault();
Navigation.navigate(navigationRoute);
}}
role={CONST.ROLE.LINK}
accessibilityLabel={`/${navigationRoute}`}
>
<UserDetailsTooltip
accountID={accountID}
fallbackUserDetails={{
displayName: displayNameOrLogin,
}}
>
<Text
style={[styles.link, _.omit(props.style, 'color'), StyleUtils.getMentionStyle(isOurMention), {color: StyleUtils.getMentionTextColor(isOurMention)}]}
role={CONST.ROLE.LINK}
testID="span"
href={`/${navigationRoute}`}
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
{!_.isEmpty(htmlAttribAccountID) ? `@${displayNameOrLogin}` : <TNodeChildrenRenderer tnode={props.tnode} />}
</Text>
</UserDetailsTooltip>
</Text>
)}
</ShowContextMenuContext.Consumer>
);
}
MentionUserRenderer.propTypes = propTypes;
MentionUserRenderer.displayName = 'MentionUserRenderer';
export default withCurrentUserPersonalDetails(MentionUserRenderer);