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

timelineScrollAnchoring.ts

141 lines4.7 KB

1import type { TurnId } from "@t3tools/contracts";
2​
3// Match the titlebar fade inset so draft promotion preserves the first row's position.
4export const CHAT_TIMELINE_ANCHOR_OFFSET = 24;
5​
6export type TimelineScrollMode = "following-end" | "anchoring-new-turn" | "free-scrolling";
7​
8export interface TimelineListMeasurementState {
9 readonly data: readonly unknown[];
10 readonly scroll: number;
11 readonly scrollLength: number;
12 readonly positionAtIndex: (index: number) => number | undefined;
13 readonly sizeAtIndex: (index: number) => number | undefined;
15​
16export interface AnchoredTurnMetrics {
17 readonly anchorTop: number;
18 readonly lastBottom: number;
19 readonly turnHeight: number;
20 readonly usableViewportHeight: number;
21 readonly visibleUsableBottom: number;
22 readonly overflowsUsableViewport: boolean;
23 readonly targetScrollToRevealEnd: number;
24 readonly scrollDeltaToRevealEnd: number;
26​
27export function getRowBottom(state: TimelineListMeasurementState, index: number): number | null {
28 const top = state.positionAtIndex(index);
29 const height = state.sizeAtIndex(index);
30 if (
31 typeof top !== "number" ||
32 typeof height !== "number" ||
33 !Number.isFinite(top) ||
34 !Number.isFinite(height)
35 ) {
36 return null;
37 }
38​
39 return top + Math.max(1, height);
41​
42/**
43 * Whether the timeline's real rows extend past the viewport left above the
44 * composer. The list's own content length includes the composer inset
45 * spacer, so this measures from the last row instead. Unknown row geometry
46 * or an unmeasured viewport counts as fitting.
47 */
48export function timelineContentOverflowsViewport(
49 state: TimelineListMeasurementState | undefined,
50 input: { readonly composerInset: number; readonly anchorOffset: number },
51): boolean {
52 if (!state || !state.data || state.data.length === 0) {
53 return false;
54 }
55 const scrollLength = state.scrollLength;
56 if (typeof scrollLength !== "number" || !Number.isFinite(scrollLength) || scrollLength <= 0) {
57 return false;
58 }
59 const lastBottom = getRowBottom(state, state.data.length - 1);
60 if (lastBottom === null) {
61 return false;
62 }
63 const visibleScrollLength = Math.max(0, scrollLength - input.composerInset - input.anchorOffset);
64 return lastBottom > visibleScrollLength;
66​
67export function getAnchoredTurnMetrics({
68 state,
69 anchorIndex,
70 composerOverlayHeight,
71 anchorOffset,
72}: {
73 readonly state: TimelineListMeasurementState;
74 readonly anchorIndex: number;
75 readonly composerOverlayHeight: number;
76 readonly anchorOffset: number;
77}): AnchoredTurnMetrics | null {
78 if (state.data.length === 0) {
79 return null;
80 }
81​
82 const boundedAnchorIndex = Math.max(0, Math.min(anchorIndex, state.data.length - 1));
83 const anchorTop = state.positionAtIndex(boundedAnchorIndex);
84 const lastBottom = getRowBottom(state, state.data.length - 1);
85 if (typeof anchorTop !== "number" || !Number.isFinite(anchorTop) || lastBottom === null) {
86 return null;
87 }
88​
89 const usableViewportHeight = Math.max(
90 0,
91 state.scrollLength - composerOverlayHeight - anchorOffset,
92 );
93 const turnHeight = Math.max(0, lastBottom - anchorTop);
94 const visibleUsableBottom = state.scroll + usableViewportHeight;
95 const targetScrollToRevealEnd = Math.max(0, lastBottom - usableViewportHeight);
96 const scrollDeltaToRevealEnd = Math.max(0, targetScrollToRevealEnd - state.scroll);
97​
98 return {
99 anchorTop,
100 lastBottom,
101 turnHeight,
102 usableViewportHeight,
103 visibleUsableBottom,
104 overflowsUsableViewport: turnHeight > usableViewportHeight,
105 targetScrollToRevealEnd,
106 scrollDeltaToRevealEnd,
107 };
109​
110export interface RememberedTimelinePosition {
111 readonly rowId: string;
112 readonly offsetWithinRow: number;
113 readonly scrollOffset: number;
114 readonly atEnd: boolean;
115 readonly disclosures?: {
116 readonly turns: ReadonlySet<TurnId>;
117 readonly workGroups: ReadonlySet<string>;
118 readonly spawnEntries: ReadonlySet<string>;
119 readonly reasoningMessages: ReadonlySet<string>;
120 readonly workGroupState: {
121 scrollPositions: Map<string, { readonly entryId: string; readonly offset: number }>;
122 expandedEntries: Set<string>;
123 };
124 };
126​
127// Scoped thread keys keep separate environments independent. Bound the session cache.
128const rememberedTimelinePositions = new Map<string, RememberedTimelinePosition>();
129​
130export function readTimelinePosition(threadKey: string) {
131 return rememberedTimelinePositions.get(threadKey);
133​
134export function rememberTimelinePosition(threadKey: string, position: RememberedTimelinePosition) {
135 rememberedTimelinePositions.delete(threadKey);
136 rememberedTimelinePositions.set(threadKey, position);
137 if (rememberedTimelinePositions.size > 100) {
138 const oldest = rememberedTimelinePositions.keys().next().value;
139 if (oldest !== undefined) rememberedTimelinePositions.delete(oldest);
140 }