import React, { FC, useCallback, memo } from '../../lib/teact/teact'; import { withGlobal } from '../../lib/teact/teactn'; import { ApiChat } from '../../api/types'; import { GlobalActions } from '../../global/types'; import { selectIsChatWithSelf, selectUser } from '../../modules/selectors'; import { isChatPrivate, getUserFirstOrLastName, getPrivateChatUserId, isChatBasicGroup, isChatSuperGroup, isChatChannel, getChatTitle, } from '../../modules/helpers'; import { pick } from '../../util/iteratees'; import useLang from '../../hooks/useLang'; import renderText from './helpers/renderText'; import Avatar from './Avatar'; import Modal from '../ui/Modal'; import Button from '../ui/Button'; import './DeleteChatModal.scss'; export type OwnProps = { isOpen: boolean; chat: ApiChat; onClose: () => void; onCloseAnimationEnd?: () => void; }; type StateProps = { isChannel: boolean; isChatWithSelf?: boolean; isPrivateChat: boolean; isBasicGroup: boolean; isSuperGroup: boolean; canDeleteForAll?: boolean; contactName?: string; }; type DispatchProps = Pick; const DeleteChatModal: FC = ({ isOpen, chat, isChannel, isPrivateChat, isChatWithSelf, isBasicGroup, isSuperGroup, canDeleteForAll, contactName, onClose, onCloseAnimationEnd, leaveChannel, deleteHistory, deleteChannel, }) => { const lang = useLang(); const chatTitle = getChatTitle(lang, chat); const handleDeleteMessageForAll = useCallback(() => { deleteHistory({ chatId: chat.id, shouldDeleteForAll: true }); onClose(); }, [deleteHistory, chat.id, onClose]); const handleDeleteChat = useCallback(() => { if (isPrivateChat || isBasicGroup) { deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); } else if ((isChannel || isSuperGroup) && !chat.isCreator) { leaveChannel({ chatId: chat.id }); } else if ((isChannel || isSuperGroup) && chat.isCreator) { deleteChannel({ chatId: chat.id }); } onClose(); }, [ isPrivateChat, isBasicGroup, isChannel, isSuperGroup, chat.isCreator, chat.id, onClose, deleteHistory, leaveChannel, deleteChannel, ]); function renderHeader() { return (

{lang(renderTitle())}

); } function renderTitle() { if (isChannel && !chat.isCreator) { return 'LeaveChannel'; } if (isChannel && chat.isCreator) { return 'ChannelDelete'; } if (isBasicGroup || isSuperGroup) { return 'Group.LeaveGroup'; } return 'DeleteChatUser'; } function renderMessage() { if (isChannel && chat.isCreator) { return

{renderText(lang('ChatList.DeleteAndLeaveGroupConfirmation', chatTitle), ['simple_markdown'])}

; } if ((isChannel && !chat.isCreator) || isBasicGroup || isSuperGroup) { return

{renderText(lang('ChannelLeaveAlertWithName', chatTitle), ['simple_markdown'])}

; } return

{renderText(lang('ChatList.DeleteChatConfirmation', contactName), ['simple_markdown'])}

; } function renderActionText() { if (isChannel && !chat.isCreator) { return 'LeaveChannel'; } if (isChannel && chat.isCreator) { return 'Chat.Input.Delete'; } if (isBasicGroup || isSuperGroup) { return 'Group.LeaveGroup'; } return canDeleteForAll ? 'ChatList.DeleteForCurrentUser' : 'Delete'; } return ( {renderMessage()} {canDeleteForAll && ( )} ); }; export default memo(withGlobal( (global, { chat }): StateProps => { const isPrivateChat = isChatPrivate(chat.id); const isChatWithSelf = selectIsChatWithSelf(global, chat.id); const canDeleteForAll = (isPrivateChat && !isChatWithSelf); const contactName = chat && isChatPrivate(chat.id) ? getUserFirstOrLastName(selectUser(global, getPrivateChatUserId(chat)!)) : undefined; return { isPrivateChat, isChatWithSelf, isChannel: isChatChannel(chat), isBasicGroup: isChatBasicGroup(chat), isSuperGroup: isChatSuperGroup(chat), canDeleteForAll, contactName, }; }, (setGlobal, actions): DispatchProps => pick(actions, ['leaveChannel', 'deleteHistory', 'deleteChannel']), )(DeleteChatModal));