@meshagent/meshagent-tailwind 0.55.0 → 0.55.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +34 -3
  3. package/dist/cjs/chat/agent-thread.d.ts +42 -7
  4. package/dist/cjs/chat/agent-thread.js +422 -183
  5. package/dist/cjs/chat/chat-input-state.d.ts +2 -0
  6. package/dist/cjs/chat/chat-input-state.js +41 -0
  7. package/dist/cjs/chat/chat-input.d.ts +14 -2
  8. package/dist/cjs/chat/chat-input.js +41 -76
  9. package/dist/cjs/chat/chat-scroller.js +45 -43
  10. package/dist/cjs/chat/chat-thread-timeline.d.ts +2 -0
  11. package/dist/cjs/chat/chat-thread-timeline.js +83 -10
  12. package/dist/cjs/chat/composer-action-button.d.ts +11 -0
  13. package/dist/cjs/chat/composer-action-button.js +68 -0
  14. package/dist/cjs/chat/use-chat-virtualizer.d.ts +14 -17
  15. package/dist/cjs/chat/use-chat-virtualizer.js +288 -222
  16. package/dist/cjs/chat/virtualization.d.ts +0 -5
  17. package/dist/cjs/chat/virtualization.js +4 -6
  18. package/dist/cjs/index.d.ts +1 -0
  19. package/dist/cjs/index.js +1 -0
  20. package/dist/esm/chat/agent-thread.d.ts +42 -7
  21. package/dist/esm/chat/agent-thread.js +424 -186
  22. package/dist/esm/chat/chat-input-state.d.ts +2 -0
  23. package/dist/esm/chat/chat-input-state.js +21 -0
  24. package/dist/esm/chat/chat-input.d.ts +14 -2
  25. package/dist/esm/chat/chat-input.js +41 -76
  26. package/dist/esm/chat/chat-scroller.js +46 -44
  27. package/dist/esm/chat/chat-thread-timeline.d.ts +2 -0
  28. package/dist/esm/chat/chat-thread-timeline.js +83 -10
  29. package/dist/esm/chat/composer-action-button.d.ts +11 -0
  30. package/dist/esm/chat/composer-action-button.js +48 -0
  31. package/dist/esm/chat/use-chat-virtualizer.d.ts +14 -17
  32. package/dist/esm/chat/use-chat-virtualizer.js +290 -226
  33. package/dist/esm/chat/virtualization.d.ts +0 -5
  34. package/dist/esm/chat/virtualization.js +4 -6
  35. package/dist/esm/index.d.ts +1 -0
  36. package/dist/esm/index.js +1 -0
  37. package/dist/index.css +1 -1
  38. package/package.json +6 -5
@@ -1,9 +1,7 @@
1
- import { type RefObject } from "react";
2
- import { type Page, type VisiblePageRange } from "./virtualization.js";
1
+ import { type VisiblePageRange } from "./virtualization.js";
3
2
  export type ChatVirtualizerState = {
4
3
  visibleRange: VisiblePageRange;
5
4
  measuredHeights: ReadonlyMap<number, number>;
6
- bottomAdjustment: number;
7
5
  };
8
6
  export type ChatVirtualizerAction = {
9
7
  type: "setVisibleRange";
@@ -13,12 +11,12 @@ export type ChatVirtualizerAction = {
13
11
  pageCount: number;
14
12
  overscanPages: number;
15
13
  } | {
16
- type: "measurePage";
17
- pageIndex: number;
18
- height: number;
19
- shouldAdjustBottom: boolean;
14
+ type: "measurePages";
15
+ heights: ReadonlyMap<number, number>;
20
16
  } | {
21
- type: "clearBottomAdjustment";
17
+ type: "reset";
18
+ pageCount: number;
19
+ overscanPages: number;
22
20
  };
23
21
  export type CreateChatVirtualizerStateArgs = {
24
22
  pageCount: number;
@@ -31,17 +29,16 @@ type UseChatVirtualizerOptions<TMessage> = {
31
29
  bottomThresholdPx: number;
32
30
  getMessageId: (message: TMessage) => string;
33
31
  };
34
- type UseChatVirtualizerResult<TMessage> = {
35
- scrollRef: RefObject<HTMLDivElement | null>;
36
- visiblePages: Page<TMessage>[];
37
- spacerHeight: number;
38
- windowBottom: number;
39
- handleScroll: () => void;
40
- handlePageHeightChange: (pageIndex: number, height: number) => void;
41
- };
42
32
  export declare function getInitialVisibleRange(pageCount: number, overscanPages: number): VisiblePageRange;
43
33
  export declare function clampVisibleRange(range: VisiblePageRange, pageCount: number): VisiblePageRange;
44
34
  export declare function createChatVirtualizerState({ pageCount, overscanPages, }: CreateChatVirtualizerStateArgs): ChatVirtualizerState;
45
35
  export declare function chatVirtualizerReducer(state: ChatVirtualizerState, action: ChatVirtualizerAction): ChatVirtualizerState;
46
- export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>): UseChatVirtualizerResult<TMessage>;
36
+ export declare function useChatVirtualizer<TMessage>({ messages, pageSize, overscanPages, bottomThresholdPx, getMessageId, }: UseChatVirtualizerOptions<TMessage>): {
37
+ scrollRef: import("react").RefObject<HTMLDivElement | null>;
38
+ visiblePages: import("./virtualization.js").Page<TMessage>[];
39
+ spacerHeights: import("./virtualization.js").SpacerHeights;
40
+ handleScroll: () => void;
41
+ handleScrollIntent: () => void;
42
+ registerPage: (index: number, element: HTMLDivElement) => () => void;
43
+ };
47
44
  export {};
@@ -27,20 +27,17 @@ __export(use_chat_virtualizer_exports, {
27
27
  module.exports = __toCommonJS(use_chat_virtualizer_exports);
28
28
  var import_react = require("react");
29
29
  var import_virtualization = require("./virtualization.js");
30
+ const RESIZE_QUIET_MS = 100;
31
+ const RESIZE_MAX_WAIT_MS = 300;
32
+ const SCROLL_EPSILON = 1;
30
33
  function getInitialVisibleRange(pageCount, overscanPages) {
31
- if (pageCount === 0) {
32
- return { first: 0, last: -1 };
33
- }
34
- return {
34
+ return pageCount === 0 ? { first: 0, last: -1 } : {
35
35
  first: Math.max(0, pageCount - 1 - Math.max(0, overscanPages)),
36
36
  last: pageCount - 1
37
37
  };
38
38
  }
39
39
  function clampVisibleRange(range, pageCount) {
40
- if (pageCount === 0) {
41
- return { first: 0, last: -1 };
42
- }
43
- return {
40
+ return pageCount === 0 ? { first: 0, last: -1 } : {
44
41
  first: Math.min(pageCount - 1, Math.max(0, range.first)),
45
42
  last: Math.min(pageCount - 1, Math.max(0, range.last))
46
43
  };
@@ -51,157 +48,103 @@ function createChatVirtualizerState({
51
48
  }) {
52
49
  return {
53
50
  visibleRange: getInitialVisibleRange(pageCount, overscanPages),
54
- measuredHeights: /* @__PURE__ */ new Map(),
55
- bottomAdjustment: 0
51
+ measuredHeights: /* @__PURE__ */ new Map()
56
52
  };
57
53
  }
58
- function areRangesEqual(firstRange, secondRange) {
59
- return firstRange.first === secondRange.first && firstRange.last === secondRange.last;
60
- }
61
- function pruneMeasuredHeights(measuredHeights, pageCount) {
62
- let didPrune = false;
63
- const nextMeasuredHeights = /* @__PURE__ */ new Map();
64
- for (const [pageIndex, height] of measuredHeights) {
65
- if (pageIndex >= pageCount) {
66
- didPrune = true;
67
- continue;
68
- }
69
- nextMeasuredHeights.set(pageIndex, height);
70
- }
71
- return didPrune ? nextMeasuredHeights : measuredHeights;
54
+ function areRangesEqual(a, b) {
55
+ return a.first === b.first && a.last === b.last;
72
56
  }
73
57
  function chatVirtualizerReducer(state, action) {
74
58
  switch (action.type) {
59
+ case "reset": {
60
+ return createChatVirtualizerState(action);
61
+ }
75
62
  case "setVisibleRange": {
76
- if (areRangesEqual(state.visibleRange, action.range)) {
77
- return state;
78
- }
79
- return {
80
- ...state,
81
- visibleRange: action.range
82
- };
63
+ return areRangesEqual(state.visibleRange, action.range) ? state : { ...state, visibleRange: action.range };
83
64
  }
84
65
  case "syncPageWindow": {
85
66
  const visibleRange = state.visibleRange.last === -1 ? getInitialVisibleRange(action.pageCount, action.overscanPages) : clampVisibleRange(state.visibleRange, action.pageCount);
86
- const measuredHeights = pruneMeasuredHeights(state.measuredHeights, action.pageCount);
87
- if (areRangesEqual(state.visibleRange, visibleRange) && measuredHeights === state.measuredHeights) {
88
- return state;
89
- }
90
- return {
91
- ...state,
92
- visibleRange,
93
- measuredHeights
94
- };
95
- }
96
- case "measurePage": {
97
- const roundedHeight = Math.ceil(action.height);
98
- const previousHeight = state.measuredHeights.get(action.pageIndex) ?? 0;
99
- if (previousHeight === roundedHeight) {
100
- return state;
101
- }
102
- const heightDelta = roundedHeight - previousHeight;
103
67
  const measuredHeights = new Map(state.measuredHeights);
104
- if (roundedHeight > 0) {
105
- measuredHeights.set(action.pageIndex, roundedHeight);
106
- } else {
107
- measuredHeights.delete(action.pageIndex);
68
+ for (const index of measuredHeights.keys()) {
69
+ if (index >= action.pageCount) {
70
+ measuredHeights.delete(index);
71
+ }
108
72
  }
109
- return {
110
- ...state,
111
- measuredHeights,
112
- bottomAdjustment: state.bottomAdjustment + (action.shouldAdjustBottom ? heightDelta : 0)
113
- };
73
+ return areRangesEqual(state.visibleRange, visibleRange) && measuredHeights.size === state.measuredHeights.size ? state : { visibleRange, measuredHeights };
114
74
  }
115
- case "clearBottomAdjustment": {
116
- if (state.bottomAdjustment === 0) {
117
- return state;
75
+ case "measurePages": {
76
+ const measuredHeights = new Map(state.measuredHeights);
77
+ for (const [index, height] of action.heights) {
78
+ measuredHeights.set(index, height);
118
79
  }
119
- return {
120
- ...state,
121
- bottomAdjustment: 0
122
- };
80
+ return { ...state, measuredHeights };
123
81
  }
124
82
  }
125
83
  }
126
- function getMessageSelector(messageId) {
127
- const escapedMessageId = typeof CSS !== "undefined" && typeof CSS.escape === "function" ? CSS.escape(messageId) : messageId.replace(/"/gu, '\\"');
128
- return '.chat-scroll__message[data-message-id="' + escapedMessageId + '"]';
84
+ function cancelResizeTimers(runtime) {
85
+ window.clearTimeout(runtime.quietTimer);
86
+ window.clearTimeout(runtime.maxTimer);
87
+ runtime.quietTimer = void 0;
88
+ runtime.maxTimer = void 0;
129
89
  }
130
- function captureScrollAnchor(scrollElement, runtime) {
131
- if (!scrollElement || runtime.isAtBottom) {
132
- runtime.anchor = void 0;
133
- return void 0;
90
+ function captureAnchors(element, runtime) {
91
+ const rect = element.getBoundingClientRect();
92
+ runtime.anchors = [];
93
+ runtime.lastScrollTop = element.scrollTop;
94
+ runtime.lastScrollHeight = element.scrollHeight;
95
+ if (runtime.isAtBottom) {
96
+ return;
134
97
  }
135
- const scrollRect = scrollElement.getBoundingClientRect();
136
- const messageElements = scrollElement.querySelectorAll(
137
- ".chat-scroll__message[data-message-id]"
138
- );
139
- for (let index = 0; index < messageElements.length; index += 1) {
140
- const messageElement = messageElements.item(index);
141
- const messageRect = messageElement.getBoundingClientRect();
142
- if (messageRect.bottom > scrollRect.top) {
143
- const messageId = messageElement.dataset.messageId;
144
- const closestPageElement = messageElement.closest(
145
- ".chat-scroll__page[data-page-index]"
146
- );
147
- const pageIndex = closestPageElement instanceof HTMLElement ? Number(closestPageElement.dataset.pageIndex) : Number.NaN;
148
- if (messageId && Number.isInteger(pageIndex)) {
149
- const anchor = {
150
- messageId,
151
- offsetTop: messageRect.top - scrollRect.top,
152
- pageIndex
153
- };
154
- runtime.anchor = anchor;
155
- return anchor;
156
- }
157
- runtime.anchor = void 0;
158
- return void 0;
98
+ for (const message of Array.from(
99
+ element.querySelectorAll(
100
+ ".chat-scroll__message[data-message-id]"
101
+ )
102
+ )) {
103
+ const messageRect = message.getBoundingClientRect();
104
+ if (messageRect.bottom > rect.top && messageRect.top < rect.bottom && message.dataset.messageId) {
105
+ runtime.anchors.push({
106
+ messageId: message.dataset.messageId,
107
+ offsetTop: messageRect.top - rect.top
108
+ });
159
109
  }
160
110
  }
161
- runtime.anchor = void 0;
162
- return void 0;
163
111
  }
164
- function restoreScrollAnchor(scrollElement, runtime, bottomAdjustment) {
165
- const anchor = runtime.anchor;
166
- if (!anchor || !scrollElement || runtime.isAtBottom) {
167
- runtime.anchor = void 0;
168
- return bottomAdjustment !== 0;
169
- }
170
- const messageElement = scrollElement.querySelector(
171
- getMessageSelector(anchor.messageId)
112
+ function writeScrollTop(element, runtime, target) {
113
+ const clampedTarget = Math.max(
114
+ 0,
115
+ Math.min(target, element.scrollHeight - element.clientHeight)
172
116
  );
173
- if (!messageElement) {
174
- runtime.anchor = void 0;
175
- return bottomAdjustment !== 0;
176
- }
177
- const scrollRect = scrollElement.getBoundingClientRect();
178
- const messageRect = messageElement.getBoundingClientRect();
179
- const nextOffsetTop = messageRect.top - scrollRect.top;
180
- const offsetDelta = nextOffsetTop - anchor.offsetTop;
181
- if (offsetDelta !== 0) {
182
- scrollElement.scrollTop += offsetDelta;
117
+ if (Math.abs(element.scrollTop - clampedTarget) > SCROLL_EPSILON) {
118
+ element.scrollTop = clampedTarget;
119
+ runtime.expectedScrollTop = element.scrollTop;
183
120
  }
184
- if (bottomAdjustment !== 0) {
185
- scrollElement.scrollTop += bottomAdjustment;
186
- }
187
- runtime.anchor = void 0;
188
- return bottomAdjustment !== 0;
189
121
  }
190
- function updateVisibleRange(scrollElement, runtime, dispatch) {
191
- if (!scrollElement) {
192
- return;
193
- }
194
- const nextRange = (0, import_virtualization.getVisiblePageRange)({
195
- prefixHeights: runtime.prefixHeights,
196
- scrollTop: scrollElement.scrollTop,
197
- viewportHeight: scrollElement.clientHeight,
198
- overscanPages: runtime.overscanPages
199
- });
200
- const clampedNextRange = clampVisibleRange(nextRange, runtime.pageCount);
201
- if (!areRangesEqual(runtime.visibleRange, clampedNextRange)) {
202
- captureScrollAnchor(scrollElement, runtime);
122
+ function restoreAnchors(element, runtime) {
123
+ if (runtime.isAtBottom) {
124
+ writeScrollTop(element, runtime, element.scrollHeight);
125
+ } else {
126
+ const messages = new Map(
127
+ Array.from(
128
+ element.querySelectorAll(
129
+ ".chat-scroll__message[data-message-id]"
130
+ ),
131
+ (message) => [message.dataset.messageId, message]
132
+ )
133
+ );
134
+ for (const anchor of runtime.anchors) {
135
+ const message = messages.get(anchor.messageId);
136
+ if (message) {
137
+ const offset = message.getBoundingClientRect().top - element.getBoundingClientRect().top;
138
+ writeScrollTop(
139
+ element,
140
+ runtime,
141
+ element.scrollTop + offset - anchor.offsetTop
142
+ );
143
+ break;
144
+ }
145
+ }
203
146
  }
204
- dispatch({ type: "setVisibleRange", range: clampedNextRange });
147
+ captureAnchors(element, runtime);
205
148
  }
206
149
  function useChatVirtualizer({
207
150
  messages,
@@ -212,125 +155,248 @@ function useChatVirtualizer({
212
155
  }) {
213
156
  const scrollRef = (0, import_react.useRef)(null);
214
157
  const runtimeRef = (0, import_react.useRef)({
158
+ active: true,
215
159
  isAtBottom: true,
216
- hasMounted: false,
217
- lastMessageId: void 0,
218
- anchor: void 0,
160
+ anchors: [],
161
+ expectedScrollTop: void 0,
162
+ lastScrollTop: 0,
163
+ lastScrollHeight: 0,
164
+ userScrollIntent: false,
165
+ quietTimer: void 0,
166
+ maxTimer: void 0,
219
167
  frameId: void 0,
220
- prefixHeights: [0],
221
- visibleRange: { first: 0, last: -1 },
222
- pageCount: 0,
223
- overscanPages,
224
- bottomThresholdPx
168
+ pages: /* @__PURE__ */ new Map(),
169
+ pending: /* @__PURE__ */ new Map(),
170
+ newPages: false,
171
+ needsRestore: true,
172
+ messageIds: /* @__PURE__ */ new Set(),
173
+ pageSize
225
174
  });
226
175
  const pages = (0, import_react.useMemo)(
227
176
  () => (0, import_virtualization.createPages)([...messages].reverse(), pageSize),
228
177
  [messages, pageSize]
229
178
  );
179
+ const messageIds = (0, import_react.useMemo)(
180
+ () => new Set(messages.map(getMessageId)),
181
+ [messages, getMessageId]
182
+ );
230
183
  const pageCount = pages.length;
231
184
  const [state, dispatch] = (0, import_react.useReducer)(
232
185
  chatVirtualizerReducer,
233
186
  { pageCount, overscanPages },
234
187
  createChatVirtualizerState
235
188
  );
236
- const clampedVisibleRange = clampVisibleRange(state.visibleRange, pageCount);
237
- const pageHeights = (0, import_react.useMemo)(
238
- () => (0, import_virtualization.buildPageHeights)(pageCount, state.measuredHeights),
189
+ const visibleRange = clampVisibleRange(state.visibleRange, pageCount);
190
+ const prefixHeights = (0, import_react.useMemo)(
191
+ () => (0, import_virtualization.buildPrefixHeights)((0, import_virtualization.buildPageHeights)(pageCount, state.measuredHeights)),
239
192
  [pageCount, state.measuredHeights]
240
193
  );
241
- const prefixHeights = (0, import_react.useMemo)(() => (0, import_virtualization.buildPrefixHeights)(pageHeights), [pageHeights]);
242
- const virtualWindowMetrics = (0, import_virtualization.getVirtualWindowMetrics)(prefixHeights, clampedVisibleRange);
243
- const visiblePages = clampedVisibleRange.last >= clampedVisibleRange.first ? pages.slice(clampedVisibleRange.first, clampedVisibleRange.last + 1) : [];
244
- const runtime = runtimeRef.current;
245
- runtime.prefixHeights = prefixHeights;
246
- runtime.visibleRange = clampedVisibleRange;
247
- runtime.pageCount = pageCount;
248
- runtime.overscanPages = overscanPages;
249
- runtime.bottomThresholdPx = bottomThresholdPx;
250
- const handleScroll = (0, import_react.useCallback)(() => {
251
- const scrollElement = scrollRef.current;
252
- const currentRuntime = runtimeRef.current;
253
- if (!scrollElement) {
254
- return;
194
+ const geometryRef = (0, import_react.useRef)({
195
+ prefixHeights,
196
+ visibleRange,
197
+ pageCount,
198
+ overscanPages,
199
+ bottomThresholdPx
200
+ });
201
+ const flushMeasurements = (0, import_react.useCallback)(() => {
202
+ const runtime = runtimeRef.current;
203
+ const heights = /* @__PURE__ */ new Map();
204
+ cancelResizeTimers(runtime);
205
+ for (const [index, page] of runtime.pending) {
206
+ if (runtime.pages.get(index) === page) {
207
+ heights.set(index, page.height);
208
+ }
255
209
  }
256
- currentRuntime.isAtBottom = (0, import_virtualization.isNearBottom)({
257
- scrollTop: scrollElement.scrollTop,
258
- scrollHeight: scrollElement.scrollHeight,
259
- clientHeight: scrollElement.clientHeight,
260
- thresholdPx: currentRuntime.bottomThresholdPx
261
- });
262
- if (currentRuntime.frameId !== void 0) {
210
+ runtime.pending.clear();
211
+ if (!runtime.active || heights.size === 0) {
212
+ return false;
213
+ }
214
+ runtime.needsRestore = true;
215
+ dispatch({ type: "measurePages", heights });
216
+ return true;
217
+ }, []);
218
+ const registerPage = (0, import_react.useCallback)(
219
+ (index, element) => {
220
+ const runtime = runtimeRef.current;
221
+ const page = { element, height: element.getBoundingClientRect().height };
222
+ let observing = true;
223
+ runtime.active = true;
224
+ runtime.pages.set(index, page);
225
+ runtime.pending.set(index, page);
226
+ runtime.newPages = true;
227
+ runtime.needsRestore = true;
228
+ const observer = new ResizeObserver(() => {
229
+ if (!observing || !runtime.active || runtime.pages.get(index) !== page) {
230
+ return;
231
+ }
232
+ const height = element.getBoundingClientRect().height;
233
+ if (page.height === height) {
234
+ return;
235
+ }
236
+ page.height = height;
237
+ runtime.pending.set(index, page);
238
+ window.clearTimeout(runtime.quietTimer);
239
+ runtime.quietTimer = window.setTimeout(
240
+ flushMeasurements,
241
+ RESIZE_QUIET_MS
242
+ );
243
+ if (runtime.maxTimer === void 0) {
244
+ runtime.maxTimer = window.setTimeout(
245
+ flushMeasurements,
246
+ RESIZE_MAX_WAIT_MS
247
+ );
248
+ }
249
+ });
250
+ observer.observe(element);
251
+ return () => {
252
+ observing = false;
253
+ observer.disconnect();
254
+ if (runtime.pages.get(index) === page) {
255
+ runtime.pages.delete(index);
256
+ runtime.pending.delete(index);
257
+ }
258
+ if (runtime.pending.size === 0) {
259
+ cancelResizeTimers(runtime);
260
+ }
261
+ };
262
+ },
263
+ [flushMeasurements]
264
+ );
265
+ const updateVisibleRange = (0, import_react.useCallback)(() => {
266
+ const element = scrollRef.current;
267
+ const runtime = runtimeRef.current;
268
+ const geometry = geometryRef.current;
269
+ if (!element || runtime.pending.size > 0) {
263
270
  return;
264
271
  }
265
- currentRuntime.frameId = window.requestAnimationFrame(() => {
266
- currentRuntime.frameId = void 0;
267
- updateVisibleRange(scrollRef.current, currentRuntime, dispatch);
268
- });
272
+ const range = clampVisibleRange(
273
+ (0, import_virtualization.getVisiblePageRange)({
274
+ prefixHeights: geometry.prefixHeights,
275
+ scrollTop: element.scrollTop,
276
+ viewportHeight: element.clientHeight,
277
+ overscanPages: geometry.overscanPages
278
+ }),
279
+ geometry.pageCount
280
+ );
281
+ if (!areRangesEqual(range, geometry.visibleRange)) {
282
+ runtime.needsRestore = true;
283
+ dispatch({ type: "setVisibleRange", range });
284
+ }
269
285
  }, []);
270
- const handlePageHeightChange = (0, import_react.useCallback)((pageIndex, height) => {
271
- const currentRuntime = runtimeRef.current;
272
- const anchor = captureScrollAnchor(scrollRef.current, currentRuntime);
273
- const shouldAdjustBottom = anchor ? (0, import_virtualization.shouldAdjustScrollForHeightChange)({
274
- changedPageIndex: pageIndex,
275
- firstVisiblePageIndex: anchor.pageIndex,
276
- isAtBottom: currentRuntime.isAtBottom
277
- }) : false;
278
- dispatch({ type: "measurePage", pageIndex, height, shouldAdjustBottom });
286
+ const handleScrollIntent = (0, import_react.useCallback)(() => {
287
+ runtimeRef.current.userScrollIntent = true;
288
+ runtimeRef.current.expectedScrollTop = void 0;
279
289
  }, []);
280
- (0, import_react.useLayoutEffect)(() => {
281
- const currentRuntime = runtimeRef.current;
282
- const scrollElement = scrollRef.current;
283
- const shouldClearBottomAdjustment = restoreScrollAnchor(
284
- scrollElement,
285
- currentRuntime,
286
- state.bottomAdjustment
287
- );
288
- if (shouldClearBottomAdjustment) {
289
- dispatch({ type: "clearBottomAdjustment" });
290
+ const handleScroll = (0, import_react.useCallback)(() => {
291
+ const element = scrollRef.current;
292
+ const runtime = runtimeRef.current;
293
+ if (!element) {
294
+ return;
290
295
  }
291
- if (!scrollElement || !currentRuntime.isAtBottom) {
296
+ const expected = runtime.expectedScrollTop;
297
+ const layoutClamped = element.scrollHeight < runtime.lastScrollHeight && runtime.lastScrollTop > element.scrollHeight - element.clientHeight && Math.abs(
298
+ element.scrollTop - Math.max(0, element.scrollHeight - element.clientHeight)
299
+ ) <= SCROLL_EPSILON;
300
+ if (!runtime.userScrollIntent && (expected !== void 0 && Math.abs(expected - element.scrollTop) <= SCROLL_EPSILON || layoutClamped)) {
301
+ runtime.expectedScrollTop = void 0;
292
302
  return;
293
303
  }
294
- scrollElement.scrollTop = scrollElement.scrollHeight;
295
- updateVisibleRange(scrollElement, currentRuntime, dispatch);
296
- });
304
+ runtime.userScrollIntent = false;
305
+ runtime.expectedScrollTop = void 0;
306
+ runtime.isAtBottom = (0, import_virtualization.isNearBottom)({
307
+ scrollTop: element.scrollTop,
308
+ scrollHeight: element.scrollHeight,
309
+ clientHeight: element.clientHeight,
310
+ thresholdPx: geometryRef.current.bottomThresholdPx
311
+ });
312
+ captureAnchors(element, runtime);
313
+ if (runtime.pending.size > 0) {
314
+ flushMeasurements();
315
+ }
316
+ if (runtime.frameId === void 0) {
317
+ runtime.frameId = window.requestAnimationFrame(() => {
318
+ runtime.frameId = void 0;
319
+ updateVisibleRange();
320
+ });
321
+ }
322
+ }, [flushMeasurements, updateVisibleRange]);
297
323
  (0, import_react.useLayoutEffect)(() => {
298
- const currentRuntime = runtimeRef.current;
299
- const scrollElement = scrollRef.current;
300
- const bottomMessage = messages[0];
301
- const bottomMessageId = bottomMessage ? getMessageId(bottomMessage) : void 0;
302
- const previousLastMessageId = currentRuntime.lastMessageId;
303
- dispatch({ type: "syncPageWindow", pageCount, overscanPages });
304
- currentRuntime.lastMessageId = bottomMessageId;
305
- if (!scrollElement) {
324
+ const runtime = runtimeRef.current;
325
+ const element = scrollRef.current;
326
+ const replaced = runtime.pageSize !== pageSize || runtime.messageIds.size > 0 && !Array.from(messageIds).some((id) => runtime.messageIds.has(id));
327
+ const addedPageAtBottom = runtime.isAtBottom && pageCount > geometryRef.current.pageCount;
328
+ geometryRef.current = {
329
+ prefixHeights,
330
+ visibleRange,
331
+ pageCount,
332
+ overscanPages,
333
+ bottomThresholdPx
334
+ };
335
+ runtime.active = true;
336
+ runtime.messageIds = messageIds;
337
+ runtime.pageSize = pageSize;
338
+ if (replaced) {
339
+ cancelResizeTimers(runtime);
340
+ window.cancelAnimationFrame(runtime.frameId ?? 0);
341
+ runtime.frameId = void 0;
342
+ runtime.pending.clear();
343
+ runtime.anchors = [];
344
+ runtime.expectedScrollTop = void 0;
345
+ runtime.isAtBottom = true;
346
+ runtime.userScrollIntent = false;
347
+ runtime.needsRestore = true;
348
+ runtime.newPages = true;
349
+ for (const [index, page] of runtime.pages) {
350
+ page.height = page.element.getBoundingClientRect().height;
351
+ runtime.pending.set(index, page);
352
+ }
353
+ dispatch({ type: "reset", pageCount, overscanPages });
306
354
  return;
307
355
  }
308
- if (!currentRuntime.hasMounted) {
309
- scrollElement.scrollTop = scrollElement.scrollHeight;
310
- currentRuntime.isAtBottom = true;
311
- currentRuntime.hasMounted = true;
312
- updateVisibleRange(scrollElement, currentRuntime, dispatch);
313
- return;
356
+ if (!areRangesEqual(state.visibleRange, visibleRange) || state.visibleRange.last === -1 && pageCount > 0 || Array.from(state.measuredHeights.keys()).some(
357
+ (index) => index >= pageCount
358
+ )) {
359
+ dispatch({ type: "syncPageWindow", pageCount, overscanPages });
314
360
  }
315
- if (previousLastMessageId !== void 0 && previousLastMessageId !== bottomMessageId && currentRuntime.isAtBottom) {
316
- scrollElement.scrollTop = scrollElement.scrollHeight;
317
- updateVisibleRange(scrollElement, currentRuntime, dispatch);
361
+ if (addedPageAtBottom) {
362
+ runtime.needsRestore = true;
363
+ dispatch({
364
+ type: "setVisibleRange",
365
+ range: {
366
+ first: visibleRange.first,
367
+ last: pageCount - 1
368
+ }
369
+ });
318
370
  }
319
- }, [getMessageId, messages, overscanPages, pageCount]);
320
- (0, import_react.useEffect)(() => {
321
- return () => {
322
- const frameId = runtimeRef.current.frameId;
323
- if (frameId !== void 0) {
324
- window.cancelAnimationFrame(frameId);
371
+ if (runtime.newPages) {
372
+ runtime.newPages = false;
373
+ if (flushMeasurements()) {
374
+ return;
325
375
  }
326
- };
327
- }, []);
376
+ }
377
+ if (element && runtime.needsRestore && runtime.pending.size === 0) {
378
+ runtime.needsRestore = false;
379
+ restoreAnchors(element, runtime);
380
+ updateVisibleRange();
381
+ }
382
+ });
383
+ (0, import_react.useLayoutEffect)(
384
+ () => () => {
385
+ const runtime = runtimeRef.current;
386
+ runtime.active = false;
387
+ cancelResizeTimers(runtime);
388
+ window.cancelAnimationFrame(runtime.frameId ?? 0);
389
+ runtime.frameId = void 0;
390
+ runtime.pending.clear();
391
+ },
392
+ []
393
+ );
328
394
  return {
329
395
  scrollRef,
330
- visiblePages,
331
- spacerHeight: virtualWindowMetrics.total,
332
- windowBottom: virtualWindowMetrics.bottom + state.bottomAdjustment,
396
+ visiblePages: visibleRange.last >= visibleRange.first ? pages.slice(visibleRange.first, visibleRange.last + 1) : [],
397
+ spacerHeights: (0, import_virtualization.getSpacerHeights)(prefixHeights, visibleRange),
333
398
  handleScroll,
334
- handlePageHeightChange
399
+ handleScrollIntent,
400
+ registerPage
335
401
  };
336
402
  }
@@ -37,8 +37,3 @@ export declare function isNearBottom(args: {
37
37
  clientHeight: number;
38
38
  thresholdPx: number;
39
39
  }): boolean;
40
- export declare function shouldAdjustScrollForHeightChange(args: {
41
- changedPageIndex: number;
42
- firstVisiblePageIndex: number;
43
- isAtBottom: boolean;
44
- }): boolean;