import type { FC } from '../../lib/teact/teact'; import React, { memo, useEffect, useMemo } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { ApiChat, ApiThreadInfo, ApiTopic, ApiTypingStatus, ApiUser, } from '../../api/types'; import type { IconName } from '../../types/icons'; import { MediaViewerOrigin, type StoryViewerOrigin, type ThreadId } from '../../types'; import { getChatTypeString, getGroupStatus, getMainUsername, isChatSuperGroup, } from '../../global/helpers'; import { selectChat, selectChatMessages, selectChatOnlineCount, selectThreadInfo, selectThreadMessagesCount, selectTopic, selectUser, } from '../../global/selectors'; import buildClassName from '../../util/buildClassName'; import { REM } from './helpers/mediaDimensions'; import renderText from './helpers/renderText'; import useLastCallback from '../../hooks/useLastCallback'; import useOldLang from '../../hooks/useOldLang'; import Transition from '../ui/Transition'; import Avatar from './Avatar'; import DotAnimation from './DotAnimation'; import FullNameTitle from './FullNameTitle'; import Icon from './icons/Icon'; import TopicIcon from './TopicIcon'; import TypingStatus from './TypingStatus'; const TOPIC_ICON_SIZE = 2.5 * REM; type OwnProps = { chatId: string; threadId?: ThreadId; className?: string; statusIcon?: IconName; typingStatus?: ApiTypingStatus; avatarSize?: 'tiny' | 'small' | 'medium' | 'large' | 'jumbo'; status?: string; withDots?: boolean; withMediaViewer?: boolean; withUsername?: boolean; withFullInfo?: boolean; withUpdatingStatus?: boolean; withChatType?: boolean; noEmojiStatus?: boolean; emojiStatusSize?: number; noRtl?: boolean; noAvatar?: boolean; noStatusOrTyping?: boolean; withStory?: boolean; storyViewerOrigin?: StoryViewerOrigin; isSavedDialog?: boolean; onClick?: VoidFunction; onEmojiStatusClick?: NoneToVoidFunction; }; type StateProps = { chat?: ApiChat; threadInfo?: ApiThreadInfo; topic?: ApiTopic; onlineCount?: number; areMessagesLoaded: boolean; messagesCount?: number; self?: ApiUser; }; const GroupChatInfo: FC = ({ typingStatus, className, statusIcon, avatarSize = 'medium', noAvatar, status, withDots, withMediaViewer, withUsername, withFullInfo, withUpdatingStatus, withChatType, threadInfo, noRtl, chat, onlineCount, areMessagesLoaded, topic, messagesCount, noStatusOrTyping, withStory, storyViewerOrigin, noEmojiStatus, emojiStatusSize, isSavedDialog, self, onClick, onEmojiStatusClick, }) => { const { loadFullChat, openMediaViewer, loadMoreProfilePhotos, } = getActions(); const lang = useOldLang(); const isSuperGroup = chat && isChatSuperGroup(chat); const isTopic = Boolean(chat?.isForum && threadInfo && topic); const { id: chatId, isMin, isRestricted } = chat || {}; useEffect(() => { if (chatId && !isMin) { if (withFullInfo) loadFullChat({ chatId }); if (withMediaViewer) loadMoreProfilePhotos({ peerId: chatId, isPreload: true }); } }, [chatId, isMin, withFullInfo, isSuperGroup, withMediaViewer]); const handleAvatarViewerOpen = useLastCallback( (e: React.MouseEvent, hasMedia: boolean) => { if (chat && hasMedia) { e.stopPropagation(); openMediaViewer({ isAvatarView: true, chatId: chat.id, mediaIndex: 0, origin: avatarSize === 'jumbo' ? MediaViewerOrigin.ProfileAvatar : MediaViewerOrigin.MiddleHeaderAvatar, }); } }, ); const mainUsername = useMemo(() => chat && withUsername && getMainUsername(chat), [chat, withUsername]); if (!chat) { return undefined; } function renderStatusOrTyping() { if (status) { return withDots ? ( ) : ( {statusIcon && } {renderText(status)} ); } if (withUpdatingStatus && !areMessagesLoaded && !isRestricted) { return ( ); } if (!chat) { return undefined; } if (typingStatus) { return ; } if (isTopic) { return ( {messagesCount !== undefined && lang('messages', messagesCount, 'i')} ); } if (withChatType) { return ( {lang(getChatTypeString(chat))} ); } const groupStatus = getGroupStatus(lang, chat); const onlineStatus = onlineCount ? `, ${lang('OnlineCount', onlineCount, 'i')}` : undefined; return ( {mainUsername && {mainUsername}} {groupStatus} {onlineStatus && {onlineStatus}} ); } return (
{!noAvatar && !isTopic && ( <> {isSavedDialog && self && ( )} )} {isTopic && ( )}
{topic ?

{renderText(topic.title)}

: ( )} {!noStatusOrTyping && renderStatusOrTyping()}
); }; export default memo(withGlobal( (global, { chatId, threadId }): StateProps => { const chat = selectChat(global, chatId); const threadInfo = threadId ? selectThreadInfo(global, chatId, threadId) : undefined; const onlineCount = chat ? selectChatOnlineCount(global, chat) : undefined; const areMessagesLoaded = Boolean(selectChatMessages(global, chatId)); const topic = threadId ? selectTopic(global, chatId, threadId) : undefined; const messagesCount = topic && selectThreadMessagesCount(global, chatId, threadId!); const self = selectUser(global, global.currentUserId!); return { chat, threadInfo, onlineCount, topic, areMessagesLoaded, messagesCount, self, }; }, )(GroupChatInfo));