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

timelineScrollAnchoring.ts

175 lines5.8 KB

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