* Effect-only child that tracks the last user-prompt scrolled above the * viewport top and fires onChange when it changes. * * Rendered as a separate component (not a hook in VirtualMessageList) so it * can subscribe to scroll at FINER granularity than SCROLL_QUANTUM=40. The * list needs the co
({
messages,
previousStickyPromptIndex,
start,
end,
offsets,
getItemTop,
getItemElement,
scrollRef
}: {
messages: RenderableMessage[];
previousStickyPromptIndex: ArrayLike<number>;
start: number;
end: number;
offsets: ArrayLike<number>;
getItemTop: (index: number) => number;
getItemElement: (index: number) => DOMElement | null;
scrollRef: RefObject<ScrollBoxHandle | null>;
})
| 519 | * from the mount-range end; break when an item's top is above target. |
| 520 | */ |
| 521 | function StickyTracker({ |
| 522 | messages, |
| 523 | previousStickyPromptIndex, |
| 524 | start, |
| 525 | end, |
| 526 | offsets, |
| 527 | getItemTop, |
| 528 | getItemElement, |
| 529 | scrollRef |
| 530 | }: { |
| 531 | messages: RenderableMessage[]; |
| 532 | previousStickyPromptIndex: ArrayLike<number>; |
| 533 | start: number; |
| 534 | end: number; |
| 535 | offsets: ArrayLike<number>; |
| 536 | getItemTop: (index: number) => number; |
| 537 | getItemElement: (index: number) => DOMElement | null; |
| 538 | scrollRef: RefObject<ScrollBoxHandle | null>; |
| 539 | }): null { |
| 540 | const { |
| 541 | setStickyPrompt |
| 542 | } = useContext(ScrollChromeContext); |
| 543 | // Fine-grained subscription — snapshot is unquantized scrollTop+delta so |
| 544 | // every scroll action (wheel tick, PgUp, drag) triggers a re-render of |
| 545 | // THIS component only. Sticky bit folded into the sign so sticky→broken |
| 546 | // also triggers (scrollToBottom sets sticky without moving scrollTop). |
| 547 | const subscribe = useCallback((listener: () => void) => scrollRef.current?.subscribe(listener) ?? NOOP_UNSUB, [scrollRef]); |
| 548 | useSyncExternalStore(subscribe, () => { |
| 549 | const s = scrollRef.current; |
| 550 | if (!s) return NaN; |
| 551 | const t = s.getScrollTop() + s.getPendingDelta(); |
| 552 | return s.isSticky() ? -1 - t : t; |
| 553 | }); |
| 554 | |
| 555 | // Read live scroll state on every render. |
| 556 | const isSticky = scrollRef.current?.isSticky() ?? true; |
| 557 | const target = Math.max(0, (scrollRef.current?.getScrollTop() ?? 0) + (scrollRef.current?.getPendingDelta() ?? 0)); |
| 558 | |
| 559 | // Walk the mounted range to find the first item at-or-below the viewport |
| 560 | // top. `range` is from the parent's coarse-quantum render (may be slightly |
| 561 | // stale) but overscan guarantees it spans well past the viewport in both |
| 562 | // directions. Items without a Yoga layout yet (newly mounted this frame) |
| 563 | // are treated as at-or-below — they're somewhere in view, and assuming |
| 564 | // otherwise would show a sticky for a prompt that's actually on screen. |
| 565 | let firstVisible = start; |
| 566 | let firstVisibleTop = -1; |
| 567 | for (let i = end - 1; i >= start; i--) { |
| 568 | const top = getItemTop(i); |
| 569 | if (top >= 0) { |
| 570 | if (top < target) break; |
| 571 | firstVisibleTop = top; |
| 572 | } |
| 573 | firstVisible = i; |
| 574 | } |
| 575 | let idx = -1; |
| 576 | let text: string | null = null; |
| 577 | if (firstVisible > 0 && !isSticky) { |
| 578 | for (let i = previousStickyPromptIndex[firstVisible] ?? -1; i >= 0; i = previousStickyPromptIndex[i] ?? -1) { |
nothing calls this directly
no test coverage detected