From 1c806be94f0c8dae906dfe9dae1a5fdab9a4ec0b Mon Sep 17 00:00:00 2001 From: Jannis Braun <151788261+TheZwiss@users.noreply.github.com> Date: Mon, 16 Mar 2026 22:34:21 +0100 Subject: [PATCH] fix: clear message cache on reconnect to prevent stale scroll position After a server restart, navigating to a previously-visited channel showed messages at a stale middle position instead of scrolling to the bottom. The in-memory message cache survived the reconnect, so loadMessages() bailed (cache hit) and the scroll-to-bottom logic never fired. Now the ready handler clears the messages and hasMore maps for all channels belonging to the reconnecting origin (including DMs for home). The currently open channel is force-reloaded immediately; other channels get fresh-fetched on next visit, triggering proper scroll-to-bottom. Also fixes voice channel settings gear icon placement to match text channels (flex-1 pushes icon to right edge). --- .../web/src/components/voice/VoiceChannel.tsx | 6 ++-- packages/web/src/hooks/useWebSocket.ts | 30 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/web/src/components/voice/VoiceChannel.tsx b/packages/web/src/components/voice/VoiceChannel.tsx index c8837eed..6d40d936 100644 --- a/packages/web/src/components/voice/VoiceChannel.tsx +++ b/packages/web/src/components/voice/VoiceChannel.tsx @@ -138,8 +138,8 @@ export function VoiceChannel({ channelId, channelName, onClick, locked, canManag )} - {channelName} - {canManage && onSettingsClick && ( + {channelName} + {canManage && ( { e.stopPropagation(); - onSettingsClick(); + onSettingsClick?.(); }} > diff --git a/packages/web/src/hooks/useWebSocket.ts b/packages/web/src/hooks/useWebSocket.ts index a99fb0cd..6c251f7f 100644 --- a/packages/web/src/hooks/useWebSocket.ts +++ b/packages/web/src/hooks/useWebSocket.ts @@ -143,11 +143,31 @@ function handleEvent(origin: string, event: ServerEvent): void { } } - // Only force-reload the current channel on reconnect; other channels keep their cache - if (isHome) { - const { loadMessages: reloadMessages, currentChannelId } = useChatStore.getState(); - if (currentChannelId) { - reloadMessages(currentChannelId, true); + // Clear stale message cache for all channels on this origin so the next + // visit does a fresh fetch (and scroll-to-bottom fires correctly). + // Force-reload the currently open channel immediately. + { + const chatState = useChatStore.getState(); + const { channelOriginMap } = useSpaceStore.getState(); + const newMessages = new Map(chatState.messages); + const newHasMore = new Map(chatState.hasMore); + for (const [channelId, chOrigin] of channelOriginMap) { + if (chOrigin === origin) { + newMessages.delete(channelId); + newHasMore.delete(channelId); + } + } + if (isHome) { + for (const key of [...newMessages.keys()]) { + if (key.startsWith('dm-')) { + newMessages.delete(key); + newHasMore.delete(key); + } + } + } + useChatStore.setState({ messages: newMessages, hasMore: newHasMore }); + if (chatState.currentChannelId) { + chatState.loadMessages(chatState.currentChannelId, true); } }