import type { MouseEvent as ReactMouseEvent } from 'react'; import type { FC } from '../../lib/teact/teact'; import React, { useEffect, useCallback, memo } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { ApiChat, ApiTypingStatus } from '../../api/types'; import type { GlobalState } from '../../global/types'; import { MediaViewerOrigin } from '../../types'; import { getChatTypeString, getChatTitle, isChatSuperGroup, } from '../../global/helpers'; import { selectChat, selectChatMessages, selectChatOnlineCount } from '../../global/selectors'; import renderText from './helpers/renderText'; import type { LangFn } from '../../hooks/useLang'; import useLang from '../../hooks/useLang'; import Avatar from './Avatar'; import VerifiedIcon from './VerifiedIcon'; import TypingStatus from './TypingStatus'; import DotAnimation from './DotAnimation'; import FakeIcon from './FakeIcon'; type OwnProps = { chatId: string; typingStatus?: ApiTypingStatus; avatarSize?: 'small' | 'medium' | 'large' | 'jumbo'; status?: string; withDots?: boolean; withMediaViewer?: boolean; withUsername?: boolean; withFullInfo?: boolean; withUpdatingStatus?: boolean; withChatType?: boolean; noRtl?: boolean; }; type StateProps = { chat?: ApiChat; onlineCount?: number; areMessagesLoaded: boolean; } & Pick; const GroupChatInfo: FC = ({ typingStatus, avatarSize = 'medium', status, withDots, withMediaViewer, withUsername, withFullInfo, withUpdatingStatus, withChatType, noRtl, chat, onlineCount, areMessagesLoaded, lastSyncTime, }) => { const { loadFullChat, openMediaViewer, } = getActions(); const isSuperGroup = chat && isChatSuperGroup(chat); const { id: chatId, isMin, isRestricted } = chat || {}; useEffect(() => { if (chatId && !isMin && withFullInfo && lastSyncTime) { loadFullChat({ chatId }); } }, [chatId, isMin, lastSyncTime, withFullInfo, loadFullChat, isSuperGroup]); const handleAvatarViewerOpen = useCallback((e: ReactMouseEvent, hasPhoto: boolean) => { if (chat && hasPhoto) { e.stopPropagation(); openMediaViewer({ avatarOwnerId: chat.id, origin: avatarSize === 'jumbo' ? MediaViewerOrigin.ProfileAvatar : MediaViewerOrigin.MiddleHeaderAvatar, }); } }, [chat, avatarSize, openMediaViewer]); const lang = useLang(); if (!chat) { return undefined; } function renderStatusOrTyping() { if (status) { return withDots ? ( ) : ( {status} ); } if (withUpdatingStatus && !areMessagesLoaded && !isRestricted) { return ( ); } if (!chat) { return undefined; } if (typingStatus) { return ; } if (withChatType) { return ( {lang(getChatTypeString(chat))} ); } const handle = withUsername ? chat.username : undefined; const groupStatus = getGroupStatus(lang, chat); const onlineStatus = onlineCount ? `, ${lang('OnlineCount', onlineCount, 'i')}` : undefined; return ( {handle && {handle}} {groupStatus} {onlineStatus && {onlineStatus}} ); } return (

{renderText(getChatTitle(lang, chat))}

{chat.isVerified && } {chat.fakeType && }
{renderStatusOrTyping()}
); }; function getGroupStatus(lang: LangFn, chat: ApiChat) { const chatTypeString = lang(getChatTypeString(chat)); const { membersCount } = chat; if (chat.isRestricted) { return chatTypeString === 'Channel' ? 'channel is inaccessible' : 'group is inaccessible'; } if (!membersCount) { return chatTypeString; } return chatTypeString === 'Channel' ? lang('Subscribers', membersCount, 'i') : lang('Members', membersCount, 'i'); } export default memo(withGlobal( (global, { chatId }): StateProps => { const { lastSyncTime } = global; const chat = selectChat(global, chatId); const onlineCount = chat ? selectChatOnlineCount(global, chat) : undefined; const areMessagesLoaded = Boolean(selectChatMessages(global, chatId)); return { lastSyncTime, chat, onlineCount, areMessagesLoaded, }; }, )(GroupChatInfo));