From ec196994184535b7301dfd6df797177a725d2d17 Mon Sep 17 00:00:00 2001 From: Ananovo Date: Sun, 16 Aug 2026 22:39:07 +0800 Subject: [PATCH] fix(web): preserve session sidebar scroll during navigation (#1552) * fix(web): preserve session sidebar scroll during navigation * fix(web): preserve nested session pane scroll * fix(web): avoid repeated file scroll restoration * fix(web): preserve scroll when leaving chat file previews * fix(web): preserve scroll after session resolution --- web/src/components/SessionChat.tsx | 16 ++++-- web/src/components/ToastContainer.tsx | 4 +- .../markdown-file-anchor.test.tsx | 1 + .../components/assistant-ui/markdown-text.tsx | 5 +- web/src/hooks/useAppGoBack.test.ts | 2 + web/src/hooks/useAppGoBack.ts | 22 ++++++-- web/src/lib/sessionNavigation.test.ts | 21 ++++++++ web/src/lib/sessionNavigation.ts | 16 ++++++ web/src/router.tsx | 48 ++++++++++++----- web/src/routes/sessions/file.test.tsx | 20 +++++++ web/src/routes/sessions/file.tsx | 54 ++++++++++++++++++- web/src/routes/sessions/files.test.tsx | 15 ++++++ web/src/routes/sessions/files.tsx | 19 +++++-- 13 files changed, 215 insertions(+), 28 deletions(-) create mode 100644 web/src/lib/sessionNavigation.test.ts create mode 100644 web/src/lib/sessionNavigation.ts diff --git a/web/src/components/SessionChat.tsx b/web/src/components/SessionChat.tsx index ce35204c..de6096af 100644 --- a/web/src/components/SessionChat.tsx +++ b/web/src/components/SessionChat.tsx @@ -1,6 +1,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { flushSync } from 'react-dom' import { useNavigate } from '@tanstack/react-router' +import { PRESERVE_SESSION_SIDEBAR_SCROLL } from '@/lib/sessionNavigation' import { AssistantRuntimeProvider, useAui, useAuiState } from '@assistant-ui/react' import { DragDropZone } from '@/components/AssistantChat/DragDropZone' import type { ApiClient } from '@/api/client' @@ -564,7 +565,11 @@ function SessionChatInner(props: SessionChatProps) { setHistoryActionPending(true) try { const result = await props.api.forkConversation(props.session.id, messageLocalId) - await navigate({ to: '/sessions/$sessionId', params: { sessionId: result.sessionId } }) + await navigate({ + to: '/sessions/$sessionId', + params: { sessionId: result.sessionId }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, + }) } finally { setHistoryActionPending(false) } @@ -1521,7 +1526,8 @@ function SessionChatInner(props: SessionChatProps) { setOutlineOpen(false) navigate({ to: '/sessions/$sessionId/files', - params: { sessionId: props.session.id } + params: { sessionId: props.session.id }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }) }, [navigate, props.session.id]) @@ -1532,7 +1538,8 @@ function SessionChatInner(props: SessionChatProps) { const handleViewTerminal = useCallback(() => { navigate({ to: '/sessions/$sessionId/terminal', - params: { sessionId: props.session.id } + params: { sessionId: props.session.id }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }) }, [navigate, props.session.id]) @@ -1727,7 +1734,8 @@ function SessionChatInner(props: SessionChatProps) { () => navigate({ to: '/sessions/$sessionId', params: { sessionId: newSessionId }, - replace: true + replace: true, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }), ) }} diff --git a/web/src/components/ToastContainer.tsx b/web/src/components/ToastContainer.tsx index 719dc86e..23896328 100644 --- a/web/src/components/ToastContainer.tsx +++ b/web/src/components/ToastContainer.tsx @@ -1,4 +1,5 @@ import { useNavigate } from '@tanstack/react-router' +import { PRESERVE_SESSION_SIDEBAR_SCROLL } from '@/lib/sessionNavigation' import { Toast } from '@/components/ui/Toast' import { useToast } from '@/lib/toast-context' @@ -26,7 +27,8 @@ export function ToastContainer() { if (toast.sessionId) { void navigate({ to: '/sessions/$sessionId', - params: { sessionId: toast.sessionId } + params: { sessionId: toast.sessionId }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }) return } diff --git a/web/src/components/assistant-ui/markdown-file-anchor.test.tsx b/web/src/components/assistant-ui/markdown-file-anchor.test.tsx index a0445d99..17454503 100644 --- a/web/src/components/assistant-ui/markdown-file-anchor.test.tsx +++ b/web/src/components/assistant-ui/markdown-file-anchor.test.tsx @@ -57,6 +57,7 @@ describe('chat file anchors', () => { path: encodeBase64(filePath), origin: 'chat', }, + resetScroll: false, }) }) }) diff --git a/web/src/components/assistant-ui/markdown-text.tsx b/web/src/components/assistant-ui/markdown-text.tsx index 5fa44a31..2e7ff78f 100644 --- a/web/src/components/assistant-ui/markdown-text.tsx +++ b/web/src/components/assistant-ui/markdown-text.tsx @@ -15,6 +15,7 @@ import rehypeKatex from 'rehype-katex' import remarkDisableIndentedCode from '@/lib/remark-disable-indented-code' import remarkRepairTables from '@/lib/remark-repair-tables' import { useNavigate } from '@tanstack/react-router' +import { PRESERVE_SESSION_SIDEBAR_SCROLL } from '@/lib/sessionNavigation' import remarkStripCjkAutolink from '@/lib/remark-strip-cjk-autolink' import remarkNonHttpsAutolink from '@/lib/remark-non-https-autolink' import { cn, encodeBase64 } from '@/lib/utils' @@ -520,7 +521,8 @@ function FilePathAnchor(props: ComponentPropsWithoutRef<'a'> & { filePath: strin search: { path: encodeBase64(filePath), origin: 'chat', - } + }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }) } @@ -550,6 +552,7 @@ function SessionPathAnchor(props: ComponentPropsWithoutRef<'a'> & { targetSessio void navigate({ to: '/sessions/$sessionId', params: { sessionId: props.targetSessionId }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }) } diff --git a/web/src/hooks/useAppGoBack.test.ts b/web/src/hooks/useAppGoBack.test.ts index fda57812..6e814dc0 100644 --- a/web/src/hooks/useAppGoBack.test.ts +++ b/web/src/hooks/useAppGoBack.test.ts @@ -72,6 +72,7 @@ describe('useAppGoBack file preview navigation', () => { expect(routerMocks.navigate).toHaveBeenCalledWith({ to: '/sessions/session-1', + resetScroll: false, }) }) @@ -92,6 +93,7 @@ describe('useAppGoBack file preview navigation', () => { tab: 'directories', query: 'readme', }, + resetScroll: false, }) }) }) diff --git a/web/src/hooks/useAppGoBack.ts b/web/src/hooks/useAppGoBack.ts index d3b217b7..fe61e311 100644 --- a/web/src/hooks/useAppGoBack.ts +++ b/web/src/hooks/useAppGoBack.ts @@ -1,5 +1,6 @@ import { useCallback } from 'react' import { useLocation, useNavigate, useRouter } from '@tanstack/react-router' +import { PRESERVE_SESSION_SIDEBAR_SCROLL } from '@/lib/sessionNavigation' export function getSettingsBackTarget(pathname: string): string | null { if (pathname === '/settings') return '/sessions' @@ -32,7 +33,10 @@ export function useAppGoBack(): () => void { return useCallback(() => { // Use explicit path navigation for consistent behavior across all environments if (pathname === '/sessions/new') { - navigate({ to: '/sessions' }) + navigate({ + to: '/sessions', + ...PRESERVE_SESSION_SIDEBAR_SCROLL, + }) return } @@ -50,19 +54,29 @@ export function useAppGoBack(): () => void { ? (search as { origin?: unknown }).origin : undefined if (origin === 'chat') { - navigate({ to: pathname.replace(/\/file$/, '') }) + navigate({ + to: pathname.replace(/\/file$/, ''), + ...PRESERVE_SESSION_SIDEBAR_SCROLL, + }) return } const filesPath = pathname.replace(/\/file$/, '/files') - navigate({ to: filesPath, search: getSessionFilesBackSearch(search) }) + navigate({ + to: filesPath, + search: getSessionFilesBackSearch(search), + ...PRESERVE_SESSION_SIDEBAR_SCROLL, + }) return } // For session routes, navigate to parent path if (pathname.startsWith('/sessions/')) { const parentPath = pathname.replace(/\/[^/]+$/, '') || '/sessions' - navigate({ to: parentPath }) + navigate({ + to: parentPath, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, + }) return } diff --git a/web/src/lib/sessionNavigation.test.ts b/web/src/lib/sessionNavigation.test.ts new file mode 100644 index 00000000..5e077d72 --- /dev/null +++ b/web/src/lib/sessionNavigation.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest' +import { + getSessionListSelectionNavigation, + PRESERVE_SESSION_SIDEBAR_SCROLL, +} from './sessionNavigation' + +describe('PRESERVE_SESSION_SIDEBAR_SCROLL', () => { + it('skips router-wide scroll restoration for internal session navigation', () => { + expect(PRESERVE_SESSION_SIDEBAR_SCROLL).toEqual({ resetScroll: false }) + }) +}) + +describe('getSessionListSelectionNavigation', () => { + it('preserves the shared sidebar scroll position when selecting a session', () => { + expect(getSessionListSelectionNavigation('session-active')).toEqual({ + to: '/sessions/$sessionId', + params: { sessionId: 'session-active' }, + resetScroll: false, + }) + }) +}) diff --git a/web/src/lib/sessionNavigation.ts b/web/src/lib/sessionNavigation.ts new file mode 100644 index 00000000..52e4458b --- /dev/null +++ b/web/src/lib/sessionNavigation.ts @@ -0,0 +1,16 @@ +// The /sessions layout keeps its sidebar mounted while its child route changes. +// TanStack Router restores every scrollable element by default, so a cached child +// route can otherwise overwrite the sidebar's current scrollTop. The child panes +// own their own scroll state; internal workspace navigation should skip the +// router-wide restoration pass. +export const PRESERVE_SESSION_SIDEBAR_SCROLL = { + resetScroll: false, +} as const + +export function getSessionListSelectionNavigation(sessionId: string) { + return { + to: '/sessions/$sessionId' as const, + params: { sessionId }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, + } +} diff --git a/web/src/router.tsx b/web/src/router.tsx index e3cc311f..7cf2361c 100644 --- a/web/src/router.tsx +++ b/web/src/router.tsx @@ -13,6 +13,10 @@ import { useSearch, } from '@tanstack/react-router' import { getScrollRestorationKey } from '@/lib/scrollRestorationKey' +import { + getSessionListSelectionNavigation, + PRESERVE_SESSION_SIDEBAR_SCROLL, +} from '@/lib/sessionNavigation' import { App } from '@/App' import { SessionChat } from '@/components/SessionChat' import { SessionList } from '@/components/SessionList' @@ -215,7 +219,8 @@ function SessionsPage() { to: '/sessions/new', search: args.machineId ? { directory: args.directory, machineId: args.machineId } - : { directory: args.directory } + : { directory: args.directory }, + ...PRESERVE_SESSION_SIDEBAR_SCROLL, }) }, [navigate]) @@ -236,11 +241,11 @@ function SessionsPage() { key={initializedHub === baseUrl ? 'last-seen-ready' : 'last-seen-pending'} sessions={sessions} selectedSessionId={selectedSessionId} - onSelect={(sessionId) => navigate({ - to: '/sessions/$sessionId', - params: { sessionId }, + onSelect={(sessionId) => navigate(getSessionListSelectionNavigation(sessionId))} + onNewSession={() => navigate({ + to: '/sessions/new', + ...PRESERVE_SESSION_SIDEBAR_SCROLL, })} - onNewSession={() => navigate({ to: '/sessions/new' })} onNewSessionInDirectory={handleNewSessionInDirectory} onBrowse={canBrowse ? () => navigate({ to: '/browse' }) : undefined} onRefresh={handleRefresh} @@ -268,7 +273,10 @@ function SessionsPage() {