import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants' ; import { Attachment , Message } from '@proton/shared/lib/interfaces/mail/Message' ; import { MESSAGE_FLAGS } from '@proton/shared/lib/mail/constants' ; import { setFlag } from '@proton/shared/lib/mail/messages' ; import { PayloadAction } from '@reduxjs/toolkit' ; import { Draft } from 'immer' ; import { replaceEmbeddedAttachments } from '../../../helpers/message/messageEmbeddeds' ; import { getEmbeddedImages , updateImages } from '../../../helpers/message/messageImages' ; import { getLocalID , getMessage } from '../helpers/messagesReducer' ; import { MessageEmbeddedImage , MessagesState , MessageState , PartialMessageState } from '../messagesTypes' ; export const createDraft = ( state , { payload : message } ) => { ( state as MessagesState ) [ message . localID ] = message ; } ; export const openDraft = ( state , { payload : { ID , fromUndo } } ) => { const localID = getLocalID ( state , ID ) ; const messageState = getMessage ( state , ID ) ; if ( messageState ) { messageState . messageDocument = undefined ; if ( ! messageState . draftFlags ) { messageState . draftFlags = { } ; } messageState . draftFlags = { ... messageState . draftFlags , openDraftFromUndo : fromUndo , isSentDraft : false , } ; messageState . messageImages = undefined ; } else { state [ localID ] = { localID , draftFlags : { openDraftFromUndo : fromUndo } } ; } } ; export const removeInitialAttachments = ( state , { payload : ID } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . draftFlags ) { messageState . draftFlags . initialAttachments = undefined ; } } ; export const draftSaved = ( state , { payload : { ID , message } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . data ) { messageState . data = message ; messageState . messageImages = replaceEmbeddedAttachments ( messageState . data as PartialMessageState , message . Attachments ) ; } else { state [ ID ] = { localID : ID , data : message } as any ; } } ; export const updateScheduled = ( state , { payload : { ID , scheduledAt } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState ) { if ( messageState . draftFlags ) { messageState . draftFlags . scheduledAt = scheduledAt ; } else { messageState . draftFlags = { scheduledAt } ; } } } ; export const updateExpires = ( state , { payload : { ID , expiresIn } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState ) { if ( messageState . draftFlags ) { messageState . draftFlags . expiresIn = expiresIn ; } else { messageState . draftFlags = { expiresIn } ; } } } ; export const startSending = ( state , { payload : ID } ) => { const message = getMessage ( state , ID ) ; if ( message && message . draftFlags ) { message . draftFlags . sending = true ; } } ; export const sendModifications = ( state , { payload : { ID , attachments , images } , } ) => { const message = getMessage ( state , ID ) ; if ( message && message . data ) { message . data . Attachments . push ( ... attachments ) ; const embeddedImages = getEmbeddedImages ( message ) ; embeddedImages . push ( ... images ) ; message . messageImages = updateImages ( message . messageImages , undefined , undefined , embeddedImages ) ; } } ; export const endUndo = ( state , { payload : ID } ) => { const message = getMessage ( state , ID ) ; if ( message && message . data ) { message . loadRetry = 0 ; message . data . LabelIDs = message . data . LabelIDs . filter ( ( value ) => value !== MAILBOX_LABEL_IDS . OUTBOX ) ; message . data . Flags = setFlag ( MESSAGE_FLAGS . FLAG_SENT ) ( message . data ) ; } } ; export const sent = ( state , { payload : Sent } ) => { const message = getMessage ( state , Sent . ID ) ; if ( message ) { message . data = Sent ; message . messageDocument = undefined ; message . messageImages = undefined ; } } ; export const endSending = ( state , { payload : ID } ) => { const message = getMessage ( state , ID ) ; if ( message && message . draftFlags ) { message . draftFlags . sending = false ; } } ; export const deleteDraft = ( state , { payload : ID } ) => { const localID = getLocalID ( state , ID ) ; delete state [ localID ] ; } ; export const cancelScheduled = ( state , { payload : ID } ) => { const message = getMessage ( state , ID ) ; if ( message ) { message . loadRetry = 0 ; if ( message . draftFlags ) { message . draftFlags . scheduledAt = undefined ; } } } ;