From 00eac15fa1bf458ae78b0c678f7acf5b4c931f3c Mon Sep 17 00:00:00 2001 From: Wes Date: Wed, 25 Mar 2026 14:27:58 -0700 Subject: [PATCH] fix(desktop): refresh DM sidebar in real-time when new messages arrive When a WebSocket event arrived for a DM channel not yet in the sidebar, the event was silently dropped because the channel wasn't in the known set (liveChannelIds). This meant new DMs only appeared after the 60s polling interval or an app restart. Now, when a message arrives for an unknown channel, the channels query is immediately invalidated so the sidebar refetches and picks up the new DM. Also invalidates on relay reconnect to catch anything missed during connection drops. Co-Authored-By: Claude Opus 4.6 --- .../features/channels/useUnreadChannels.ts | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/desktop/src/features/channels/useUnreadChannels.ts b/desktop/src/features/channels/useUnreadChannels.ts index e08f6ffa4b1..5d4bc0ac1d5 100644 --- a/desktop/src/features/channels/useUnreadChannels.ts +++ b/desktop/src/features/channels/useUnreadChannels.ts @@ -1,7 +1,10 @@ import * as React from "react"; import { useQueryClient } from "@tanstack/react-query"; -import { updateChannelLastMessageAt } from "@/features/channels/hooks"; +import { + channelsQueryKey, + updateChannelLastMessageAt, +} from "@/features/channels/hooks"; import { getChannelIdFromTags } from "@/features/messages/lib/threading"; import { mergeTimelineCacheMessages } from "@/features/messages/hooks"; import { relayClient } from "@/shared/api/relayClient"; @@ -180,11 +183,12 @@ export function useUnreadChannels( const handleIncomingMessage = React.useEffectEvent((event: RelayEvent) => { const channelId = getChannelIdFromTags(event.tags); - if ( - !channelId || - channelId === activeChannelId || - !liveChannelIds.has(channelId) - ) { + if (!channelId || channelId === activeChannelId) { + return; + } + + if (!liveChannelIds.has(channelId)) { + void queryClient.invalidateQueries({ queryKey: channelsQueryKey }); return; } @@ -203,6 +207,12 @@ export function useUnreadChannels( ); }); + React.useEffect(() => { + return relayClient.subscribeToReconnects(() => { + void queryClient.invalidateQueries({ queryKey: channelsQueryKey }); + }); + }, [queryClient]); + React.useEffect(() => { if (liveChannelIds.size === 0) { return;