Open History to see the last commit
1// Match the titlebar fade inset so draft promotion preserves the first row's position.
2export const CHAT_TIMELINE_ANCHOR_OFFSET = 24;
3
4export type TimelineScrollMode = "following-end" | "anchoring-new-turn" | "free-scrolling";
5
6export interface TimelineListMeasurementState {
7 readonly data: readonly unknown[];
8 readonly scroll: number;
9 readonly scrollLength: number;
10 readonly positionAtIndex: (index: number) => number | undefined;
11 readonly sizeAtIndex: (index: number) => number | undefined;
12}
13
14export interface AnchoredTurnMetrics {
15 readonly anchorTop: number;
16 readonly lastBottom: number;
17 readonly turnHeight: number;
18 readonly usableViewportHeight: number;
19 readonly visibleUsableBottom: number;
20 readonly overflowsUsableViewport: boolean;
21 readonly targetScrollToRevealEnd: number;
22 readonly scrollDeltaToRevealEnd: number;
23}
24
25export function getRowBottom(state: TimelineListMeasurementState, index: number): number | null {
26 const top = state.positionAtIndex(index);
27 const height = state.sizeAtIndex(index);
28 if (
29 typeof top !== "number" ||
30 typeof height !== "number" ||
31 !Number.isFinite(top) ||
32 !Number.isFinite(height)
33 ) {
34 return null;
35 }
36
37 return top + Math.max(1, height);
38}
39
40/**
41 * Whether the timeline's real rows extend past the viewport left above the
42 * composer. The list's own content length includes the composer inset
43 * spacer, so this measures from the last row instead. Unknown row geometry
44 * or an unmeasured viewport counts as fitting.
45 */
46export function timelineContentOverflowsViewport(
47 state: TimelineListMeasurementState | undefined,
48 input: { readonly composerInset: number; readonly anchorOffset: number },
49): boolean {
50 if (!state || !state.data || state.data.length === 0) {
51 return false;
52 }
53 const scrollLength = state.scrollLength;
54 if (typeof scrollLength !== "number" || !Number.isFinite(scrollLength) || scrollLength <= 0) {
55 return false;
56 }
57 const lastBottom = getRowBottom(state, state.data.length - 1);
58 if (lastBottom === null) {
59 return false;
60 }
61 const visibleScrollLength = Math.max(0, scrollLength - input.composerInset - input.anchorOffset);
62 return lastBottom > visibleScrollLength;
63}
64
65export function getAnchoredTurnMetrics({
66 state,
67 anchorIndex,
68 composerOverlayHeight,
69 anchorOffset,
70}: {
71 readonly state: TimelineListMeasurementState;
72 readonly anchorIndex: number;
73 readonly composerOverlayHeight: number;
74 readonly anchorOffset: number;
75}): AnchoredTurnMetrics | null {
76 if (state.data.length === 0) {
77 return null;
78 }
79
80 const boundedAnchorIndex = Math.max(0, Math.min(anchorIndex, state.data.length - 1));
81 const anchorTop = state.positionAtIndex(boundedAnchorIndex);
82 const lastBottom = getRowBottom(state, state.data.length - 1);
83 if (typeof anchorTop !== "number" || !Number.isFinite(anchorTop) || lastBottom === null) {
84 return null;
85 }
86
87 const usableViewportHeight = Math.max(
88 0,
89 state.scrollLength - composerOverlayHeight - anchorOffset,
90 );
91 const turnHeight = Math.max(0, lastBottom - anchorTop);
92 const visibleUsableBottom = state.scroll + usableViewportHeight;
93 const targetScrollToRevealEnd = Math.max(0, lastBottom - usableViewportHeight);
94 const scrollDeltaToRevealEnd = Math.max(0, targetScrollToRevealEnd - state.scroll);
95
96 return {
97 anchorTop,
98 lastBottom,
99 turnHeight,
100 usableViewportHeight,
101 visibleUsableBottom,
102 overflowsUsableViewport: turnHeight > usableViewportHeight,
103 targetScrollToRevealEnd,
104 scrollDeltaToRevealEnd,
105 };
106}