diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index e623e5d9bf..4f0cfa9ee3 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -1362,6 +1362,13 @@ pageSubscribe(); showControlsSubscribe(); selectedFolderSubscribe(); + + // Clear the selected chat when leaving the chat surface (e.g. navigating + // to the admin panel), otherwise the previously-viewed chat stays selected + // in the sidebar and deleting/archiving it wrongly navigates away. + chatId.set(''); + chatTitle.set(''); + window.removeEventListener('message', onMessageHandler); $socket?.off('events', chatEventHandler); $socket?.off('connect', handleSocketConnect); diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index e4e004f461..63abd53729 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -90,6 +90,15 @@ let shiftKey = false; let selectedChatId = null; + + // Keep the optimistic sidebar highlight in sync with the active chat. Leaving the + // chat view (e.g. navigating to an admin page) clears chatId, and programmatic + // navigation such as cloning moves chatId to a different chat — in both cases the + // previously-selected item must not stay highlighted. The optimistic on-click + // highlight is preserved because a click sets selectedChatId without changing + // chatId, so this reactive only re-runs once chatId catches up to the same value. + $: selectedChatId = $chatId || null; + let showCreateChannel = false; // Pagination variables