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