"use client"; import { useCallback, useEffect, useRef } from "react"; interface AutoScrollOptions { hasMessages: boolean; isStreaming: boolean; composerHeight: number; messageCount: number; lastMessageContent?: string; lastEventCount?: number; } const THROTTLE_MS = 80; export function useChatAutoScroll({ hasMessages, isStreaming, composerHeight, messageCount, lastMessageContent, lastEventCount, }: AutoScrollOptions) { const containerRef = useRef(null); const endRef = useRef(null); const shouldAutoScrollRef = useRef(true); const lastScrollTimeRef = useRef(0); const pendingRafRef = useRef(0); const scrollToBottom = useCallback((behavior: ScrollBehavior) => { const container = containerRef.current; if (!container) return; container.scrollTo({ top: container.scrollHeight, behavior, }); }, []); useEffect(() => { if (!shouldAutoScrollRef.current) return; const now = performance.now(); const elapsed = now - lastScrollTimeRef.current; if (isStreaming && elapsed < THROTTLE_MS) { if (pendingRafRef.current) return; pendingRafRef.current = window.setTimeout(() => { pendingRafRef.current = 0; if (shouldAutoScrollRef.current) { scrollToBottom("instant"); lastScrollTimeRef.current = performance.now(); } }, THROTTLE_MS - elapsed); return () => { if (pendingRafRef.current) { clearTimeout(pendingRafRef.current); pendingRafRef.current = 0; } }; } const raf = window.requestAnimationFrame(() => { scrollToBottom(isStreaming ? "instant" : "smooth"); lastScrollTimeRef.current = performance.now(); }); return () => { window.cancelAnimationFrame(raf); if (pendingRafRef.current) { clearTimeout(pendingRafRef.current); pendingRafRef.current = 0; } }; }, [ isStreaming, lastEventCount, lastMessageContent, messageCount, scrollToBottom, ]); useEffect(() => { if (!hasMessages || !shouldAutoScrollRef.current) return; const raf = window.requestAnimationFrame(() => { scrollToBottom("instant"); }); return () => window.cancelAnimationFrame(raf); }, [composerHeight, hasMessages, scrollToBottom]); // After streaming ends, dynamically-loaded components (e.g. MathAnimatorViewer // via next/dynamic) may render and grow the content height. Detect that and // scroll to bottom so the user can see the full result. // hasMessages is in deps so the observer attaches once the messages // container mounts on session reopen — without it, the container ref is null // on initial mount and the observer is never set up. useEffect(() => { if (isStreaming) return; if (!hasMessages) return; const container = containerRef.current; if (!container) return; let prevHeight = container.scrollHeight; let rafId = 0; const check = () => { if (rafId) return; rafId = requestAnimationFrame(() => { rafId = 0; const curHeight = container.scrollHeight; if (curHeight > prevHeight && shouldAutoScrollRef.current) { scrollToBottom("instant"); } prevHeight = curHeight; }); }; const mo = new MutationObserver(check); mo.observe(container, { childList: true, subtree: true }); return () => { mo.disconnect(); if (rafId) cancelAnimationFrame(rafId); }; }, [hasMessages, isStreaming, scrollToBottom]); const handleScroll = useCallback(() => { const container = containerRef.current; if (!container) return; const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight; shouldAutoScrollRef.current = distanceFromBottom < 80; }, []); return { containerRef, endRef, shouldAutoScrollRef, scrollToBottom, handleScroll, }; }