Skip to content

Commit a765877

Browse files
Yerazeclaude
andauthored
fix: auto-mark incoming messages as read when viewing channel (#2316) (#2451)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 654afb2 commit a765877

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/App.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,25 +1875,27 @@ function App() {
18751875

18761876
// Unread counts polling is now handled by useUnreadCounts hook in MessagingContext
18771877

1878-
// Mark messages as read when viewing a channel
1878+
// Mark messages as read when viewing a channel — also re-fires when new messages arrive
1879+
// so that incoming messages are immediately marked as read while the user is viewing the channel.
1880+
// Without the message count dependency, new messages would show as "unread" until the user
1881+
// clicks away and back (#2316).
1882+
const currentChannelMsgCount = (channelMessages[selectedChannel] || []).length;
18791883
useEffect(() => {
18801884
if (activeTab === 'channels' && selectedChannel >= 0) {
1881-
// Mark all messages in the selected channel as read
1882-
console.log('📖 Marking channel messages as read:', selectedChannel);
1883-
logger.debug('📖 Marking channel messages as read:', selectedChannel);
18841885
markMessagesAsRead(undefined, selectedChannel);
18851886
}
1886-
}, [selectedChannel, activeTab, markMessagesAsRead]);
1887+
}, [selectedChannel, activeTab, markMessagesAsRead, currentChannelMsgCount]);
18871888

1888-
// Mark messages as read when viewing a DM conversation
1889+
// Mark messages as read when viewing a DM conversation — also re-fires on new messages
1890+
// Filter to only the selected conversation so we don't fire on messages from other DMs
1891+
const currentDMMsgCount = selectedDMNode
1892+
? messages.filter(msg => msg.fromNodeId === selectedDMNode || msg.toNodeId === selectedDMNode).length
1893+
: 0;
18891894
useEffect(() => {
18901895
if (activeTab === 'messages' && selectedDMNode) {
1891-
// Mark all DMs with the selected node as read
1892-
console.log('📖 Marking DM messages as read with node:', selectedDMNode);
1893-
logger.debug('📖 Marking DM messages as read with node:', selectedDMNode);
18941896
markMessagesAsRead(undefined, undefined, selectedDMNode);
18951897
}
1896-
}, [selectedDMNode, activeTab, markMessagesAsRead]);
1898+
}, [selectedDMNode, activeTab, markMessagesAsRead, currentDMMsgCount]);
18971899

18981900
// Handle push notification navigation (click on notification -> navigate to channel/DM and scroll to message)
18991901
useNotificationNavigationHandler(

src/hooks/useWebSocket.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ export function useWebSocket(enabled: boolean = true): WebSocketState {
236236
// Add message directly to cache - processPollData will run when cache updates
237237
// and handle timestamp conversion, notification sounds via the useEffect
238238
addMessageToCache(data);
239+
// Invalidate unread counts so badges update promptly (#2316)
240+
queryClient.invalidateQueries({ queryKey: ['unreadCounts'] });
239241
});
240242

241243
socket.on('channel:updated', (data: Channel) => {

0 commit comments

Comments
 (0)