Skip to content

Keep sidebar layout per tab - #4615

Merged
ymichael merged 1 commit into
mainfrom
bb/left-sidebar-collapse-should-be-sessionstorage-n-thr_2gic8imcix
Oct 1, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/left-sidebar-collapse-should-be-sessionstorage-n-thr_2gic8imcix

Conversation

@ymichael

@ymichael ymichael commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The left sidebar's open state (bb.sidebar.open) and width (bb.sidebar.width) were persisted with createLocalStorageSyncStorage, whose subscribe relays the browser's storage event into the backing jotai atom. A write in one tab was therefore pushed live into every other open tab and desktop window. Someone keeping several threads open in tabs, with the right panel open in some of them, would collapse the sidebar in one tab to make room — and every other tab collapsed with it. This is the same defect class as #3190 (a stale second tab reverting the root composer's project selection), which was fixed by scoping that state to the tab.

Reported in the Discord #bugs forum thread "Left sidebar collapse should be sessionStorage not localStorage" (autocole, 2026-09-30), seconded by webcoder. No GitHub issue was filed, so there is nothing for this PR to close.

What changed

apps/app/src/components/layout/AppLayout.tsx — both sidebar storages now use the existing createTabScopedStorage helper (apps/app/src/lib/browser-storage.ts) with persistInitialValue: true. No new mechanism; the same helper already backs the root composer's selection and the split layout.

Operation Behaviour
Read sessionStorage[key] → else localStorage[key] → else the default (true, 320)
Write Both, so the localStorage copy is a mirror of "most recent choice anywhere" — never a live input to an already-open tab
Cross-tab No subscribe, so no storage event can rewrite a tab's layout
First read in a tab persistInitialValue pins the value the tab started with into its own sessionStorage

The resulting behaviour:

  • Collapsing or resizing the sidebar in one tab changes only that tab.
  • A newly opened tab or window starts from the most recent choice made anywhere in that browser.
  • Reloading a tab keeps that tab's own state rather than adopting another tab's later change. This is what persistInitialValue buys — without it, "per-tab" would silently lapse on every refresh.

Sidebar width is included deliberately, not smuggled in. It is the same storage mechanism, the same user-visible surprise, and the same one-line change, and docs/configuration.md already treats the pair as one unit ("Sidebar width and open state stay in the browser because they depend on the window size"). Resizing in one tab was verified to propagate to others before this change.

Neighbouring layout state was measured and deliberately left alone. The right panel's open state (per-thread fixedPanelTabsState, which already omits subscribe) and its width percent (bb.thread.secondaryPanel.widthPercent, only read as a mount-time default by react-resizable-panels) were both checked live and neither reproduces this bug. Split layout and maximized pane already use createTabScopedStorage.

Docs: docs/configuration.md and packages/templates/src/templates/bb-guide-customization.md now state the per-tab rule in the "Client-local UI preferences" section.

No wire changes. HOST_DAEMON_PROTOCOL_VERSION is untouched and does not apply — this is client-only, with no server, daemon, schema, SDK, CLI or plugin surface involved. Storage keys and value formats are unchanged, so an existing user's saved value is read as the seed on first load after upgrade and nobody's sidebar resets; rolling back finds exactly the values the old build expects.

How you verified

Tests. Two tests added to AppLayout.test.tsx render the layout, dispatch a cross-tab storage event, and assert this tab's sidebar does not move. Confirmed to fail against the previous storage and pass after:

× keeps this tab expanded when another tab collapses the sidebar
× keeps this tab's width when another tab resizes the sidebar

sessionStorage.clear() was also added to the file's existing hooks, which only cleared localStorage.

Commands. pnpm exec turbo run test typecheck lint --filter='...[origin/main]' on the rebased head — app, server, mobile and every bundled plugin: all tests pass, typecheck clean, 0 lint errors (remaining warnings are pre-existing and none are in touched code).

Manual, in the real app. Two separate builds of the dev app — the parent commit and this change — each driven with two real browser tabs at 1280×800 with storage cleared first:

Before: collapsing in tab A collapsed tab B (ls_open:"false", B state:"collapsed"); dragging the width in tab A moved tab B to the same 404px.

After: collapsing in tab A left tab B at state:"expanded"; dragging tab B's width to 404px left tab A and tab C at their own widths (ls_width:"404" but ss_width:"320" in the untouched tabs); a newly opened tab inherited the latest collapse and width; reloading an untouched tab kept its own state.

Not verified: desktop two-window. I did not run the Electron app, and the source reading (application windows share a session partition, so one localStorage, but each renderer gets its own sessionStorage) is a hypothesis rather than evidence. It is worth exercising deliberately before relying on it.

AGENT GENERATED

The left sidebar's open state (`bb.sidebar.open`) and width
(`bb.sidebar.width`) were stored with `createLocalStorageSyncStorage`, whose
`subscribe` relays the browser's `storage` event into the jotai atom. Writing
either value in one tab therefore pushed it live into every other open tab and
desktop window, so collapsing the sidebar to make room for the right panel in
one thread's tab collapsed it everywhere.

Move both to the existing `createTabScopedStorage` helper already used by the
root composer's selection and the split layout: reads prefer this tab's
`sessionStorage` and fall back to `localStorage`, writes update both, and there
is no `subscribe`, so no cross-tab event can rewrite a tab's layout. With
`persistInitialValue`, a tab pins the value it started with, so reloading it
keeps its own view. A newly opened tab or window still starts from the most
recent choice made anywhere in that browser, via the `localStorage` mirror.

Verification: with the worktree dev app on two real browser tabs, collapsing
and resizing the sidebar in one tab leaves the other untouched; a third tab
opened afterwards inherits the latest collapse and width; reloading the
untouched tab keeps its own state. Two new `AppLayout` tests dispatch a
cross-tab `storage` event and assert this tab's sidebar does not move; both
fail against the previous storage. `turbo run test typecheck lint` passes for
@bb/app and @bb/templates.

Co-Authored-By: Claude Opus 5 <[email protected]>
@ymichael
ymichael merged commit 7255c27 into main Oct 1, 2026
44 of 46 checks passed
@ymichael
ymichael deleted the bb/left-sidebar-collapse-should-be-sessionstorage-n-thr_2gic8imcix branch October 1, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant