MCPcopy Create free account
hub / github.com/Noumena-Network/code / useUnseenDivider

Function useUnseenDivider

src/components/FullscreenLayout.tsx:87–191  ·  view source on GitHub ↗
(messageCount: number)

Source from the content-addressed store, hash-verified

85 * handle; `onRepin` by submit/scroll-to-bottom.
86 */
87export function useUnseenDivider(messageCount: number): {
88 /** Index into messages[] where the divider line renders. Cleared on
89 * sticky-resume (scroll back to bottom) so the "N new" line doesn't
90 * linger once everything is visible. */
91 dividerIndex: number | null;
92 /** scrollHeight snapshot at first scroll-away — the divider's y-position.
93 * FullscreenLayout subscribes to ScrollBox and compares viewport bottom
94 * against this for pillVisible. Ref so writes don't re-render REPL. */
95 dividerYRef: RefObject<number | null>;
96 onScrollAway: (handle: ScrollBoxHandle) => void;
97 onRepin: () => void;
98 /** Scroll the handle so the divider line is at the top of the viewport. */
99 jumpToNew: (handle: ScrollBoxHandle | null) => void;
100 /** Shift dividerIndex and dividerYRef when messages are prepended
101 * (infinite scroll-back). indexDelta = number of messages prepended;
102 * heightDelta = content height growth in rows. */
103 shiftDivider: (indexDelta: number, heightDelta: number) => void;
104} {
105 const [dividerIndex, setDividerIndex] = useState<number | null>(null);
106 // Ref holds the current count for onScrollAway to snapshot. Written in
107 // the render body (not useEffect) so wheel events arriving between a
108 // message-append render and its effect flush don't capture a stale
109 // count (off-by-one in the baseline). React Compiler bails out here —
110 // acceptable for a hook instantiated once in REPL.
111 const countRef = useRef(messageCount);
112 countRef.current = messageCount;
113 // scrollHeight snapshot — the divider's y in content coords. Ref-only:
114 // read synchronously in onScrollAway (setState is batched, can't
115 // read-then-write in the same callback) AND by FullscreenLayout's
116 // pillVisible subscription. null = pinned to bottom.
117 const dividerYRef = useRef<number | null>(null);
118 const onRepin = useCallback(() => {
119 // Don't clear dividerYRef here — a trackpad momentum wheel event
120 // racing in the same stdin batch would see null and re-snapshot,
121 // overriding the setDividerIndex(null) below. The useEffect below
122 // clears the ref after React commits the null dividerIndex, so the
123 // ref stays non-null until the state settles.
124 setDividerIndex(null);
125 }, []);
126 const onScrollAway = useCallback((handle: ScrollBoxHandle) => {
127 // Nothing below the viewport → nothing to jump to. Covers both:
128 // • empty/short session: scrollUp calls scrollTo(0) which breaks sticky
129 // even at scrollTop=0 (wheel-up on fresh session showed the pill)
130 // • click-to-select at bottom: useDragToScroll.check() calls
131 // scrollTo(current) to break sticky so streaming content doesn't shift
132 // under the selection, then onScroll(false, …) — but scrollTop is still
133 // at max (Sarah Deaton, #claude-code-feedback 2026-03-15)
134 // pendingDelta: scrollBy accumulates without updating scrollTop. Without
135 // it, wheeling up from max would see scrollTop==max and suppress the pill.
136 const max = Math.max(0, handle.getScrollHeight() - handle.getViewportHeight());
137 if (handle.getScrollTop() + handle.getPendingDelta() >= max) return;
138 // Snapshot only on the FIRST scroll-away. onScrollAway fires on EVERY
139 // scroll action (not just the initial break from sticky) — this guard
140 // preserves the original baseline so the count doesn't reset on the
141 // second PageUp. Subsequent calls are ref-only no-ops (no REPL re-render).
142 if (dividerYRef.current === null) {
143 dividerYRef.current = handle.getScrollHeight();
144 // New scroll-away session → move the divider here (replaces old one)

Callers 1

REPLFunction · 0.85

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected