import { Message } from '@proton/shared/lib/interfaces/mail/Message' ; import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants' ; import { isDraft , isSentAutoReply } from '@proton/shared/lib/mail/messages' ; import { isCustomLabelOrFolder } from '../labels' ; import { hasLabel } from '../elements' ; import { PLACEHOLDER_ID_PREFIX } from '../../hooks/usePlaceholders' ; /**
 * Filter the list of message to find the first readable message
 * - iterate backwards
 * - check if the previous item is read
 * - if the previous isRead === 1, break the iteration
 */ const getFirstMessageToRead = ( messages ) : Message | undefined => { if ( messages . length === 0 ) { return ; } let index = messages . length ; let contains = false ; while ( -- index > 0 ) { if ( messages [ index - 1 ] . Unread === 0 ) { contains = true ; break ; } } const position = contains ? index : 0 ; return messages [ position ] ; } ; const getLast = ( messages ) : Message | undefined => { if ( messages . length === 0 ) { return ; } return messages [ messages . length - 1 ] ; } ; /**
 * Find in the message to scroll and expand
 */ export const findMessageToExpand = ( labelID = '' , messages = [ ] ) : Message | undefined => { if ( messages . length === 0 ) { return ; } if ( messages [ 0 ] . ID . startsWith ( PLACEHOLDER_ID_PREFIX ) ) { return getLast ( messages ) ; } if ( labelID === MAILBOX_LABEL_IDS . STARRED || isCustomLabelOrFolder ( labelID ) ) { return getFirstMessageToRead ( messages . filter ( ( message ) => hasLabel ( message , labelID ) && ! isDraft ( message ) ) ) ; } if ( labelID === MAILBOX_LABEL_IDS . DRAFTS || labelID === MAILBOX_LABEL_IDS . ALL_DRAFTS ) { return getLast ( messages . filter ( isDraft ) ) ; } const realMessages = messages . filter ( ( message ) => ! isDraft ( message ) && ! isSentAutoReply ( message ) ) ; const latest = getLast ( realMessages ) ; if ( latest && latest . Unread === 0 ) { return latest ; } return getFirstMessageToRead ( realMessages ) ; } ;