import { useRef, useEffect, useCallback } from 'react' const SCROLL_THRESHOLD = 100 interface MessageInfo { role: string } interface Message { info: MessageInfo } interface UseAutoScrollOptions { containerRef?: React.RefObject messages?: T[] sessionId?: string onScrollStateChange?: (isScrolledUp: boolean) => void } interface UseAutoScrollReturn { scrollToBottom: () => void } export function useAutoScroll({ containerRef, messages, sessionId, onScrollStateChange }: UseAutoScrollOptions): UseAutoScrollReturn { const isFollowingRef = useRef(true) const lastMessageCountRef = useRef(0) const hasInitialScrolledRef = useRef(false) const scrollToBottom = useCallback(() => { if (!containerRef?.current) return containerRef.current.scrollTop = containerRef.current.scrollHeight isFollowingRef.current = true onScrollStateChange?.(false) }, [containerRef, onScrollStateChange]) useEffect(() => { isFollowingRef.current = true lastMessageCountRef.current = 0 hasInitialScrolledRef.current = false }, [sessionId]) useEffect(() => { if (!containerRef?.current) return const container = containerRef.current const handleScroll = () => { const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD if (isScrolledUp) { isFollowingRef.current = false } onScrollStateChange?.(isScrolledUp) } container.addEventListener('scroll', handleScroll, { passive: true }) return () => container.removeEventListener('scroll', handleScroll) }, [containerRef, onScrollStateChange]) useEffect(() => { if (!containerRef?.current || !messages) return const currentCount = messages.length const prevCount = lastMessageCountRef.current lastMessageCountRef.current = currentCount if (!hasInitialScrolledRef.current && currentCount > 0) { hasInitialScrolledRef.current = true scrollToBottom() return } if (currentCount > prevCount) { const newMessage = messages[currentCount - 1] if (newMessage?.info.role === 'user') { isFollowingRef.current = true scrollToBottom() return } } if (isFollowingRef.current) { scrollToBottom() } }, [messages, containerRef, scrollToBottom]) return { scrollToBottom } }