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

timelineScrollAnchoring.ts

81 lines2.5 KB

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;
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;
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);
39​
40export function getAnchoredTurnMetrics({
41 state,
42 anchorIndex,
43 composerOverlayHeight,
44 anchorOffset,
45}: {
46 readonly state: TimelineListMeasurementState;
47 readonly anchorIndex: number;
48 readonly composerOverlayHeight: number;
49 readonly anchorOffset: number;
50}): AnchoredTurnMetrics | null {
51 if (state.data.length === 0) {
52 return null;
53 }
54​
55 const boundedAnchorIndex = Math.max(0, Math.min(anchorIndex, state.data.length - 1));
56 const anchorTop = state.positionAtIndex(boundedAnchorIndex);
57 const lastBottom = getRowBottom(state, state.data.length - 1);
58 if (typeof anchorTop !== "number" || !Number.isFinite(anchorTop) || lastBottom === null) {
59 return null;
60 }
61​
62 const usableViewportHeight = Math.max(
63 0,
64 state.scrollLength - composerOverlayHeight - anchorOffset,
65 );
66 const turnHeight = Math.max(0, lastBottom - anchorTop);
67 const visibleUsableBottom = state.scroll + usableViewportHeight;
68 const targetScrollToRevealEnd = Math.max(0, lastBottom - usableViewportHeight);
69 const scrollDeltaToRevealEnd = Math.max(0, targetScrollToRevealEnd - state.scroll);
70​
71 return {
72 anchorTop,
73 lastBottom,
74 turnHeight,
75 usableViewportHeight,
76 visibleUsableBottom,
77 overflowsUsableViewport: turnHeight > usableViewportHeight,
78 targetScrollToRevealEnd,
79 scrollDeltaToRevealEnd,
80 };