Skip to content

Commit

Permalink
fix(fix referfch after archive): fix refetch after archiving the last…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
rileylnapier committed Sep 18, 2024
1 parent 525cc3e commit ad3744d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions packages/react-hooks/src/inbox/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand All @@ -349,8 +359,9 @@ export default (state: IInbox = initialState, action?: InboxAction): IInbox => {

return {
...state,
pinned: newPinned,
messages: newMessages,
pinned: newPinned,
recentlyArchiveMessageIds,
unreadMessageCount,
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/react-hooks/src/inbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = IInboxMessagePreview> {
tenantId?: string;
brand?: Brand;
from?: number;
isLoading?: boolean;
Expand All @@ -11,9 +10,11 @@ export interface IInbox<T = IInboxMessagePreview> {
lastMessagesFetched?: number;
messages?: Array<T>;
onEvent?: OnEvent;
searchParams?: IGetInboxMessagesParams;
pinned?: Array<T>;
recentlyArchiveMessageIds?: Array<string>;
searchParams?: IGetInboxMessagesParams;
startCursor?: string;
tenantId?: string;
unreadMessageCount?: number;
view?: string | "preferences";
}

0 comments on commit ad3744d

Please sign in to comment.