import { c , msgid } from 'ttag' ; import { Alert , ConfirmModal , ErrorButton , useApi , useEventManager , useModals , useNotifications , } from '@proton/components' ; import { deleteConversations } from '@proton/shared/lib/api/conversations' ; import { deleteMessages } from '@proton/shared/lib/api/messages' ; import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants' ; import { useGetElementsFromIDs } from './mailbox/useElements' ; import { isConversation } from '../helpers/elements' ; import useOptimisticDelete from './optimistic/useOptimisticDelete' ; const { DRAFTS , ALL_DRAFTS } = MAILBOX_LABEL_IDS ; export const getDeleteTitle = ( isDraft , isConversationMode , count , totalMessages ? ) => { if ( isDraft ) { if ( count === 1 ) { return c ( 'Title' ) . t `Delete draft` ; } return c ( 'Title' ) . ngettext ( msgid `Delete ${ count } draft` , `Delete ${ count } drafts` , count ) ; } if ( isConversationMode ) { if ( count === 1 ) { if ( totalMessages === 1 ) { return c ( 'Title' ) . t `Delete message` ; } return c ( 'Title' ) . t `Delete conversation` ; } return c ( 'Title' ) . ngettext ( msgid `Delete ${ count } conversation` , `Delete ${ count } conversations` , count ) ; } if ( count === 1 ) { return c ( 'Title' ) . t `Delete message` ; } return c ( 'Title' ) . ngettext ( msgid `Delete ${ count } message` , `Delete ${ count } messages` , count ) ; } ; export const getModalText = ( isDraft , isConversationMode , count , totalMessages ? ) => { if ( isDraft ) { if ( count === 1 ) { return c ( 'Info' ) . t `Are you sure you want to permanently delete this draft?` ; } return c ( 'Info' ) . ngettext ( msgid `Are you sure you want to permanently delete ${ count } draft?` , `Are you sure you want to permanently delete ${ count } drafts?` , count ) ; } if ( isConversationMode ) { if ( count === 1 ) { if ( totalMessages === 1 ) { return c ( 'Info' ) . t `Are you sure you want to permanently delete this message?` ; } return c ( 'Info' ) . t `Are you sure you want to permanently delete this conversation?` ; } return c ( 'Info' ) . ngettext ( msgid `Are you sure you want to permanently delete ${ count } conversation?` , `Are you sure you want to permanently delete ${ count } conversations?` , count ) ; } if ( count === 1 ) { return c ( 'Info' ) . t `Are you sure you want to permanently delete this message?` ; } return c ( 'Info' ) . ngettext ( msgid `Are you sure you want to permanently delete ${ count } message?` , `Are you sure you want to permanently delete ${ count } messages?` , count ) ; } ; export const getNotificationText = ( isDraft , isConversationMode , count , totalMessages ? ) => { if ( isDraft ) { if ( count === 1 ) { return c ( 'Success' ) . t `Draft deleted` ; } return c ( 'Success' ) . ngettext ( msgid `${ count } draft deleted` , `${ count } drafts deleted` , count ) ; } if ( isConversationMode ) { if ( count === 1 ) { if ( totalMessages === 1 ) { return c ( 'Success' ) . t `Message deleted` ; } return c ( 'Success' ) . t `Conversation deleted` ; } return c ( 'Success' ) . ngettext ( msgid `${ count } conversation deleted` , `${ count } conversations deleted` , count ) ; } if ( count === 1 ) { return c ( 'Success' ) . t `Message deleted` ; } return c ( 'Success' ) . ngettext ( msgid `${ count } message deleted` , `${ count } messages deleted` , count ) ; } ; export const usePermanentDelete = ( labelID ) => { const { createNotification } = useNotifications ( ) ; const { createModal } = useModals ( ) ; const { call } = useEventManager ( ) ; const api = useApi ( ) ; const getElementsFromIDs = useGetElementsFromIDs ( ) ; const optimisticDelete = useOptimisticDelete ( ) ; return async ( selectedIDs ) => { const selectedItemsCount = selectedIDs . length ; const draft = labelID === DRAFTS || labelID === ALL_DRAFTS ; const elements = getElementsFromIDs ( selectedIDs ) ; const totalMessages = elements . reduce ( ( total , element ) => { return total + ( ( 'ContextNumMessages' in element && element . ContextNumMessages ) || 0 ) ; } , 0 ) ; const conversationMode = isConversation ( elements [ 0 ] ) ; const modalTitle = getDeleteTitle ( draft , conversationMode , selectedItemsCount , totalMessages ) ; const modalText = getModalText ( draft , conversationMode , selectedItemsCount , totalMessages ) ; await new Promise ( ( resolve , reject ) => { createModal ( < ConfirmModal title = { modalTitle } confirm = { < ErrorButton type = "submit" > { c ( 'Action' ) . t `Delete` } < /ErrorButton>} onConfirm={() => resolve(undefined)} onClose={reject}> < Alert className = "mb1" type = "error" > { modalText } < /Alert> < /ConfirmModal>); } ) ; const rollback = optimisticDelete ( elements , labelID ) ; try { const action = conversationMode ? deleteConversations ( selectedIDs , labelID ) : deleteMessages ( selectedIDs ) ; await api ( action ) ; await call ( ) ; const notificationText = getNotificationText ( draft , conversationMode , selectedItemsCount , totalMessages ) ; createNotification ( { text : notificationText } ) ; } catch { rollback ( ) ; } } ; } ;