Skip to content

Commit

Permalink
fix: properly sync own reads while offline mode is enabled (#2901)
Browse files Browse the repository at this point in the history
* fix: properly sync own reads while offline mode is enabled

* fix: use proper unreadCount

* fix: use countUnread() api for brevity
  • Loading branch information
isekovanic authored Jan 23, 2025
1 parent d69b2f6 commit 4df41c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const useCreateChannelsContext = <
const channelValueString = channels
?.map(
(channel) =>
`${channel.data?.name ?? ''}${channel.id ?? ''}${Object.values(channel.state.members)
`${channel.data?.name ?? ''}${channel.id ?? ''}${
channel?.state?.unreadCount ?? ''
}${Object.values(channel.state.members)
.map((member) => member.user?.online)
.join()}`,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export const useChannelPreviewData = <
}
const newUnreadCount = channel.countUnread();
setUnread(newUnreadCount);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [channel, channelLastMessageString, channelListForceUpdate]);
}, [channel, channelLastMessage, channelLastMessageString, channelListForceUpdate, lastMessage]);

/**
* This effect listens for the `notification.mark_read` event and sets the unread count to 0
Expand Down
32 changes: 27 additions & 5 deletions package/src/components/Chat/hooks/handleEventToSyncDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,37 @@ export const handleEventToSyncDB = async <
}

if (type === 'message.new') {
const message = event.message;
const { cid, message, user } = event;

if (message && (!message.parent_id || message.show_in_channel)) {
return await queriesWithChannelGuard((flushOverride) =>
upsertMessages({
return await queriesWithChannelGuard(async (flushOverride) => {
let queries = await upsertMessages({
flush: flushOverride,
messages: [message],
}),
);
});
if (cid && client.user && client.user.id !== user?.id) {
const userId = client.user.id;
const channel = client.activeChannels[cid];
if (channel) {
const ownReads = channel.state.read[userId];
const unreadCount = channel.countUnread();
const upsertReadsQueries = await upsertReads({
cid,
flush: flushOverride,
reads: [
{
last_read: ownReads.last_read.toString() as string,
last_read_message_id: ownReads.last_read_message_id,
unread_messages: unreadCount,
user: client.user,
},
],
});
queries = [...queries, ...upsertReadsQueries];
}
}
return queries;
});
}
}

Expand Down

0 comments on commit 4df41c4

Please sign in to comment.