import { createAsyncThunk } from '@reduxjs/toolkit' import { ChannelConversation } from 'api/api.types' import { ThunkAPI } from 'domains/redux/redux.types' type LastEvent = { lastEvent: { id: string; occurredAt: number } } export const getConversation = createAsyncThunk< ChannelConversation | undefined, LastEvent | { body: LastEvent }, ThunkAPI >( 'getConversation', async (_, { extra: { api }, rejectWithValue }) => { try { const response = await api.getConversation() if (!response) { throw new Error('No conversation found') } return response satisfies ChannelConversation } catch (error: any) { return rejectWithValue({ name: error?.name, message: error?.message, langKey: error?.langKey, action: error?.action, originalEvent: error?.originalEvent, originalError: error?.originalError, }) } }, { condition(payload, { getState }) { const { state: { events }, } = getState() const lastEvent = events.at(-1) const payloadLastEventId = 'lastEvent' in payload ? payload?.lastEvent?.id : payload?.body?.lastEvent?.id return lastEvent && payloadLastEventId !== lastEvent.payload.id }, }, )