Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useCodeViewScrollRestore.ts

97 lines3.7 KB

1import type { CodeViewScrollTarget } from "@pierre/diffs";
2import { useEffect, useState } from "react";
3​
4interface Viewer {
5 getHeight(): number;
6 getScrollHeight(): number;
7 getRenderedItems(): ReadonlyArray<{ id: string }>;
8 getTopForItem(id: string): number | undefined;
9}
10​
11interface ScrollRestoreHandle {
12 getInstance(): Viewer | undefined;
13 scrollTo(target: CodeViewScrollTarget): void;
15​
16// Session-only, like the panel's folds.
17const scrollTopByScopeKey = new Map<string, number>();
18​
19/**
20 * Puts a remounted code view back where its scope was last scrolled. `attach` is the viewer's
21 * ref and `flush` its post-render handler; the position waits until the content can reach it,
22 * loading more files while any remain, and clamps once nothing is left to load.
23 */
24export function createCodeViewScrollRestore() {
25 let scopeKey: string | null = null;
26 let settled = true;
27 let loadMore: (() => void) | null = null;
28 let viewer: Viewer | undefined;
29 let handle: ScrollRestoreHandle | null = null;
30 let pending: number | null = null;
31 // The user scrolled first: the saved position still loads its files but no longer moves them.
32 let userScrolled = false;
33 const flush = () => {
34 if (pending === null || !viewer || !handle || viewer.getHeight() === 0) return;
35 const maxScrollTop = viewer.getScrollHeight() - viewer.getHeight();
36 if (maxScrollTop < pending && !settled) {
37 loadMore?.();
38 return;
39 }
40 // Pierre lowers an in-range position target by the sticky header, so the last header's
41 // height is unreachable; an item target offset back to the saved position is not lowered.
42 if (userScrolled || pending <= 0) {
43 pending = null;
44 return;
45 }
46 const anchor = viewer.getRenderedItems()[0];
47 const anchorTop = anchor && viewer.getTopForItem(anchor.id);
48 if (!anchor || anchorTop === undefined) return;
49 const offset = anchorTop - pending;
50 pending = null;
51 handle.scrollTo({ type: "item", id: anchor.id, align: "start", offset });
52 };
53 return {
54 update(nextScopeKey: string | null, nextSettled: boolean, nextLoadMore: (() => void) | null) {
55 scopeKey = nextScopeKey;
56 settled = nextSettled;
57 loadMore = nextLoadMore;
58 },
59 attach(next: ScrollRestoreHandle | null) {
60 handle = next;
61 const instance = next?.getInstance();
62 if (instance && instance !== viewer) {
63 viewer = instance;
64 pending = scopeKey === null ? null : (scrollTopByScopeKey.get(scopeKey) ?? null);
65 userScrolled = false;
66 }
67 flush();
68 },
69 onScroll(scrollTop: number, source: object) {
70 if (source !== viewer || scopeKey === null) return;
71 // A remounted viewer reports the top before anyone scrolls; only later events are the user's.
72 if (pending !== null && !userScrolled && scrollTop === 0) return;
73 if (pending !== null) userScrolled = true;
74 if (scrollTop === 0) scrollTopByScopeKey.delete(scopeKey);
75 else scrollTopByScopeKey.set(scopeKey, scrollTop);
76 },
77 /** Something else positions the viewer, such as a file reveal or collapsing every file. */
78 discard() {
79 pending = null;
80 userScrolled = false;
81 if (scopeKey !== null) scrollTopByScopeKey.delete(scopeKey);
82 },
83 flush,
84 };
86​
87export function useCodeViewScrollRestore(
88 scopeKey: string | null,
89 settled: boolean,
90 loadMore: (() => void) | null,
91) {
92 const [restore] = useState(createCodeViewScrollRestore);
93 restore.update(scopeKey, settled, loadMore);
94 // 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
95 useEffect(() => restore.flush());
96 return restore;