From ad3744dcbccf9b43d72b9322bd30eb3e9ef93710 Mon Sep 17 00:00:00 2001 From: Riley Love Date: Wed, 18 Sep 2024 10:27:12 -0700 Subject: [PATCH] fix(fix referfch after archive): fix refetch after archiving the last message --- packages/react-hooks/src/inbox/reducer.ts | 15 +++++++++++++-- packages/react-hooks/src/inbox/types.ts | 5 +++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/react-hooks/src/inbox/reducer.ts b/packages/react-hooks/src/inbox/reducer.ts index 571a2cd5..bcf996c7 100644 --- a/packages/react-hooks/src/inbox/reducer.ts +++ b/packages/react-hooks/src/inbox/reducer.ts @@ -168,12 +168,20 @@ export default (state: IInbox = initialState, action?: InboxAction): IInbox => { ? [...(state?.messages ?? []), ...(action.payload?.messages ?? [])] : action.payload?.messages; + const newMessagesFiltered = newMessages?.filter((m) => { + if (state?.recentlyArchiveMessageIds?.includes(m.messageId)) { + return false; + } + + return true; + }); + return { ...state, isLoading: false, searchParams: action.meta.searchParams, lastMessagesFetched: new Date().getTime(), - messages: newMessages as IInboxMessagePreview[], + messages: newMessagesFiltered as IInboxMessagePreview[], pinned: action.payload?.appendMessages ? state.pinned : sortPinned(action.payload?.pinned, state.brand), @@ -335,6 +343,8 @@ export default (state: IInbox = initialState, action?: InboxAction): IInbox => { case INBOX_MARK_MESSAGE_ARCHIVED: { let unreadMessageCount = state.unreadMessageCount ?? 0; + const recentlyArchiveMessageIds = state.recentlyArchiveMessageIds ?? []; + recentlyArchiveMessageIds.push(action.payload.messageId); const handleArchived = (message) => { const isMatching = message.messageId === action.payload.messageId; if (isMatching && !message.read) { @@ -349,8 +359,9 @@ export default (state: IInbox = initialState, action?: InboxAction): IInbox => { return { ...state, - pinned: newPinned, messages: newMessages, + pinned: newPinned, + recentlyArchiveMessageIds, unreadMessageCount, }; } diff --git a/packages/react-hooks/src/inbox/types.ts b/packages/react-hooks/src/inbox/types.ts index 50f492fc..2ac92312 100644 --- a/packages/react-hooks/src/inbox/types.ts +++ b/packages/react-hooks/src/inbox/types.ts @@ -2,7 +2,6 @@ import { IGetInboxMessagesParams } from "@trycourier/client-graphql"; import { Brand, IInboxMessagePreview } from "@trycourier/core"; import { OnEvent } from "@trycourier/react-provider"; export interface IInbox { - tenantId?: string; brand?: Brand; from?: number; isLoading?: boolean; @@ -11,9 +10,11 @@ export interface IInbox { lastMessagesFetched?: number; messages?: Array; onEvent?: OnEvent; - searchParams?: IGetInboxMessagesParams; pinned?: Array; + recentlyArchiveMessageIds?: Array; + searchParams?: IGetInboxMessagesParams; startCursor?: string; + tenantId?: string; unreadMessageCount?: number; view?: string | "preferences"; }