import { PayloadAction } from '@reduxjs/toolkit' ; import { Draft } from 'immer' ; import { hasLabel } from '../../../helpers/elements' ; import { applyLabelChangesOnMessage , LabelChanges } from '../../../helpers/labels' ; import { applyMarkAsChangesOnMessage } from '../../../helpers/message/messages' ; import { MarkAsChanges } from '../../../hooks/optimistic/useOptimisticMarkAs' ; import { getLocalID , getMessage } from '../helpers/messagesReducer' ; import { MessagesState , MessageState } from '../messagesTypes' ; export const optimisticApplyLabels = ( state , { payload : { ID , changes , unreadStatuses } , } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . data ) { messageState . data = applyLabelChangesOnMessage ( messageState . data , changes , unreadStatuses ) ; } } ; export const optimisticMarkAs = ( state , { payload : { ID , changes } } ) => { const messageState = getMessage ( state , ID ) ; if ( messageState && messageState . data ) { messageState . data = applyMarkAsChangesOnMessage ( messageState . data , changes ) ; } } ; export const optimisticDelete = ( state , { payload : IDs } ) => { IDs . forEach ( ( ID ) => { const localID = getLocalID ( state , ID ) ; delete state [ localID ] ; } ) ; } ; export const optimisticEmptyLabel = ( state , { payload : labelID } ) => { Object . entries ( state ) . forEach ( ( [ ID , message ] ) => { if ( message && message . data && hasLabel ( message . data , labelID ) ) { delete state [ ID ] ; } } ) ; } ; export const optimisticRestore = ( state , { payload : messages } ) => { messages . forEach ( ( message ) => { state [ message . localID ] = message as any ; } ) ; } ;