Skip to content

Code review (git-diff) panel loses scroll position when switching secondary-panel tabs #6162

Open

Description

@kolsha

Summary

The git-diff ("code review") secondary-panel tab discards its scroll position whenever you switch to another tab and back, dropping reviewers at the top of a long diff. The fix is small and mirrors a pattern BB already ships for file-preview tabs — a prototype with tests is ready (linked below). Opening this first per CONTRIBUTING, since it touches UI.

Context

In the thread's secondary panel you can flip between the git-diff review tab and other tabs (thread info, file previews, terminals). File-preview tabs remember where you scrolled to when you come back. The git-diff tab does not: returning to a review you were halfway through snaps you back to the first file. On a large changeset this means re-scrolling and re-finding your place every time you glance at another tab.

Steps to reproduce

  1. Open a thread whose environment has a sizable git diff (enough to scroll).
  2. Open the secondary panel's git-diff ("code review") tab.
  3. Scroll partway down the diff.
  4. Switch to another panel tab (e.g. thread info or a file preview).
  5. Switch back to the git-diff tab.

Expected: the diff is still scrolled to where you left it (as file-preview tabs already behave).

Actual: the diff is scrolled back to the top.

Root cause

The git-diff tab is a fixed tab: it renders in a different branch from file-preview tabs and unmounts on tab switch, so its virtualized scroll container (DiffFilesPanel) is recreated from scratch.

  • File-preview tabs persist scroll via a scoped position map and FilePreviewScrollPositionContext (apps/app/src/components/secondary-panel/ThreadSecondaryPanel.tsx, ~L115-128, L723-734, L844-852; restore pattern in FilePreview.tsx L1041-1053).
  • The git-diff fixed-tab branch (ThreadSecondaryPanel.tsx L854-866) wraps content only in ThreadDiffContentContext — there is no scroll-position persistence.

Proposed fix

Persist the git-diff scroll offset the same way the panel already persists per-file collapse state: in diffFilesStore via a jotai atomFamily keyed by diffIdentity. DiffFilesPanel restores the offset on remount in a layout effect (setting scrollTop, matching the file-preview precedent) and saves it on scroll. A pending deep-link scroll target (scrollToPath) still takes precedence, and stale offsets are pruned on diff-identity change alongside the existing card-state cleanup.

Note: initialOffset on the virtualizer is not enough here — for an element-scoped virtualizer observeElementOffset reads the real scrollTop (0) on mount and overwrites it, so a layout-effect restore is used instead.

Touches 3 files (+32/-3) plus a test. Prototype branch: https://github.com/kolsha/bb/tree/feat/git-diff-scroll-persist

Verification

  • New behavior test DiffFilesPanel.scrollPersist.test.tsx (restore across unmount/remount, per-identity isolation, cleanup, deep-link precedence); confirmed it fails without the fix.
  • git-diff + secondary-panel suites green (58/58), typecheck and oxlint clean.

Ruled out

  • A plugin (experimental_diffRenderer) — it replaces per-file diff bodies, not the panel's scroll container or tab lifecycle, so it cannot address this.
  • Keeping the tab mounted (display:none) — larger architectural change and against the panel's current unmount-based pattern, which file previews already work around with the scoped position map.

Environment: bb 0.46.1 (reproduces on current main).

Activity

  1. added theissue type on Oct 23, 2026
  2. added
    ui
    confirmed-repro
    on Oct 23, 2026
  3. bb-slop-cop commented on Oct 23, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority Medium · Effort Low · ui.
    • Reproduction and root-cause report: REPRODUCED, high confidence; repeated in a second clean checkout of trusted origin/main.
    • Root cause: switching tabs unmounts the virtualized diff scroll container without retaining its viewport offset.
    • Reproduction label: confirmed-repro.
    • No PR: a local candidate passed 111 tests but still shifted a deeper real-browser position from 6000px to 5172px. The unsafe fix was stopped and not pushed.

    AGENT GENERATED

  4. kolsha commented on Oct 23, 2026

    @kolsha
    Author

    Update: pivoted from scroll-restore to an opt-in keep-alive experiment

    The original "persist and restore the scroll offset" approach turned out to be the wrong shape for this panel, confirmed by testing:

    • The diff list is virtualized with lazily loaded, dynamically measured cards. Restoring a saved scrollTop drifts once off-screen rows re-measure after the switch — the position lands in the wrong place.
    • Seeding the virtualizer's measurementsCache + initialOffset reduced but did not eliminate the drift on real diffs.
    • Selection can't be restored this way at all — it's live DOM/selection state destroyed on unmount.

    The robust fix is to not tear the panel down. Reworked prototype:

    • Keep the git-diff panel mounted when another secondary-panel tab is active, hidden via visibility (not display:none, which resets scrollTop) so scroll position and already-loaded diffs survive the switch.
    • Gated behind a new off-by-default reviewTabKeepAlive experiment (Settings → Experiments), so default behavior is unchanged and this is pure opt-in.
    • Scoped to the pane that owns the git-diff tab (works with the single-pane path, which runs through SidebarSplitContainer).

    Clean prototype (force-pushed, restore approach dropped from history): https://github.com/kolsha/bb/tree/feat/git-diff-scroll-persist — 4 files, +35/-2 (ThreadSecondaryPanel.tsx keep-alive layer + the experiment in domain, system-config fallback, and the Experiments settings UI). typecheck/lint clean. Verified working on a local build against a copy of real data.

    Open question for maintainers: the secondary panel unmounts inactive tabs deliberately (memory/perf with many tabs, browser views, terminals). Is an opt-in keep-alive for just the git-diff tab an acceptable direction, or would you prefer a different mechanism? Happy to adjust before this becomes a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproui

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions