diff --git a/src/components/Channel/Channel.tsx b/src/components/Channel/Channel.tsx index e070a39ea..668f21743 100644 --- a/src/components/Channel/Channel.tsx +++ b/src/components/Channel/Channel.tsx @@ -302,7 +302,20 @@ const ChannelInner = ( throttle( async (options?: MarkReadWrapperOptions) => { const { updateChannelUiUnreadState = true } = options ?? {}; - if (channel.disconnected || !channelConfig?.read_events) { + if (channel.disconnected) return; + + if (!channelConfig?.read_events && client.options.isLocalUnreadCountEnabled) { + const event = channel.markReadLocally(); + + if (updateChannelUiUnreadState && event) { + lastRead.current = new Date(); + _setChannelUnreadUiState({ + last_read: lastRead.current, + last_read_message_id: event.last_read_message_id, + unread_messages: 0, + }); + } + return; } @@ -316,7 +329,7 @@ const ChannelInner = ( ); } else { const markReadResponse = await channel.markRead(); - // markReadResponse.event can be null in case of a user that is not a member of a channel being marked read + // markReadResponse.event can be null in case of a user that is not a member of a channel being marked read // in that case event is null and we should not set unread UI if (updateChannelUiUnreadState && markReadResponse?.event) { _setChannelUnreadUiState({ @@ -343,6 +356,7 @@ const ChannelInner = ( activeUnreadHandler, channel, channelConfig, + client, doMarkReadRequest, setChannelUnreadUiState, t, @@ -381,7 +395,7 @@ const ChannelInner = ( if (mainChannelUpdated) { if ( document.hidden && - channelConfig?.read_events && + (channelConfig?.read_events || client.options.isLocalUnreadCountEnabled) && !channel.muteStatus().muted ) { const unread = channel.countUnread(lastRead.current); diff --git a/src/components/ChannelListItem/ChannelListItem.tsx b/src/components/ChannelListItem/ChannelListItem.tsx index 259f5cf57..dd94eb392 100644 --- a/src/components/ChannelListItem/ChannelListItem.tsx +++ b/src/components/ChannelListItem/ChannelListItem.tsx @@ -117,25 +117,20 @@ export const ChannelListItem = (props: ChannelListItemProps) => { typeof active === 'undefined' ? activeChannel?.cid === channel.cid : active; const { muted } = useIsChannelMuted(channel); - useEffect(() => { - const handleEvent = (event: Event) => { - if (!event.cid) return setUnread(0); - if (channel.cid === event.cid) setUnread(0); - }; - - client.on('notification.mark_read', handleEvent); - return () => client.off('notification.mark_read', handleEvent); - }, [channel, client]); - useEffect(() => { const handleEvent = (event: Event) => { if (channel.cid !== event.cid) return; if (event.user?.id !== client.user?.id) return; setUnread(channel.countUnread()); }; + + client.on('notification.mark_read', handleEvent); channel.on('notification.mark_unread', handleEvent); + channel.on('message.read_locally', handleEvent); return () => { + client.off('notification.mark_read', handleEvent); channel.off('notification.mark_unread', handleEvent); + channel.off('message.read_locally', handleEvent); }; }, [channel, client]); diff --git a/src/components/ChannelListItem/hooks/useMessageDeliveryStatus.ts b/src/components/ChannelListItem/hooks/useMessageDeliveryStatus.ts index 92f98a426..cfbc65d98 100644 --- a/src/components/ChannelListItem/hooks/useMessageDeliveryStatus.ts +++ b/src/components/ChannelListItem/hooks/useMessageDeliveryStatus.ts @@ -92,10 +92,12 @@ export const useMessageDeliveryStatus = ({ channel.on('message.delivered', handleMessageDelivered); channel.on('message.read', handleMarkRead); + channel.on('message.read_locally', handleMarkRead); return () => { channel.off('message.delivered', handleMessageDelivered); channel.off('message.read', handleMarkRead); + channel.off('message.read_locally', handleMarkRead); }; }, [channel, client, isOwnMessage, lastMessage]); diff --git a/src/components/MessageList/hooks/useMarkRead.ts b/src/components/MessageList/hooks/useMarkRead.ts index 5307f562a..e67e0f2ee 100644 --- a/src/components/MessageList/hooks/useMarkRead.ts +++ b/src/components/MessageList/hooks/useMarkRead.ts @@ -37,7 +37,11 @@ export const useMarkRead = ({ const { channel } = useChannelStateContext('useMarkRead'); useEffect(() => { - if (!channel.getConfig()?.read_events) return; + const unreadNotificationSupported = + channel.getConfig()?.read_events || client.options.isLocalUnreadCountEnabled; + + if (!unreadNotificationSupported) return; + const shouldMarkRead = () => !document.hidden && !wasMarkedUnread &&