import React, { FC, memo, useCallback, useEffect, useMemo, useState, } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import { ApiBotCommand, ApiChat } from '../../api/types'; import { IAnchorPosition } from '../../types'; import { REPLIES_USER_ID } from '../../config'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment'; import { disableScrolling, enableScrolling } from '../../util/scrollLock'; import { selectChat, selectNotifySettings, selectNotifyExceptions, selectUser, selectChatBot, } from '../../global/selectors'; import { isUserId, getCanDeleteChat, selectIsChatMuted, getCanAddContact, isChatChannel, isChatGroup, } from '../../global/helpers'; import useShowTransition from '../../hooks/useShowTransition'; import useLang from '../../hooks/useLang'; import Portal from '../ui/Portal'; import Menu from '../ui/Menu'; import MenuItem from '../ui/MenuItem'; import DeleteChatModal from '../common/DeleteChatModal'; import ReportModal from '../common/ReportModal'; import './HeaderMenuContainer.scss'; const BOT_BUTTONS: Record = { settings: { icon: 'bots', label: 'BotSettings', }, privacy: { icon: 'info', label: 'Privacy', }, help: { icon: 'help', label: 'BotHelp', }, }; export type OwnProps = { chatId: string; threadId: number; isOpen: boolean; withExtraActions: boolean; anchor: IAnchorPosition; isChannel?: boolean; canStartBot?: boolean; canRestartBot?: boolean; canSubscribe?: boolean; canSearch?: boolean; canCall?: boolean; canMute?: boolean; canViewStatistics?: boolean; canLeave?: boolean; canEnterVoiceChat?: boolean; canCreateVoiceChat?: boolean; onSubscribeChannel: () => void; onSearchClick: () => void; onClose: () => void; onCloseAnimationEnd: () => void; }; type StateProps = { chat?: ApiChat; botCommands?: ApiBotCommand[]; isPrivate?: boolean; isMuted?: boolean; canAddContact?: boolean; canReportChat?: boolean; canDeleteChat?: boolean; hasLinkedChat?: boolean; }; const HeaderMenuContainer: FC = ({ chatId, isOpen, withExtraActions, anchor, isChannel, botCommands, canStartBot, canRestartBot, canSubscribe, canSearch, canCall, canMute, canViewStatistics, canLeave, canEnterVoiceChat, canCreateVoiceChat, chat, isPrivate, isMuted, canReportChat, canDeleteChat, hasLinkedChat, canAddContact, onSubscribeChannel, onSearchClick, onClose, onCloseAnimationEnd, }) => { const { updateChatMutedState, enterMessageSelectMode, sendBotCommand, restartBot, joinGroupCall, createGroupCall, openLinkedChat, openAddContactDialog, requestCall, toggleStatistics, } = getActions(); const [isMenuOpen, setIsMenuOpen] = useState(true); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [isReportModalOpen, setIsReportModalOpen] = useState(false); const { x, y } = anchor; useShowTransition(isOpen, onCloseAnimationEnd, undefined, false); const handleReport = useCallback(() => { setIsMenuOpen(false); setIsReportModalOpen(true); }, []); const closeReportModal = useCallback(() => { setIsReportModalOpen(false); onClose(); }, [onClose]); const handleDelete = useCallback(() => { setIsMenuOpen(false); setIsDeleteModalOpen(true); }, []); const closeMenu = useCallback(() => { setIsMenuOpen(false); onClose(); }, [onClose]); const closeDeleteModal = useCallback(() => { setIsDeleteModalOpen(false); onClose(); }, [onClose]); const handleStartBot = useCallback(() => { sendBotCommand({ command: '/start' }); }, [sendBotCommand]); const handleRestartBot = useCallback(() => { restartBot({ chatId }); }, [chatId, restartBot]); const handleToggleMuteClick = useCallback(() => { updateChatMutedState({ chatId, isMuted: !isMuted }); closeMenu(); }, [chatId, closeMenu, isMuted, updateChatMutedState]); const handleEnterVoiceChatClick = useCallback(() => { if (canCreateVoiceChat) { // TODO show popup to schedule createGroupCall({ chatId, }); } else { joinGroupCall({ chatId, }); } closeMenu(); }, [closeMenu, canCreateVoiceChat, chatId, joinGroupCall, createGroupCall]); const handleLinkedChatClick = useCallback(() => { openLinkedChat({ id: chatId }); closeMenu(); }, [chatId, closeMenu, openLinkedChat]); const handleAddContactClick = useCallback(() => { openAddContactDialog({ userId: chatId }); closeMenu(); }, [openAddContactDialog, chatId, closeMenu]); const handleSubscribe = useCallback(() => { onSubscribeChannel(); closeMenu(); }, [closeMenu, onSubscribeChannel]); const handleVideoCall = useCallback(() => { requestCall({ userId: chatId, isVideo: true }); closeMenu(); }, [chatId, closeMenu, requestCall]); const handleCall = useCallback(() => { requestCall({ userId: chatId }); closeMenu(); }, [chatId, closeMenu, requestCall]); const handleSearch = useCallback(() => { onSearchClick(); closeMenu(); }, [closeMenu, onSearchClick]); const handleStatisticsClick = useCallback(() => { toggleStatistics(); closeMenu(); }, [closeMenu, toggleStatistics]); const handleSelectMessages = useCallback(() => { enterMessageSelectMode(); closeMenu(); }, [closeMenu, enterMessageSelectMode]); useEffect(() => { disableScrolling(); return enableScrolling; }, []); const lang = useLang(); const botButtons = useMemo(() => { return botCommands?.map(({ command }) => { const cmd = BOT_BUTTONS[command]; if (!cmd) return undefined; const handleClick = () => { sendBotCommand({ command: `/${command}` }); closeMenu(); }; return ( {lang(cmd.label)} ); }); }, [botCommands, closeMenu, lang, sendBotCommand]); return (
{withExtraActions && canStartBot && ( {lang('BotStart')} )} {withExtraActions && canRestartBot && ( {lang('BotRestart')} )} {withExtraActions && canSubscribe && ( {lang(isChannel ? 'ProfileJoinChannel' : 'ProfileJoinGroup')} )} {canAddContact && ( {lang('AddContact')} )} {IS_SINGLE_COLUMN_LAYOUT && canCall && ( {lang('Call')} )} {canCall && ( {lang('VideoCall')} )} {IS_SINGLE_COLUMN_LAYOUT && canSearch && ( {lang('Search')} )} {canMute && ( {lang(isMuted ? 'ChatsUnmute' : 'ChatsMute')} )} {(canEnterVoiceChat || canCreateVoiceChat) && ( {lang(canCreateVoiceChat ? 'StartVoipChat' : 'VoipGroupJoinCall')} )} {hasLinkedChat && ( {lang(isChannel ? 'ViewDiscussion' : 'lng_profile_view_channel')} )} {lang('ReportSelectMessages')} {canViewStatistics && ( {lang('Statistics')} )} {canReportChat && ( {lang('ReportPeer.Report')} )} {botButtons} {canLeave && ( {lang(isPrivate ? 'DeleteChatUser' : (canDeleteChat ? 'GroupInfo.DeleteAndExit' : (isChannel ? 'LeaveChannel' : 'Group.LeaveGroup')))} )} {chat && ( )} {canReportChat && chat?.id && ( )}
); }; export default memo(withGlobal( (global, { chatId }): StateProps => { const chat = selectChat(global, chatId); if (!chat || chat.isRestricted) { return {}; } const isPrivate = isUserId(chat.id); const user = isPrivate ? selectUser(global, chatId) : undefined; const canAddContact = user && getCanAddContact(user); const canReportChat = isChatChannel(chat) || isChatGroup(chat) || (user && !user.isSelf); const chatBot = chatId !== REPLIES_USER_ID ? selectChatBot(global, chatId) : undefined; return { chat, isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)), isPrivate, canAddContact, canReportChat, canDeleteChat: getCanDeleteChat(chat), hasLinkedChat: Boolean(chat?.fullInfo?.linkedChatId), botCommands: chatBot?.fullInfo?.botInfo?.commands, }; }, )(HeaderMenuContainer));