import { useRef, useEffect, useCallback } from 'react' import type { SessionMessageInfo } from '@opencode-manager/shared/opencode' const SCROLL_LOCK_MS = 300 const BOTTOM_THRESHOLD_PX = 48 const SHOW_SCROLL_BUTTON_THRESHOLD_PX = 120 const SCROLL_TO_BOTTOM_FRAME_COUNT = 2 interface UseAutoScrollOptions { container?: HTMLDivElement | null messages?: SessionMessageInfo[] sessionId?: string contentVersion?: number isContentReady: boolean onScrollStateChange?: (isScrolledUp: boolean) => void } interface UseAutoScrollReturn { scrollToBottom: () => void scrollByUser: (delta: number) => void } export function useAutoScroll({ container, messages, sessionId, contentVersion, isContentReady, onScrollStateChange }: UseAutoScrollOptions): UseAutoScrollReturn { const lastMessageCountRef = useRef(0) const lastMessageIdRef = useRef(undefined) const hasInitialScrolledRef = useRef(false) const userScrolledAtRef = useRef(0) const userDisengagedRef = useRef(false) const pointerStartYRef = useRef(null) const pointerActiveRef = useRef(false) const touchStartYRef = useRef(null) const touchActiveRef = useRef(false) const onScrollStateChangeRef = useRef(onScrollStateChange) const scrollRequestIdRef = useRef(0) const isScrollButtonVisibleRef = useRef(false) onScrollStateChangeRef.current = onScrollStateChange const scrollToBottom = useCallback(() => { if (!container) return userScrolledAtRef.current = 0 userDisengagedRef.current = false isScrollButtonVisibleRef.current = false const scrollRequestId = scrollRequestIdRef.current + 1 scrollRequestIdRef.current = scrollRequestId let frameCount = 0 const scrollAfterLayout = () => { if (scrollRequestIdRef.current !== scrollRequestId) return container.scrollTop = container.scrollHeight frameCount += 1 if (frameCount < SCROLL_TO_BOTTOM_FRAME_COUNT) { requestAnimationFrame(scrollAfterLayout) } } requestAnimationFrame(scrollAfterLayout) onScrollStateChangeRef.current?.(false) }, [container]) const markDisengaged = useCallback(() => { if (!container) return const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight userScrolledAtRef.current = Date.now() scrollRequestIdRef.current += 1 if (!isScrollButtonVisibleRef.current && distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { isScrollButtonVisibleRef.current = true onScrollStateChangeRef.current?.(true) } userDisengagedRef.current = true }, [container]) const scrollByUser = useCallback((delta: number) => { if (!container) return if (delta < 0) { markDisengaged() } container.scrollBy({ top: delta }) }, [container, markDisengaged]) useEffect(() => { lastMessageCountRef.current = 0 lastMessageIdRef.current = undefined hasInitialScrolledRef.current = false userScrolledAtRef.current = 0 userDisengagedRef.current = false touchStartYRef.current = null touchActiveRef.current = false }, [sessionId]) useEffect(() => { if (!container) return const updateScrollButtonVisibility = () => { if (!userDisengagedRef.current || isScrollButtonVisibleRef.current) return const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight if (distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { isScrollButtonVisibleRef.current = true onScrollStateChangeRef.current?.(true) } } const handlePointerDown = (e: PointerEvent) => { pointerStartYRef.current = e.clientY pointerActiveRef.current = true scrollRequestIdRef.current += 1 } const handlePointerMove = (e: PointerEvent) => { if (pointerStartYRef.current === null) return if (e.clientY > pointerStartYRef.current + 4) { markDisengaged() } } const handlePointerUp = () => { pointerStartYRef.current = null pointerActiveRef.current = false } const handleTouchStart = (e: TouchEvent) => { const touch = e.touches[0] if (!touch) return touchStartYRef.current = touch.clientY touchActiveRef.current = true scrollRequestIdRef.current += 1 } const handleTouchMove = (e: TouchEvent) => { const touch = e.touches[0] if (!touch || touchStartYRef.current === null) return if (touch.clientY > touchStartYRef.current + 4) { markDisengaged() } } const handleTouchEnd = () => { touchStartYRef.current = null touchActiveRef.current = false } const handleWheel = (e: WheelEvent) => { if (e.deltaY < 0) { markDisengaged() } } const handleKeyDown = (e: KeyboardEvent) => { if (['PageUp', 'ArrowUp', 'Home'].includes(e.key)) { markDisengaged() } } const handleScroll = () => { if (!hasInitialScrolledRef.current) return const { scrollTop, scrollHeight, clientHeight } = container const isAtBottom = scrollHeight - scrollTop - clientHeight < BOTTOM_THRESHOLD_PX if (isAtBottom) { if (userDisengagedRef.current) { userScrolledAtRef.current = 0 userDisengagedRef.current = false isScrollButtonVisibleRef.current = false onScrollStateChangeRef.current?.(false) } } else if (userDisengagedRef.current && !isScrollButtonVisibleRef.current) { updateScrollButtonVisibility() } else if ((pointerActiveRef.current || touchActiveRef.current) && !userDisengagedRef.current) { userScrolledAtRef.current = Date.now() userDisengagedRef.current = true updateScrollButtonVisibility() } } container.addEventListener('pointerdown', handlePointerDown, { passive: true }) container.addEventListener('pointermove', handlePointerMove, { passive: true }) container.addEventListener('pointerup', handlePointerUp, { passive: true }) container.addEventListener('pointercancel', handlePointerUp, { passive: true }) container.addEventListener('touchstart', handleTouchStart, { passive: true }) container.addEventListener('touchmove', handleTouchMove, { passive: true }) container.addEventListener('touchend', handleTouchEnd, { passive: true }) container.addEventListener('touchcancel', handleTouchEnd, { passive: true }) container.addEventListener('wheel', handleWheel, { passive: true }) container.addEventListener('keydown', handleKeyDown) container.addEventListener('scroll', handleScroll, { passive: true }) return () => { container.removeEventListener('pointerdown', handlePointerDown) container.removeEventListener('pointermove', handlePointerMove) container.removeEventListener('pointerup', handlePointerUp) container.removeEventListener('pointercancel', handlePointerUp) container.removeEventListener('touchstart', handleTouchStart) container.removeEventListener('touchmove', handleTouchMove) container.removeEventListener('touchend', handleTouchEnd) container.removeEventListener('touchcancel', handleTouchEnd) container.removeEventListener('wheel', handleWheel) container.removeEventListener('keydown', handleKeyDown) container.removeEventListener('scroll', handleScroll) } }, [container, sessionId, messages, markDisengaged]) useEffect(() => { if (!container || !messages) return const currentCount = messages.length const prevCount = lastMessageCountRef.current lastMessageCountRef.current = currentCount const lastMessage = messages[currentCount - 1] const previousLastMessageId = lastMessageIdRef.current lastMessageIdRef.current = lastMessage?.id if (!isContentReady) return if (!hasInitialScrolledRef.current && currentCount > 0) { hasInitialScrolledRef.current = true scrollToBottom() return } if ( currentCount > prevCount && lastMessage?.type === 'user' && lastMessage.id !== previousLastMessageId ) { scrollToBottom() return } const timeSinceUserScroll = Date.now() - userScrolledAtRef.current const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS if (userDisengagedRef.current) { const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight if (!isScrollButtonVisibleRef.current && distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) { isScrollButtonVisibleRef.current = true onScrollStateChangeRef.current?.(true) } return } if (recentlyScrolled || pointerActiveRef.current || touchActiveRef.current) { return } scrollToBottom() }, [messages, container, isContentReady, scrollToBottom, contentVersion]) return { scrollToBottom, scrollByUser } }