Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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({
Expand All @@ -343,6 +356,7 @@ const ChannelInner = (
activeUnreadHandler,
channel,
channelConfig,
client,
doMarkReadRequest,
setChannelUnreadUiState,
t,
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 5 additions & 10 deletions src/components/ChannelListItem/ChannelListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
6 changes: 5 additions & 1 deletion src/components/MessageList/hooks/useMarkRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down