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}): AnchorId | null {
59 if (input.hasStartedTurn || input.hasUserMessage) {
60 return null;
61 }
62
63 if (input.currentAnchorMessageId !== null) {
64 return input.currentAnchorMessageId;
65 }
66
67 return input.queuedMessageCount > 0 ? null : input.submittedMessageId;
68}
69
70export function resolveThreadFeedLiveFollow(
71 current: boolean,
72 event: ThreadFeedLiveFollowEvent,
73): boolean {
74 switch (event.type) {
75 case "reset":
76 return true;
77 case "user-scroll-begin":
78 return false;
79 case "user-scroll-end":
80 return event.userScrollSessionActive ? event.isAtEnd : current;
81 case "disclosure-settled":
82 return !event.userScrollSessionActive && event.isAtEnd;
83 case "scroll":
84 if (event.userScrollSessionActive) {
85 return false;
86 }
87 if (event.isAtEnd) {
88 return true;
89 }
90 return current;
91 }
92}
93
94/** The LegendList state a width reflow reads before rows re-measure and after they settle. */
95export interface ThreadFeedReflowListState {
96 readonly data: ReadonlyArray<{ readonly id: string }>;
97 readonly scroll: number;
98 readonly isWithinMaintainScrollAtEndThreshold: boolean;
99 readonly indexByKey: (key: string) => number | undefined;
100 readonly positionAtIndex: (index: number) => number | undefined;
101 readonly sizes: ReadonlyMap<string, number>;
102}
103
104export interface ThreadFeedReflowLayout {
105 /** List padding and header; row positions start below them. */
106 readonly rowsTop: number;
107 /** The top of the viewport covered by the navigation header. */
108 readonly headerInset: number;
109}
110
111export interface ThreadFeedReflowRowAnchor {
112 readonly key: string;
113 readonly offsetWithinRow: number;
114 readonly size: number;
115}
116
117/** A feed that follows keeps the end through a reflow; a reader keeps the row they are in. */
118export type ThreadFeedReflowAnchor = "end" | ThreadFeedReflowRowAnchor;
119
120function readingLine(state: ThreadFeedReflowListState, layout: ThreadFeedReflowLayout) {
121 return state.scroll + layout.headerInset - layout.rowsTop;
122}
123
124export function resolveThreadFeedReflowAnchor(
125 state: ThreadFeedReflowListState,
126 layout: ThreadFeedReflowLayout,
127 following: boolean,
128): ThreadFeedReflowAnchor | null {
129 // Follow alone is not enough: a status-bar tap scrolls to the top without a drag, so it
130 // leaves follow on while the reader is far from the end.
131 if (following && state.isWithinMaintainScrollAtEndThreshold) return "end";
132 const line = readingLine(state, layout);
133 // A reader at the top of the thread stays there without help.
134 if (line <= 0) return null;
135 const row = resolveWorkGroupScrollAnchor({
136 data: state.data,
137 scroll: line,
138 positionAtIndex: state.positionAtIndex,
139 });
140 const size = row ? state.sizes.get(row.rowId) : undefined;
141 return row && size ? { key: row.rowId, offsetWithinRow: row.offsetWithinRow, size } : null;
142}
143
144function resolveReflowAnchorPoint(
145 anchor: ThreadFeedReflowRowAnchor,
146 state: ThreadFeedReflowListState,
147) {
148 const index = state.indexByKey(anchor.key);
149 const top = index === undefined ? undefined : state.positionAtIndex(index);
150 const size = state.sizes.get(anchor.key);
151 if (index === undefined || top === undefined || size === undefined) return null;
152 // Rewrapped text grows with its row, so the reader's line sits at the same share of the
153 // row rather than the same pixel offset into it.
154 return { index, top, offsetWithinRow: (anchor.offsetWithinRow * size) / anchor.size };
155}
156
157/** The scroll offset that puts the reader's line back under the header at the current sizes. */
158export function resolveThreadFeedReflowOffset(
159 anchor: ThreadFeedReflowRowAnchor,
160 state: ThreadFeedReflowListState,
161 layout: ThreadFeedReflowLayout,
162): number | undefined {
163 const point = resolveReflowAnchorPoint(anchor, state);
164 return point
165 ? point.top + point.offsetWithinRow - layout.headerInset + layout.rowsTop
166 : undefined;
167}
168
169export function resolveThreadFeedReflowScroll(
170 anchor: ThreadFeedReflowRowAnchor,
171 state: ThreadFeedReflowListState,
172 layout: ThreadFeedReflowLayout,
173): { readonly index: number; readonly viewOffset: number } | null {
174 const point = resolveReflowAnchorPoint(anchor, state);
175 if (!point) return null;
176 if (Math.abs(readingLine(state, layout) - (point.top + point.offsetWithinRow)) < 1) return null;
177 return { index: point.index, viewOffset: layout.headerInset - point.offsetWithinRow };
178}