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).
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user