Open History to see the last commit
1export type TimelineScrollMode = "following-end" | "anchoring-new-turn" | "free-scrolling";
2
3export interface TimelineListMeasurementState {
4 readonly data: readonly unknown[];
5 readonly scroll: number;
6 readonly scrollLength: number;
7 readonly positionAtIndex: (index: number) => number | undefined;
8 readonly sizeAtIndex: (index: number) => number | undefined;
9}
10
11export interface AnchoredTurnMetrics {
12 readonly anchorTop: number;
13 readonly lastBottom: number;
14 readonly turnHeight: number;
15 readonly usableViewportHeight: number;
16 readonly visibleUsableBottom: number;
17 readonly overflowsUsableViewport: boolean;
18 readonly targetScrollToRevealEnd: number;
19 readonly scrollDeltaToRevealEnd: number;
20}
21
22export function keepTimelineEndVisibleAfterOverlayGrowth({
23 timeline,
24 previousOverlayHeight,
25 overlayHeight,
26 followingEnd,
27}: {
28 readonly timeline: { scrollToEnd: (options: { animated: boolean }) => unknown } | null;
29 readonly previousOverlayHeight: number;
30 readonly overlayHeight: number;
31 readonly followingEnd: boolean;
32}): void {
33 if (timeline && followingEnd && overlayHeight > previousOverlayHeight) {
34 void timeline.scrollToEnd({ animated: false });
35 }
36}
37
38export function getRowBottom(state: TimelineListMeasurementState, index: number): number | null {
39 const top = state.positionAtIndex(index);
40 const height = state.sizeAtIndex(index);
41 if (
42 typeof top !== "number" ||
43 typeof height !== "number" ||
44 !Number.isFinite(top) ||
45 !Number.isFinite(height)
46 ) {
47 return null;
48 }
49
50 return top + Math.max(1, height);
51}
52
53export function getAnchoredTurnMetrics({
54 state,
55 anchorIndex,
56 composerOverlayHeight,
57 anchorOffset,
58}: {
59 readonly state: TimelineListMeasurementState;
60 readonly anchorIndex: number;
61 readonly composerOverlayHeight: number;
62 readonly anchorOffset: number;
63}): AnchoredTurnMetrics | null {
64 if (state.data.length === 0) {
65 return null;
66 }
67
68 const boundedAnchorIndex = Math.max(0, Math.min(anchorIndex, state.data.length - 1));
69 const anchorTop = state.positionAtIndex(boundedAnchorIndex);
70 const lastBottom = getRowBottom(state, state.data.length - 1);
71 if (typeof anchorTop !== "number" || !Number.isFinite(anchorTop) || lastBottom === null) {
72 return null;
73 }
74
75 const usableViewportHeight = Math.max(
76 0,
77 state.scrollLength - composerOverlayHeight - anchorOffset,
78 );
79 const turnHeight = Math.max(0, lastBottom - anchorTop);
80 const visibleUsableBottom = state.scroll + usableViewportHeight;
81 const targetScrollToRevealEnd = Math.max(0, lastBottom - usableViewportHeight);
82 const scrollDeltaToRevealEnd = Math.max(0, targetScrollToRevealEnd - state.scroll);
83
84 return {
85 anchorTop,
86 lastBottom,
87 turnHeight,
88 usableViewportHeight,
89 visibleUsableBottom,
90 overflowsUsableViewport: turnHeight > usableViewportHeight,
91 targetScrollToRevealEnd,
92 scrollDeltaToRevealEnd,
93 };
94}