import { MouseEvent as ReactMouseEvent } from 'react'; import React, { FC, useEffect, useCallback, memo, } from '../../lib/teact/teact'; import { withGlobal } from '../../lib/teact/teactn'; import { ApiChat, ApiTypingStatus } from '../../api/types'; import { GlobalActions, GlobalState } from '../../global/types'; import { MediaViewerOrigin } from '../../types'; import { getChatTypeString, getChatTitle, isChatSuperGroup, } from '../../modules/helpers'; import { selectChat, selectChatMessages, selectChatOnlineCount } from '../../modules/selectors'; import renderText from './helpers/renderText'; import { pick } from '../../util/iteratees'; import useLang, { LangFn } from '../../hooks/useLang'; import Avatar from './Avatar'; import VerifiedIcon from './VerifiedIcon'; import TypingStatus from './TypingStatus'; type OwnProps = { chatId: number; typingStatus?: ApiTypingStatus; avatarSize?: 'small' | 'medium' | 'large' | 'jumbo'; withMediaViewer?: boolean; withHandle?: boolean; withFullInfo?: boolean; withUpdatingStatus?: boolean; withChatType?: boolean; }; type StateProps = { chat?: ApiChat; onlineCount?: number; areMessagesLoaded: boolean; } & Pick; type DispatchProps = Pick; const GroupChatInfo: FC = ({ typingStatus, avatarSize = 'medium', withMediaViewer, withHandle, withFullInfo, withUpdatingStatus, withChatType, chat, onlineCount, areMessagesLoaded, lastSyncTime, loadFullChat, openMediaViewer, }) => { 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 (withUpdatingStatus && !areMessagesLoaded && !isRestricted) { return ( {lang('Updating')} ); } if (!chat) { return undefined; } if (typingStatus) { return ; } if (withChatType) { return (
{lang(getChatTypeString(chat))}
); } const handle = withHandle ? chat.username : undefined; const groupStatus = getGroupStatus(chat, lang); const onlineStatus = onlineCount ? `, ${lang('OnlineCount', onlineCount, 'i')}` : undefined; return (
{handle && {handle}} {groupStatus} {onlineStatus && {onlineStatus}}
); } return (

{renderText(getChatTitle(chat))}

{chat.isVerified && }
{renderStatusOrTyping()}
); }; function getGroupStatus(chat: ApiChat, lang: LangFn) { 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, }; }, (setGlobal, actions): DispatchProps => pick(actions, ['loadFullChat', 'openMediaViewer']), )(GroupChatInfo));