Skip to content

Commit

Permalink
fix: show unread messages indicators for target channel only (#2597)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela authored Jan 10, 2025
1 parent b9eb846 commit 2af5f0a
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/components/MessageList/hooks/useMarkRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useMarkRead = <
}: UseMarkReadParams) => {
const { client } = useChatContext<StreamChatGenerics>('useMarkRead');
const { markRead, setChannelUnreadUiState } = useChannelActionContext('useMarkRead');
const { channel } = useChannelStateContext('useMarkRead');
const { channel } = useChannelStateContext<StreamChatGenerics>('useMarkRead');
const previousRenderMessageListScrolledToBottom = useRef(isMessageListScrolledToBottom);

useEffect(() => {
Expand All @@ -51,7 +51,6 @@ export const useMarkRead = <
};

const handleMessageNew = (event: Event<StreamChatGenerics>) => {
const newMessageToCurrentChannel = event.cid === channel.cid;
const isOwnMessage = event.user?.id && event.user.id === client.user?.id;
const mainChannelUpdated = !event.message?.parent_id || event.message?.show_in_channel;
if (isOwnMessage) return;
Expand All @@ -72,16 +71,12 @@ export const useMarkRead = <
unread_messages: previousUnreadCount + 1,
};
});
} else if (
newMessageToCurrentChannel &&
mainChannelUpdated &&
shouldMarkRead(channel.countUnread())
) {
} else if (mainChannelUpdated && shouldMarkRead(channel.countUnread())) {
markRead();
}
};

client.on('message.new', handleMessageNew);
channel.on('message.new', handleMessageNew);
document.addEventListener('visibilitychange', onVisibilityChange);

const hasScrolledToBottom =
Expand All @@ -92,7 +87,7 @@ export const useMarkRead = <
previousRenderMessageListScrolledToBottom.current = isMessageListScrolledToBottom;

return () => {
client.off('message.new', handleMessageNew);
channel.off('message.new', handleMessageNew);
document.removeEventListener('visibilitychange', onVisibilityChange);
};
}, [
Expand Down

0 comments on commit 2af5f0a

Please sign in to comment.