Open History to see the last commit
1import { resolveWorkGroupScrollAnchor } from "@t3tools/client-runtime/work-log/scroll-anchor";
2
3export type ThreadFeedLiveFollowEvent =
4 | { readonly type: "reset" }
5 | { readonly type: "user-scroll-begin" }
6 | {
7 readonly type: "user-scroll-end";
8 readonly isAtEnd: boolean;
9 readonly userScrollSessionActive: boolean;
10 }
11 | {
12 readonly type: "scroll" | "disclosure-settled";
13 readonly isAtEnd: boolean;
14 readonly userScrollSessionActive: boolean;
15 };
16
17export interface ThreadWorkGroupScrollPosition {
18 readonly rowId: string;
19 readonly offsetWithinRow: number;
20 readonly scrollOffset: number;
21 readonly contentHeight: number;
22}
23
24export function resolveThreadWorkGroupInitialScroll(
25 rows: ReadonlyArray<{ readonly id: string }>,
26 position: ThreadWorkGroupScrollPosition | undefined,
27) {
28 const index = position ? rows.findIndex((row) => row.id === position.rowId) : -1;
29 return index >= 0 && position ? { index, viewOffset: -position.offsetWithinRow } : undefined;
30}
31
32export function shouldFollowThreadWorkGroupAppend(input: {
33 readonly previousRows: ReadonlyArray<{ readonly id: string }>;
34 readonly rows: ReadonlyArray<{ readonly id: string }>;
35 readonly previousContentHeight: number;
36 readonly contentHeight: number;
37 readonly viewportHeight: number;
38 readonly scrollOffset: number;
39 readonly detailsChanged: boolean;
40 readonly userScrolling: boolean;
41}) {
42 return (
43 !input.detailsChanged &&
44 !input.userScrolling &&
45 input.contentHeight > input.previousContentHeight &&
46 input.rows.length > input.previousRows.length &&
47 input.previousRows.every((row, index) => row.id === input.rows[index]?.id) &&
48 input.previousContentHeight - input.viewportHeight - input.scrollOffset <= 1
49 );
50}
51
52export function resolveThreadFeedSubmissionAnchor<AnchorId>(input: {
53 readonly currentAnchorMessageId: AnchorId | null;
54 readonly submittedMessageId: AnchorId;
55 readonly hasStartedTurn: boolean;
56 readonly hasUserMessage: boolean;
57 readonly queuedMessageCount: number;
58 readonly deliveryDeferred: boolean;
59}): AnchorId | null {
60 if (input.deliveryDeferred || input.hasStartedTurn || input.hasUserMessage) {
61 return null;
62 }
63
64 if (input.currentAnchorMessageId !== null) {
65 return input.currentAnchorMessageId;
66 }
67
68 return input.queuedMessageCount > 0 ? null : input.submittedMessageId;
69}
70
71export function resolveThreadFeedLiveFollow(
72 current: boolean,
73 event: ThreadFeedLiveFollowEvent,
74): boolean {
75 switch (event.type) {
76 case "reset":
77 return true;
78 case "user-scroll-begin":
79 return false;
80 case "user-scroll-end":
81 return event.userScrollSessionActive ? event.isAtEnd : current;
82 case "disclosure-settled":
83 return !event.userScrollSessionActive && event.isAtEnd;
84 case "scroll":
85 if (event.userScrollSessionActive) {
86 return false;
87 }
88 if (event.isAtEnd) {
89 return true;
90 }
91 return current;
92 }
93}
94
95/** The LegendList state a width reflow reads before rows re-measure and after they settle. */
96export interface ThreadFeedReflowListState {
97 readonly data: ReadonlyArray<{ readonly id: string }>;
98 readonly scroll: number;
99 readonly isWithinMaintainScrollAtEndThreshold: boolean;
100 readonly indexByKey: (key: string) => number | undefined;
101 readonly positionAtIndex: (index: number) => number | undefined;
102 readonly sizes: ReadonlyMap<string, number>;
103}
104
105export interface ThreadFeedReflowLayout {
106 /** List padding and header; row positions start below them. */
107 readonly rowsTop: number;
108 /** The top of the viewport covered by the navigation header. */
109 readonly headerInset: number;
110}
111
112export interface ThreadFeedReflowRowAnchor {
113 readonly key: string;
114 readonly offsetWithinRow: number;
115 readonly size: number;
116}
117
118/** A feed that follows keeps the end through a reflow; a reader keeps the row they are in. */
119export type ThreadFeedReflowAnchor = "end" | ThreadFeedReflowRowAnchor;
120
121function readingLine(state: ThreadFeedReflowListState, layout: ThreadFeedReflowLayout) {
122 return state.scroll + layout.headerInset - layout.rowsTop;
123}
124
125export function resolveThreadFeedReflowAnchor(
126 state: ThreadFeedReflowListState,
127 layout: ThreadFeedReflowLayout,
128 following: boolean,
129): ThreadFeedReflowAnchor | null {
130 // Follow alone is not enough: a status-bar tap scrolls to the top without a drag, so it
131 // leaves follow on while the reader is far from the end.
132 if (following && state.isWithinMaintainScrollAtEndThreshold) return "end";
133 const line = readingLine(state, layout);
134 // A reader at the top of the thread stays there without help.
135 if (line <= 0) return null;
136 const row = resolveWorkGroupScrollAnchor({
137 data: state.data,
138 scroll: line,
139 positionAtIndex: state.positionAtIndex,
140 });
141 const size = row ? state.sizes.get(row.rowId) : undefined;
142 return row && size ? { key: row.rowId, offsetWithinRow: row.offsetWithinRow, size } : null;
143}
144
145function resolveReflowAnchorPoint(
146 anchor: ThreadFeedReflowRowAnchor,
147 state: ThreadFeedReflowListState,
148) {
149 const index = state.indexByKey(anchor.key);
150 const top = index === undefined ? undefined : state.positionAtIndex(index);
151 const size = state.sizes.get(anchor.key);
152 if (index === undefined || top === undefined || size === undefined) return null;
153 // Rewrapped text grows with its row, so the reader's line sits at the same share of the
154 // row rather than the same pixel offset into it.
155 return { index, top, offsetWithinRow: (anchor.offsetWithinRow * size) / anchor.size };
156}
157
158/** The scroll offset that puts the reader's line back under the header at the current sizes. */
159export function resolveThreadFeedReflowOffset(
160 anchor: ThreadFeedReflowRowAnchor,
161 state: ThreadFeedReflowListState,
162 layout: ThreadFeedReflowLayout,
163): number | undefined {
164 const point = resolveReflowAnchorPoint(anchor, state);
165 return point
166 ? point.top + point.offsetWithinRow - layout.headerInset + layout.rowsTop
167 : undefined;
168}
169
170export function resolveThreadFeedReflowScroll(
171 anchor: ThreadFeedReflowRowAnchor,
172 state: ThreadFeedReflowListState,
173 layout: ThreadFeedReflowLayout,
174): { readonly index: number; readonly viewOffset: number } | null {
175 const point = resolveReflowAnchorPoint(anchor, state);
176 if (!point) return null;
177 if (Math.abs(readingLine(state, layout) - (point.top + point.offsetWithinRow)) < 1) return null;
178 return { index: point.index, viewOffset: layout.headerInset - point.offsetWithinRow };
179}