Skip to content

Commit 9ca4c35

Browse files
committed
fix: properly sync own reads while offline mode is enabled
1 parent d69b2f6 commit 9ca4c35

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-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: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,33 @@ export const handleEventToSyncDB = async <
120120
}
121121

122122
if (type === 'message.new') {
123-
const message = event.message;
123+
const { cid, message, user } = event;
124124

125125
if (message && (!message.parent_id || message.show_in_channel)) {
126-
return await queriesWithChannelGuard((flushOverride) =>
127-
upsertMessages({
126+
return await queriesWithChannelGuard(async (flushOverride) => {
127+
let queries = await upsertMessages({
128128
flush: flushOverride,
129129
messages: [message],
130-
}),
131-
);
130+
});
131+
if (cid && client.user && client.user.id !== user?.id) {
132+
const userId = client.user.id;
133+
const ownReads = client.activeChannels[cid]?.state.read[userId];
134+
const upsertReadsQueries = await upsertReads({
135+
cid,
136+
flush: flushOverride,
137+
reads: [
138+
{
139+
last_read: ownReads.last_read.toString() as string,
140+
last_read_message_id: ownReads.last_read_message_id,
141+
unread_messages: ownReads?.unread_messages,
142+
user: client.user,
143+
},
144+
],
145+
});
146+
queries = [...queries, ...upsertReadsQueries];
147+
}
148+
return queries;
149+
});
132150
}
133151
}
134152

0 commit comments

Comments
 (0)