import React, { FC, memo, useCallback, useEffect, useState, } from '../../lib/teact/teact'; import { withGlobal } from '../../lib/teact/teactn'; import { GlobalActions } from '../../global/types'; import { ApiChat } from '../../api/types'; import { IAnchorPosition } from '../../types'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment'; import { disableScrolling, enableScrolling } from '../../util/scrollLock'; import { selectChat, selectNotifySettings, selectNotifyExceptions } from '../../modules/selectors'; import { pick } from '../../util/iteratees'; import { isChatPrivate, getCanDeleteChat, selectIsChatMuted } from '../../modules/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 './HeaderMenuContainer.scss'; type DispatchProps = Pick; export type OwnProps = { chatId: number; threadId: number; isOpen: boolean; anchor: IAnchorPosition; isChannel?: boolean; canStartBot?: boolean; canRestartBot?: boolean; canSubscribe?: boolean; canSearch?: boolean; canMute?: boolean; canSelect?: boolean; canLeave?: boolean; onSubscribeChannel: () => void; onSearchClick: () => void; onClose: () => void; onCloseAnimationEnd: () => void; }; type StateProps = { chat?: ApiChat; isPrivate?: boolean; isMuted?: boolean; canDeleteChat?: boolean; }; const HeaderMenuContainer: FC = ({ chatId, isOpen, anchor, isChannel, canStartBot, canRestartBot, canSubscribe, canSearch, canMute, canSelect, canLeave, chat, isPrivate, isMuted, canDeleteChat, onSubscribeChannel, onSearchClick, onClose, onCloseAnimationEnd, updateChatMutedState, enterMessageSelectMode, sendBotCommand, restartBot, }) => { const [isMenuOpen, setIsMenuOpen] = useState(true); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const { x, y } = anchor; useShowTransition(isOpen, onCloseAnimationEnd, undefined, false); 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 handleSubscribe = useCallback(() => { onSubscribeChannel(); closeMenu(); }, [closeMenu, onSubscribeChannel]); const handleSearch = useCallback(() => { onSearchClick(); closeMenu(); }, [closeMenu, onSearchClick]); const handleSelectMessages = useCallback(() => { enterMessageSelectMode(); closeMenu(); }, [closeMenu, enterMessageSelectMode]); useEffect(() => { disableScrolling(); return enableScrolling; }, []); const lang = useLang(); return (
{IS_SINGLE_COLUMN_LAYOUT && canStartBot && ( {lang('BotStart')} )} {IS_SINGLE_COLUMN_LAYOUT && canRestartBot && ( {lang('BotRestart')} )} {IS_SINGLE_COLUMN_LAYOUT && canSubscribe && ( {lang(isChannel ? 'Subscribe' : 'Join Group')} )} {IS_SINGLE_COLUMN_LAYOUT && canSearch && ( {lang('Search')} )} {canMute && ( {lang(isMuted ? 'ChatsUnmute' : 'ChatsMute')} )} {canSelect && ( {lang('ReportSelectMessages')} )} {canLeave && ( {lang(isPrivate ? 'Delete' : (canDeleteChat ? 'GroupInfo.DeleteAndExit' : (isChannel ? 'LeaveChannel' : 'Group.LeaveGroup')))} )} {chat && ( )}
); }; export default memo(withGlobal( (global, { chatId }): StateProps => { const chat = selectChat(global, chatId); if (!chat || chat.isRestricted) { return {}; } return { chat, isMuted: selectIsChatMuted(chat, selectNotifySettings(global), selectNotifyExceptions(global)), isPrivate: isChatPrivate(chat.id), canDeleteChat: getCanDeleteChat(chat), }; }, (setGlobal, actions): DispatchProps => pick(actions, [ 'updateChatMutedState', 'enterMessageSelectMode', 'sendBotCommand', 'restartBot', ]), )(HeaderMenuContainer));