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

timelineScrollAnchoring.ts

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