Skip to content

Commit dc870d7

Browse files
authored
fix: properly sync own reads while offline mode is enabled (#2902)
* fix: properly sync own reads while offline mode is enabled * fix: use proper unreadCount * fix: use countUnread() api for brevity
1 parent ba07bf9 commit dc870d7

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

package/src/components/ChannelList/hooks/useCreateChannelsContext.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export const useCreateChannelsContext = <
4141
const channelValueString = channels
4242
?.map(
4343
(channel) =>
44-
`${channel.data?.name ?? ''}${channel.id ?? ''}${Object.values(channel.state.members)
44+
`${channel.data?.name ?? ''}${channel.id ?? ''}${
45+
channel?.state?.unreadCount ?? ''
46+
}${Object.values(channel.state.members)
4547
.map((member) => member.user?.online)
4648
.join()}`,
4749
)

package/src/components/ChannelPreview/hooks/useChannelPreviewData.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ export const useChannelPreviewData = <
4747
}
4848
const newUnreadCount = channel.countUnread();
4949
setUnread(newUnreadCount);
50-
// eslint-disable-next-line react-hooks/exhaustive-deps
51-
}, [channel, channelLastMessageString, channelListForceUpdate]);
50+
}, [channel, channelLastMessage, channelLastMessageString, channelListForceUpdate, lastMessage]);
5251

5352
/**
5453
* This effect listens for the `notification.mark_read` event and sets the unread count to 0

package/src/components/Chat/hooks/handleEventToSyncDB.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,36 @@ export const handleEventToSyncDB = <
9797
}
9898

9999
if (type === 'message.new') {
100-
const message = event.message;
100+
const { cid, message, user } = event;
101+
101102
if (message && (!message.parent_id || message.show_in_channel)) {
102-
return queriesWithChannelGuard((flushOverride) =>
103-
upsertMessages({
103+
return queriesWithChannelGuard((flushOverride) => {
104+
let queries = upsertMessages({
104105
flush: flushOverride,
105106
messages: [message],
106-
}),
107-
);
107+
});
108+
if (cid && client.user && client.user.id !== user?.id) {
109+
const userId = client.user.id;
110+
const channel = client.activeChannels[cid];
111+
if (channel) {
112+
const ownReads = channel.state.read[userId];
113+
const unreadCount = channel.countUnread();
114+
const upsertReadsQueries = upsertReads({
115+
cid,
116+
flush: flushOverride,
117+
reads: [
118+
{
119+
last_read: ownReads.last_read.toString() as string,
120+
unread_messages: unreadCount,
121+
user: client.user,
122+
},
123+
],
124+
});
125+
queries = [...queries, ...upsertReadsQueries];
126+
}
127+
}
128+
return queries;
129+
});
108130
}
109131
}
110132

0 commit comments

Comments
 (0)