import type { FC } from '../../lib/teact/teact'; import React, { memo, useEffect, useMemo } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { ApiChatMember, ApiTypingStatus, ApiUser, ApiUserStatus, } from '../../api/types'; import type { CustomPeer, StoryViewerOrigin } from '../../types'; import type { IconName } from '../../types/icons'; import { MediaViewerOrigin } from '../../types'; import { getMainUsername, getUserStatus, isSystemBot, isUserOnline, } from '../../global/helpers'; import { selectChatMessages, selectUser, selectUserStatus } from '../../global/selectors'; import buildClassName from '../../util/buildClassName'; import renderText from './helpers/renderText'; import useLastCallback from '../../hooks/useLastCallback'; import useOldLang from '../../hooks/useOldLang'; import RippleEffect from '../ui/RippleEffect'; import Avatar from './Avatar'; import DotAnimation from './DotAnimation'; import FullNameTitle from './FullNameTitle'; import Icon from './icons/Icon'; import TypingStatus from './TypingStatus'; type OwnProps = { userId?: string; customPeer?: CustomPeer; typingStatus?: ApiTypingStatus; avatarSize?: 'tiny' | 'small' | 'medium' | 'large' | 'jumbo'; forceShowSelf?: boolean; status?: string; statusIcon?: IconName; ripple?: boolean; withDots?: boolean; withMediaViewer?: boolean; withUsername?: boolean; withStory?: boolean; withFullInfo?: boolean; withUpdatingStatus?: boolean; storyViewerOrigin?: StoryViewerOrigin; noEmojiStatus?: boolean; noFake?: boolean; noVerified?: boolean; emojiStatusSize?: number; noStatusOrTyping?: boolean; noRtl?: boolean; adminMember?: ApiChatMember; isSavedDialog?: boolean; className?: string; onEmojiStatusClick?: NoneToVoidFunction; iconElement?: React.ReactNode; rightElement?: React.ReactNode; }; type StateProps = { user?: ApiUser; userStatus?: ApiUserStatus; self?: ApiUser; isSavedMessages?: boolean; areMessagesLoaded: boolean; isSynced?: boolean; }; const PrivateChatInfo: FC = ({ customPeer, typingStatus, avatarSize = 'medium', status, statusIcon, withDots, withMediaViewer, withUsername, withStory, withFullInfo, withUpdatingStatus, emojiStatusSize, noStatusOrTyping, noEmojiStatus, noFake, noVerified, noRtl, user, userStatus, self, isSavedMessages, isSavedDialog, areMessagesLoaded, adminMember, ripple, className, storyViewerOrigin, isSynced, onEmojiStatusClick, iconElement, rightElement, }) => { const { loadFullUser, openMediaViewer, loadMoreProfilePhotos, } = getActions(); const lang = useOldLang(); const { id: userId } = user || {}; useEffect(() => { if (userId) { if (withFullInfo && isSynced) loadFullUser({ userId }); if (withMediaViewer) loadMoreProfilePhotos({ peerId: userId, isPreload: true }); } }, [userId, withFullInfo, withMediaViewer, isSynced]); const handleAvatarViewerOpen = useLastCallback( (e: React.MouseEvent, hasMedia: boolean) => { if (user && hasMedia) { e.stopPropagation(); openMediaViewer({ isAvatarView: true, chatId: user.id, mediaIndex: 0, origin: avatarSize === 'jumbo' ? MediaViewerOrigin.ProfileAvatar : MediaViewerOrigin.MiddleHeaderAvatar, }); } }, ); const mainUsername = useMemo(() => user && withUsername && getMainUsername(user), [user, withUsername]); if (!user && !customPeer) { return undefined; } function renderStatusOrTyping() { if (status) { return withDots ? ( ) : ( {statusIcon && } {renderText(status)} ); } if (withUpdatingStatus && !areMessagesLoaded) { return ( ); } if (customPeer?.subtitleKey) { return ( {lang(customPeer.subtitleKey)} ); } if (!user) { return undefined; } if (typingStatus) { return ; } if (isSystemBot(user.id)) { return undefined; } const translatedStatus = getUserStatus(lang, user, userStatus); const mainUserNameClassName = buildClassName('handle', translatedStatus && 'withStatus'); return ( {mainUsername && {mainUsername}} {translatedStatus && {translatedStatus}} ); } const customTitle = adminMember ? adminMember.customTitle || lang(adminMember.isOwner ? 'GroupInfo.LabelOwner' : 'GroupInfo.LabelAdmin') : undefined; function renderNameTitle() { if (customTitle) { return (
{customTitle && {customTitle}}
); } return ( ); } return (
{isSavedDialog && self && ( )}
{renderNameTitle()} {(status || (!isSavedMessages && !noStatusOrTyping)) && renderStatusOrTyping()}
{ripple && } {rightElement}
); }; export default memo(withGlobal( (global, { userId, forceShowSelf }): StateProps => { const { isSynced } = global; const user = userId ? selectUser(global, userId) : undefined; const userStatus = userId ? selectUserStatus(global, userId) : undefined; const isSavedMessages = !forceShowSelf && user && user.isSelf; const self = isSavedMessages ? user : selectUser(global, global.currentUserId!); const areMessagesLoaded = Boolean(userId && selectChatMessages(global, userId)); return { user, userStatus, isSavedMessages, areMessagesLoaded, self, isSynced, }; }, )(PrivateChatInfo));