import { useSelector } from 'react-redux' ; import { useEffect , useRef } from 'react' ; import { useFolders } from '@proton/components' ; import { MAILBOX_LABEL_IDS } from '@proton/shared/lib/constants' ; import isDeepEqual from '@proton/shared/lib/helpers/isDeepEqual' ; import { hasLabel , getCurrentFolderIDs } from '../helpers/elements' ; import { hasErrorType } from '../helpers/errors' ; import { ConversationState } from '../logic/conversations/conversationsTypes' ; import { useGetConversation } from './conversation/useConversation' ; import { conversationByID } from '../logic/conversations/conversationsSelectors' ; import { RootState } from '../logic/store' ; import { MessageState } from '../logic/messages/messagesTypes' ; import { useGetMessage } from './message/useMessage' ; import { messageByID } from '../logic/messages/messagesSelectors' ; const { ALL_MAIL } = MAILBOX_LABEL_IDS ; const cacheEntryToElement = ( cacheEntry ) => ( cacheEntry as ConversationState ) ?. Conversation || ( cacheEntry as MessageState ) ?. data || { } ; const cacheEntryIsFailedLoading = ( conversationMode , cacheEntry ) => { if ( conversationMode ) { return hasErrorType ( cacheEntry ?. errors , 'notExist' ) ; } const messageExtended = cacheEntry as MessageState ; return messageExtended ?. data ?. ID && ! messageExtended ?. data ?. Subject ; } ; export const useShouldMoveOut = ( conversationMode , ID , loading , onBack ) => { const getMessage = useGetMessage ( ) ; const getConversation = useGetConversation ( ) ; const [ folders = [ ] ] = useFolders ( ) ; const previousVersionRef = useRef < MessageState | ConversationState | undefined > ( ) ; const message = useSelector ( ( state ) => messageByID ( state , { ID : ID || '' } ) ) ; const conversation = useSelector ( ( state ) => conversationByID ( state , { ID : ID || '' } ) ) ; const onChange = ( cacheEntry ) => { if ( ! cacheEntry ) { onBack ( ) ; return ; } const previousElement = cacheEntryToElement ( previousVersionRef . current ) ; const currentElement = cacheEntryToElement ( cacheEntry ) ; const hadLabels = hasLabel ( previousElement , ALL_MAIL ) ; const previousFolderID = getCurrentFolderIDs ( previousElement , folders ) ; const currentFolderID = getCurrentFolderIDs ( currentElement , folders ) ; if ( hadLabels && previousFolderID . length > 0 && ! isDeepEqual ( previousFolderID , currentFolderID ) ) { onBack ( ) ; return ; } previousVersionRef . current = cacheEntry ; } ; useEffect ( ( ) => { if ( ID ) { if ( conversationMode ) { previousVersionRef . current = getConversation ( ID ) ; } else { previousVersionRef . current = getMessage ( ID ) ; } } else { previousVersionRef . current = undefined ; } } , [ ID ] ) ; useEffect ( ( ) => { if ( ! conversationMode ) { onChange ( getMessage ( ID || '' ) ) ; } } , [ message ] ) ; useEffect ( ( ) => { if ( conversationMode ) { onChange ( getConversation ( ID || '' ) ) ; } } , [ conversation ] ) ; useEffect ( ( ) => { if ( ! ID ) { return ; } const cacheEntry = conversationMode ? getConversation ( ID ) : getMessage ( ID ) ; if ( ! loading && cacheEntryIsFailedLoading ( conversationMode , cacheEntry ) ) { onBack ( ) ; } } , [ ID , loading ] ) ; } ;