import type { FC } from '../../lib/teact/teact'; import React, { useCallback, memo, useEffect } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import { selectCanDeleteSelectedMessages, selectCurrentChat, selectTabState, selectUser, } from '../../global/selectors'; import { isUserId, getUserFirstOrLastName, getPrivateChatUserId, isChatBasicGroup, isChatSuperGroup, } from '../../global/helpers'; import renderText from '../common/helpers/renderText'; import useLang from '../../hooks/useLang'; import usePrevious from '../../hooks/usePrevious'; import Modal from '../ui/Modal'; import Button from '../ui/Button'; export type OwnProps = { isOpen: boolean; isSchedule: boolean; onClose: () => void; }; type StateProps = { selectedMessageIds?: number[]; canDeleteForAll?: boolean; contactName?: string; willDeleteForCurrentUserOnly?: boolean; willDeleteForAll?: boolean; }; const DeleteSelectedMessageModal: FC = ({ isOpen, isSchedule, selectedMessageIds, canDeleteForAll, contactName, willDeleteForCurrentUserOnly, willDeleteForAll, onClose, }) => { const { deleteMessages, deleteScheduledMessages, exitMessageSelectMode, } = getActions(); const prevIsOpen = usePrevious(isOpen); const handleDeleteMessageForAll = useCallback(() => { onClose(); deleteMessages({ messageIds: selectedMessageIds!, shouldDeleteForAll: true }); }, [deleteMessages, selectedMessageIds, onClose]); const handleDeleteMessageForSelf = useCallback(() => { if (isSchedule) { deleteScheduledMessages({ messageIds: selectedMessageIds! }); } else { deleteMessages({ messageIds: selectedMessageIds!, shouldDeleteForAll: false }); } onClose(); }, [isSchedule, onClose, deleteScheduledMessages, selectedMessageIds, deleteMessages]); const lang = useLang(); // Returning `undefined` from FC instead of `` doesn't trigger useHistoryBack useEffect(() => { if (!isOpen && prevIsOpen) { exitMessageSelectMode(); } }, [exitMessageSelectMode, isOpen, prevIsOpen]); if (!selectedMessageIds) { return undefined; } return (

{lang('AreYouSureDeleteFewMessages')}

{willDeleteForCurrentUserOnly && (

This will delete them just for you, not for other participants in the chat.

)} {willDeleteForAll && (

This will delete them for everyone in this chat.

)}
{canDeleteForAll && ( )}
); }; export default memo(withGlobal( (global, { isSchedule }): StateProps => { const { messageIds: selectedMessageIds } = selectTabState(global).selectedMessages || {}; const { canDeleteForAll } = selectCanDeleteSelectedMessages(global); const chat = selectCurrentChat(global); const contactName = chat && isUserId(chat.id) ? getUserFirstOrLastName(selectUser(global, getPrivateChatUserId(chat)!)) : undefined; const willDeleteForCurrentUserOnly = chat && isChatBasicGroup(chat) && !canDeleteForAll; const willDeleteForAll = chat && isChatSuperGroup(chat); return { selectedMessageIds, canDeleteForAll: !isSchedule && canDeleteForAll, contactName, willDeleteForCurrentUserOnly, willDeleteForAll, }; }, )(DeleteSelectedMessageModal));