fix(client): share the side-card width across sessions (last drag wins)

The panel width lived in the per-session state (dsh-sidebar:v1:<id>), so a
drag in one conversation never carried to another: switching conversations
snapped the panel back to that session's own width. The width is a layout
preference, not session content, so it now persists under a cross-session
key (dsh-sidebar:v1:width):

- every persist writes the current width to the global key;
- loadState prefers the global width over a session's persisted one;
- a cache-hit session switch adopts the global width.

Backward compatible: with no global key present (first run / older data)
the behavior is unchanged. Adds a regression test covering drag-in-A ->
switch-to-B and drag-in-B -> switch-back-to-A.
This commit is contained in:
fsvov committed 2026-08-14 16:04:05 +08:00
1 parent 09de13c7bc
commit 05f67cd937
2 files changed
+93 -4

No files matched your search

+36 -1
View File
@@ -4,7 +4,7 @@ import { parseLogLines, parsePorcelainZ } from '../src/git.ts'
import { parseUnifiedDiff } from '../src/client/DiffView.tsx'
import {
activateTab, allLeaves, BOTTOM_DEFAULT, BOTTOM_MIN, closeTab, createSidebarStore, defaultWidthFor, insertLeafAt, makeDefaultState,
migrateBottomTabs, moveTab, moveTabToEdge, openDiffTab, openTabInActivePane, patchTab, reconcileAgentTerminals, resizeSplit, resizeSplitIn, sanitizeState, setBottomHeight, splitPane, tabOpenIn, toggleBottomPanel, toggleExpanded, togglePanel,
migrateBottomTabs, moveTab, moveTabToEdge, openDiffTab, openTabInActivePane, patchTab, reconcileAgentTerminals, resizeSplit, resizeSplitIn, sanitizeState, setBottomHeight, setWidth, splitPane, tabOpenIn, toggleBottomPanel, toggleExpanded, togglePanel,
type SidebarState, type SidebarTab, type SplitNode,
} from '../src/client/state.ts'
import { loadPrefs, type SidebarSettingsClient } from '../src/client/prefs.ts'
@@ -1277,6 +1277,41 @@ describe('side card preferences', () => {
expect(openStore.getSnapshot().state?.panelOpen).toBe(true)
})
it('shares the panel width across sessions (last drag wins)', () => {
// A real localStorage stub so the cross-session width actually persists
// (the default no-op mock would silently drop the write).
const storage = new Map<string, string>()
const savedWindow = (globalThis as Record<string, unknown>).window
const savedStorage = (globalThis as Record<string, unknown>).localStorage
;(globalThis as Record<string, unknown>).window = {
innerWidth: 1280,
clearTimeout: () => {},
setTimeout: () => 0,
}
;(globalThis as Record<string, unknown>).localStorage = {
getItem: (k: string) => storage.get(k) ?? null,
setItem: (k: string, v: string) => { storage.set(k, String(v)) },
removeItem: (k: string) => { storage.delete(k) },
key: () => null,
get length() { return storage.size },
}
try {
const store = createSidebarStore()
store.setSession('A')
store.reduce(s => setWidth(s, 500))
// A fresh session adopts the width dragged in A, not its own default.
store.setSession('B')
expect(store.getSnapshot().state?.width).toBe(500)
// A later drag in B carries back to the cached session A.
store.reduce(s => setWidth(s, 600))
store.setSession('A')
expect(store.getSnapshot().state?.width).toBe(600)
} finally {
;(globalThis as Record<string, unknown>).window = savedWindow
;(globalThis as Record<string, unknown>).localStorage = savedStorage
}
})
it('seeds a brand-new session COLLAPSED on narrow viewports (the panel is a full-screen drawer there)', () => {
// Stub a narrow window (the file otherwise runs without one): only
// innerWidth is read while seeding a fresh session.