The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { CodeViewScrollTarget } from "@pierre/diffs"; |
| 2 | import { useEffect, useState } from "react"; |
| 3 | |
| 4 | interface Viewer { |
| 5 | getHeight(): number; |
| 6 | getScrollHeight(): number; |
| 7 | } |
| 8 | |
| 9 | interface ScrollRestoreHandle { |
| 10 | getInstance(): Viewer | undefined; |
| 11 | scrollTo(target: CodeViewScrollTarget): void; |
| 12 | } |
| 13 | |
| 14 | // Session-only, like the panel's folds. |
| 15 | const scrollTopByScopeKey = new Map<string, number>(); |
| 16 | |
| 17 | /** |
| 18 | * Puts a remounted code view back where its scope was last scrolled. `attach` is the viewer's |
| 19 | * ref and `flush` its post-render handler; the position waits until the content can reach it, |
| 20 | * loading more files while any remain, and clamps once nothing is left to load. |
| 21 | */ |
| 22 | export function createCodeViewScrollRestore() { |
| 23 | let scopeKey: string | null = null; |
| 24 | let settled = true; |
| 25 | let loadMore: (() => void) | null = null; |
| 26 | let viewer: Viewer | undefined; |
| 27 | let handle: ScrollRestoreHandle | null = null; |
| 28 | let pending: number | null = null; |
| 29 | const flush = () => { |
| 30 | if (pending === null || !viewer || !handle || viewer.getHeight() === 0) return; |
| 31 | const maxScrollTop = viewer.getScrollHeight() - viewer.getHeight(); |
| 32 | if (maxScrollTop < pending && !settled) { |
| 33 | loadMore?.(); |
| 34 | return; |
| 35 | } |
| 36 | // Pierre lands an unclamped position target one sticky header (32px) above it. |
| 37 | const position = pending + 32 <= maxScrollTop ? pending + 32 : maxScrollTop; |
| 38 | pending = null; |
| 39 | if (position > 0) handle.scrollTo({ type: "position", position }); |
| 40 | }; |
| 41 | return { |
| 42 | update(nextScopeKey: string | null, nextSettled: boolean, nextLoadMore: (() => void) | null) { |
| 43 | scopeKey = nextScopeKey; |
| 44 | settled = nextSettled; |
| 45 | loadMore = nextLoadMore; |
| 46 | }, |
| 47 | attach(next: ScrollRestoreHandle | null) { |
| 48 | handle = next; |
| 49 | const instance = next?.getInstance(); |
| 50 | if (instance && instance !== viewer) { |
| 51 | viewer = instance; |
| 52 | pending = scopeKey === null ? null : (scrollTopByScopeKey.get(scopeKey) ?? null); |
| 53 | } |
| 54 | flush(); |
| 55 | }, |
| 56 | onScroll(scrollTop: number, source: object) { |
| 57 | if (source !== viewer || pending !== null || scopeKey === null) return; |
| 58 | scrollTopByScopeKey.set(scopeKey, scrollTop); |
| 59 | }, |
| 60 | /** Something else positions the viewer, such as a file reveal or collapsing every file. */ |
| 61 | discard() { |
| 62 | pending = null; |
| 63 | if (scopeKey !== null) scrollTopByScopeKey.delete(scopeKey); |
| 64 | }, |
| 65 | flush, |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | export function useCodeViewScrollRestore( |
| 70 | scopeKey: string | null, |
| 71 | settled: boolean, |
| 72 | loadMore: (() => void) | null, |
| 73 | ) { |
| 74 | const [restore] = useState(createCodeViewScrollRestore); |
| 75 | restore.update(scopeKey, settled, loadMore); |
| 76 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a loaded batch can leave the viewer's rendered items alone, so no ref or post-render call re-checks the pending position |
| 77 | useEffect(() => restore.flush()); |
| 78 | return restore; |
| 79 | } |