Skip to content

fix(web): a remounted diff comes back on the line it was showing - #1

Open
jbearak wants to merge 1 commit into
fix/pr-code-tab-keeps-viewfrom
fix/code-view-restore-by-line
Open

jbearak wants to merge 1 commit into
fix/pr-code-tab-keeps-viewfrom
fix/code-view-restore-by-line

Conversation

@jbearak

@jbearak jbearak commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Stacked on pingdotgg#38761 in pingdotgg/t3code (fix/pr-code-tab-keeps-view). This PR lives in the fork so it shows only its own diff. Once pingdotgg#38761 merges it can be opened against upstream main.

Problem

I scrolled a long way into a pull request's Code tab, switched to another right-panel tab, and came back. The diff was open at a different line. On pingdotgg#42582 (32 files) in a 533px panel with wrapping on, line 147 of PullRequestDetailCrumbs.tsx came back as line 224.

useCodeViewScrollRestore saved a pixel offset. The viewer measures each row it draws, so while I scrolled down, that offset counted real heights. After a remount the viewer is new. It has drawn nothing above the offset, so it estimates those rows, and a wrapped line or a review card is taller than its estimate. The same offset then lands further down the file. The thread Diff tab uses the same hook and had the same problem.

Change

The hook now saves the first line under the sticky file header along with the offset, and the restore aims at that line. Pierre keeps a line target in place while it measures, so the line comes back where it was.

  • Saving: on each scroll, the hook asks the drawn file for the line at the top of the viewport and stores it as a Pierre scroll target, with the line's distance below the header.
    • Pierre reports a scroll before it draws the rows there. After a jump (a scrollbar drag, a file reveal) the drawn files are still the old ones, and the first render after it still uses estimates. So while the viewer rests at the saved offset, each render names the line again.
    • With no line under the header (a collapsed file, the end of a long one), the hook saves the next file's header if it is in view.
  • Restoring: the hook sends the line target when the file is back and one of its hunks holds that line. Otherwise it sends the offset, as before. Pierre drops a target it cannot find and any target before it, so the hook checks first instead of sending both.
  • Waiting: the hook already waits for more files while the offset is out of reach. With a saved line it waits for that line's file instead. The thread Diff tab shows a file before its patch loads, so the hook also waits while the file's cache key ends in :pending.

The pixel path is unchanged. Its unit expectations are the same as before.

Verification

  • Running app, perf(web): thread switches paint the timeline in their first frame pingdotgg/t3code#42582, 533px panel, wrapping on. I scrolled a viewport at a time, switched to another right-panel tab and back.

    Before leaving After coming back
    Without this change PullRequestDetailCrumbs.tsx line 147 line 224
    With it, 9,000px down PullRequestDetailCrumbs.tsx line 110, 7px under the header line 110, 7px, on both of two round trips
    With it, 30,000px down PullRequestDetailPanel.tsx line 1838, 11px line 1838, 11px, on both of two round trips
    With it, after a jump back up usePullRequestDetail.ts line 66, 12px line 66, 12px
    With it, thread Diff tab line 196, 12px line 196, 12px

    Switching between All commits and one commit also brought each back to its own line.

  • useCodeViewScrollRestore.browser.test.ts is new. It drives Pierre's real CodeView through its scroll subscription and onPostRender, the way the tabs wire it. Four 300-line files with every sixth line wrapping, in a 640px viewer:

    • Reading down 10,000px and remounting fails on the base (it comes back on value198 instead of value73) and passes here.
    • Jumping back up after reading down passes here. With the line saved only at scroll time it came back 18 lines off.
    • A file that lost the saved line lands on the offset.
  • useCodeViewScrollRestore.test.ts adds cases for the line target, a jump, a file shown before its patch loads, a missing line or file, and the next file's header.

  • vp run check:changed upstream/main passes. So do DiffPanel.browser.test.tsx and PullRequestCodeTab.browser.test.tsx.

  • Cost of saving: asking the viewer for the line took 0.003 to 0.013 ms per call in the browser test fixture.

No screenshots. The change is where the diff sits after a tab switch, and the table above is the evidence.

Limits

  • A line the reader reached by expanding hidden context is not in a hunk, so it falls back to the offset. The expanded context is lost on remount anyway.
  • The pull request Code tab names files by a key that includes a hash of the diff page. When the pull request changes, the saved file is gone and the restore uses the offset.
  • A large file opened with Load diff is not reopened on remount, so a line saved inside it falls back to the offset.

Reviewed with sol-loop: 3 rounds with gpt-6.2-sol. Sol verdict: approved

Created with Claude Fable 5.2 in Claude Code, running in T3 Code.

@github-actions

Copy link
Copy Markdown

This PR changes the test suite by +8 test cases (browser +3, unit +5, server 0).

New browser test files: apps/web/src/components/diffs/useCodeViewScrollRestore.browser.test.ts.
Each browser test file costs CI seconds of module loading before its first test runs; a unit test costs milliseconds.

Keep tests that would fail for a real reason, and test logic below the UI where it can be. See Testing.

The saved scroll position was a pixel offset. The old viewer measured the rows
it drew; a new one estimates the rows above the offset, and a wrapped line or a
review card is taller than its estimate. The same offset then lands further
down: on a real 32-file pull request at normal panel width, line 147 came back
as line 224.

The restore now saves the first line under the sticky header and aims at that
line, which the viewer keeps in place while it measures. It falls back to the
offset when the file or line is gone.
@jbearak
jbearak force-pushed the fix/code-view-restore-by-line branch from 37808a9 to 1285016 Compare November 25, 2026 17:07
@github-actions github-actions Bot added the vouch:trusted Author is trusted by repo permissions or the VOUCHED list. label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant