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

timelineScrollAnchoring.ts

179 lines5.6 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​
61export function 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 geometry 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 lastBottom = getRowBottom(state, state.data.length - 1);
93 if (lastBottom === null) {
94 return false;
95 }
96 const visibleScrollLength = Math.max(0, scrollLength - input.composerInset - input.anchorOffset);
97 return lastBottom > visibleScrollLength;
99​
100export function getAnchoredTurnMetrics({
101 state,
102 anchorIndex,
103 composerOverlayHeight,
104 anchorOffset,
105}: {
106 readonly state: TimelineListMeasurementState;
107 readonly anchorIndex: number;
108 readonly composerOverlayHeight: number;
109 readonly anchorOffset: number;
110}): AnchoredTurnMetrics | null {
111 if (state.data.length === 0) {
112 return null;
113 }
114​
115 const boundedAnchorIndex = Math.max(0, Math.min(anchorIndex, state.data.length - 1));
116 const anchorTop = state.positionAtIndex(boundedAnchorIndex);
117 const lastBottom = getRowBottom(state, state.data.length - 1);
118 if (typeof anchorTop !== "number" || !Number.isFinite(anchorTop) || lastBottom === null) {
119 return null;
120 }
121​
122 const usableViewportHeight = Math.max(
123 0,
124 state.scrollLength - composerOverlayHeight - anchorOffset,
125 );
126 const turnHeight = Math.max(0, lastBottom - anchorTop);
127 const visibleUsableBottom = state.scroll + usableViewportHeight;
128 const targetScrollToRevealEnd = Math.max(0, lastBottom - usableViewportHeight);
129 const scrollDeltaToRevealEnd = Math.max(0, targetScrollToRevealEnd - state.scroll);
130​
131 return {
132 anchorTop,
133 lastBottom,
134 turnHeight,
135 usableViewportHeight,
136 visibleUsableBottom,
137 overflowsUsableViewport: turnHeight > usableViewportHeight,
138 targetScrollToRevealEnd,
139 scrollDeltaToRevealEnd,
140 };
142​
143export interface RememberedTimelinePosition {
144 readonly rowId: string;
145 readonly offsetWithinRow: number;
146 readonly scrollOffset: number;
147 readonly atEnd: boolean;
148 readonly disclosures?: {
149 readonly runs: ReadonlySet<RunId>;
150 readonly workGroups: ReadonlySet<string>;
151 readonly attempts: ReadonlySet<RunAttemptId>;
152 readonly workGroupState: {
153 scrollPositions: Map<string, { readonly entryId: string; readonly offset: number }>;
154 expandedEntries: Set<string>;
155 };
156 };
158​
159const rememberedTimelinePositions = new Map<string, RememberedTimelinePosition>();
160​
161export function readTimelinePosition(threadKey: string) {
162 return rememberedTimelinePositions.get(threadKey);
164​
165export function rememberTimelinePosition(
166 threadKey: string,
167 position: RememberedTimelinePosition,
168 liveFollowEnabled: boolean,
169) {
170 rememberedTimelinePositions.delete(threadKey);
171 rememberedTimelinePositions.set(threadKey, {
172 ...position,
173 atEnd: liveFollowEnabled || position.atEnd,
174 });
175 if (rememberedTimelinePositions.size > 100) {
176 const oldest = rememberedTimelinePositions.keys().next().value;
177 if (oldest !== undefined) rememberedTimelinePositions.delete(oldest);
178 }