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

timelineScrollAnchoring.ts

97 lines3.0 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 keepTimelineEndVisibleAfterOverlayGrowth({
26 timeline,
27 previousOverlayHeight,
28 overlayHeight,
29 followingEnd,
30}: {
31 readonly timeline: { scrollToEnd: (options: { animated: boolean }) => unknown } | null;
32 readonly previousOverlayHeight: number;
33 readonly overlayHeight: number;
34 readonly followingEnd: boolean;
35}): void {
36 if (timeline && followingEnd && overlayHeight > previousOverlayHeight) {
37 void timeline.scrollToEnd({ animated: false });
38 }
40​
41export function getRowBottom(state: TimelineListMeasurementState, index: number): number | null {
42 const top = state.positionAtIndex(index);
43 const height = state.sizeAtIndex(index);
44 if (
45 typeof top !== "number" ||
46 typeof height !== "number" ||
47 !Number.isFinite(top) ||
48 !Number.isFinite(height)
49 ) {
50 return null;
51 }
52​
53 return top + Math.max(1, height);
55​
56export function getAnchoredTurnMetrics({
57 state,
58 anchorIndex,
59 composerOverlayHeight,
60 anchorOffset,
61}: {
62 readonly state: TimelineListMeasurementState;
63 readonly anchorIndex: number;
64 readonly composerOverlayHeight: number;
65 readonly anchorOffset: number;
66}): AnchoredTurnMetrics | null {
67 if (state.data.length === 0) {
68 return null;
69 }
70​
71 const boundedAnchorIndex = Math.max(0, Math.min(anchorIndex, state.data.length - 1));
72 const anchorTop = state.positionAtIndex(boundedAnchorIndex);
73 const lastBottom = getRowBottom(state, state.data.length - 1);
74 if (typeof anchorTop !== "number" || !Number.isFinite(anchorTop) || lastBottom === null) {
75 return null;
76 }
77​
78 const usableViewportHeight = Math.max(
79 0,
80 state.scrollLength - composerOverlayHeight - anchorOffset,
81 );
82 const turnHeight = Math.max(0, lastBottom - anchorTop);
83 const visibleUsableBottom = state.scroll + usableViewportHeight;
84 const targetScrollToRevealEnd = Math.max(0, lastBottom - usableViewportHeight);
85 const scrollDeltaToRevealEnd = Math.max(0, targetScrollToRevealEnd - state.scroll);
86​
87 return {
88 anchorTop,
89 lastBottom,
90 turnHeight,
91 usableViewportHeight,
92 visibleUsableBottom,
93 overflowsUsableViewport: turnHeight > usableViewportHeight,
94 targetScrollToRevealEnd,
95 scrollDeltaToRevealEnd,
96 };