import type { FC } from '../../lib/teact/teact'; import { memo } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { ApiChat } from '../../api/types'; import { getChatTitle, getPrivateChatUserId, getUserFirstOrLastName, isChatBasicGroup, isChatChannel, isChatSuperGroup, isUserBot, } from '../../global/helpers'; import { selectIsChatWithSelf, selectUser } from '../../global/selectors'; import { isUserId } from '../../util/entities/ids'; import renderText from './helpers/renderText'; import useLastCallback from '../../hooks/useLastCallback'; import useOldLang from '../../hooks/useOldLang'; import Button from '../ui/Button'; import Modal from '../ui/Modal'; import Avatar from './Avatar'; import './DeleteChatModal.scss'; export type OwnProps = { isOpen: boolean; chat: ApiChat; isSavedDialog?: boolean; onClose: () => void; onCloseAnimationEnd?: () => void; }; type StateProps = { isChannel: boolean; isChatWithSelf?: boolean; isBot?: boolean; isPrivateChat: boolean; isBasicGroup: boolean; isSuperGroup: boolean; currentUserId: string | undefined; canDeleteForAll?: boolean; contactName?: string; }; const DeleteChatModal: FC = ({ isOpen, chat, isSavedDialog, isChannel, isPrivateChat, isChatWithSelf, isBot, isBasicGroup, isSuperGroup, currentUserId, canDeleteForAll, contactName, onClose, onCloseAnimationEnd, }) => { const { leaveChannel, deleteHistory, deleteSavedHistory, deleteChannel, deleteChatUser, blockUser, deleteChat, } = getActions(); const lang = useOldLang(); const chatTitle = getChatTitle(lang, chat); const handleDeleteForAll = useLastCallback(() => { deleteHistory({ chatId: chat.id, shouldDeleteForAll: true }); onClose(); }); const handleDeleteAndStop = useLastCallback(() => { deleteHistory({ chatId: chat.id, shouldDeleteForAll: true }); blockUser({ userId: chat.id }); onClose(); }); const handleDeleteChat = useLastCallback(() => { if (isSavedDialog) { deleteSavedHistory({ chatId: chat.id }); } else if (isPrivateChat) { deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); } else if (isBasicGroup) { if (chat.isCreator) { deleteHistory({ chatId: chat.id, shouldDeleteForAll: true }); deleteChat({ chatId: chat.id }); } else { deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); deleteChatUser({ chatId: chat.id, userId: currentUserId! }); } } else if ((isChannel || isSuperGroup) && !chat.isCreator) { leaveChannel({ chatId: chat.id }); } else if ((isChannel || isSuperGroup) && chat.isCreator) { deleteChannel({ chatId: chat.id }); } onClose(); }); const handleLeaveChat = useLastCallback(() => { if (isChannel || isSuperGroup) { leaveChannel({ chatId: chat.id }); onClose(); } else if (isBasicGroup && chat.isCreator) { deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); deleteChatUser({ chatId: chat.id, userId: currentUserId! }); } else { handleDeleteChat(); } }); function renderHeader() { return (

{lang(renderTitle())}

); } function renderTitle() { if (isSavedDialog) { return isChatWithSelf ? 'ClearHistoryMyNotesTitle' : 'ClearHistoryTitleSingle2'; } if (isChannel && !chat.isCreator) { return 'LeaveChannel'; } if (isChannel && chat.isCreator) { return 'ChannelDelete'; } if (isBasicGroup || isSuperGroup) { return 'Group.LeaveGroup'; } return 'DeleteChatUser'; } function renderContent() { if (isSavedDialog) { return (

{renderText( isChatWithSelf ? lang('ClearHistoryMyNotesMessage') : lang('ClearHistoryMessageSingle', chatTitle), ['simple_markdown', 'emoji'], )}

); } if (isChannel && chat.isCreator) { return (

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

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

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

; } return

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

; } function renderActionText() { if (isSavedDialog) { return 'Delete'; } 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 ( {renderContent()}
{isBot && !isSavedDialog && ( )} {canDeleteForAll && ( )} {!isPrivateChat && chat.isCreator && !isSavedDialog && ( )}
); }; export default memo(withGlobal( (global, { chat, isSavedDialog }): StateProps => { const isPrivateChat = isUserId(chat.id); const isChatWithSelf = selectIsChatWithSelf(global, chat.id); const user = isPrivateChat && selectUser(global, getPrivateChatUserId(chat)!); const isBot = user && isUserBot(user) && !chat.isSupport; const canDeleteForAll = (isPrivateChat && !isChatWithSelf && !isBot && !isSavedDialog); const contactName = isPrivateChat ? getUserFirstOrLastName(selectUser(global, getPrivateChatUserId(chat)!)) : undefined; return { isPrivateChat, isChatWithSelf, isBot, isChannel: isChatChannel(chat), isBasicGroup: isChatBasicGroup(chat), isSuperGroup: isChatSuperGroup(chat), currentUserId: global.currentUserId, canDeleteForAll, contactName, }; }, )(DeleteChatModal));