// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React from 'react'; import {IntlShape, useIntl} from 'react-intl'; import {StyleProp, Text, View, ViewStyle} from 'react-native'; import ChannelIcon from '@components/channel_icon'; import CustomStatusEmoji from '@components/custom_status/custom_status_emoji'; import FormattedText from '@components/formatted_text'; import ProfilePicture from '@components/profile_picture'; import {BotTag, GuestTag} from '@components/tag'; import {General} from '@constants'; import {useTheme} from '@context/theme'; import {makeStyleSheetFromTheme, changeOpacity} from '@utils/theme'; import {typography} from '@utils/typography'; import {getUserCustomStatus, isBot, isCustomStatusExpired, isGuest, isShared} from '@utils/user'; import type UserModel from '@typings/database/models/servers/user'; type AtMentionItemProps = { user?: UserProfile | UserModel; containerStyle?: StyleProp; currentUserId: string; showFullName: boolean; testID?: string; isCustomStatusEnabled: boolean; pictureContainerStyle?: StyleProp; } const getName = (user: UserProfile | UserModel | undefined, showFullName: boolean, isCurrentUser: boolean, intl: IntlShape) => { let name = ''; if (!user) { return intl.formatMessage({id: 'channel_loader.someone', defaultMessage: 'Someone'}); } const hasNickname = user.nickname.length > 0; if (showFullName) { const first = 'first_name' in user ? user.first_name : user.firstName; const last = 'last_name' in user ? user.last_name : user.lastName; name += `${first} ${last} `; } if (hasNickname && !isCurrentUser) { name += name.length > 0 ? `(${user.nickname})` : user.nickname; } return name.trim(); }; const getStyleFromTheme = makeStyleSheetFromTheme((theme: Theme) => { return { row: { height: 40, paddingVertical: 8, paddingTop: 4, paddingHorizontal: 16, flexDirection: 'row', alignItems: 'center', }, rowPicture: { marginRight: 10, marginLeft: 2, width: 24, alignItems: 'center', justifyContent: 'center', }, rowInfo: { flexDirection: 'row', overflow: 'hidden', }, rowFullname: { ...typography('Body', 200), color: theme.centerChannelColor, paddingLeft: 4, flexShrink: 1, }, rowUsername: { ...typography('Body', 200), color: changeOpacity(theme.centerChannelColor, 0.64), fontSize: 15, fontFamily: 'OpenSans', flexShrink: 5, }, icon: { marginLeft: 4, }, }; }); const UserItem = ({ containerStyle, user, currentUserId, showFullName, testID, isCustomStatusEnabled, pictureContainerStyle, }: AtMentionItemProps) => { const theme = useTheme(); const style = getStyleFromTheme(theme); const intl = useIntl(); const bot = user ? isBot(user) : false; const guest = user ? isGuest(user.roles) : false; const shared = user ? isShared(user) : false; const isCurrentUser = currentUserId === user?.id; const name = getName(user, showFullName, isCurrentUser, intl); const customStatus = getUserCustomStatus(user); const customStatusExpired = isCustomStatusExpired(user); const userItemTestId = `${testID}.${user?.id}`; return ( {bot && } {guest && } {Boolean(name.length) && {name} } {isCurrentUser && } {Boolean(user) && {` @${user!.username}`} } {Boolean(isCustomStatusEnabled && !bot && customStatus?.emoji && !customStatusExpired) && ( )} {shared && ( )} ); }; export default UserItem;