mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
139 lines
3.9 KiB
TypeScript
139 lines
3.9 KiB
TypeScript
import { useRef, useEffect, useCallback } from 'react'
|
|
|
|
const SCROLL_LOCK_MS = 300
|
|
|
|
interface MessageInfo {
|
|
role: string
|
|
}
|
|
|
|
interface Message {
|
|
info: MessageInfo
|
|
}
|
|
|
|
interface UseAutoScrollOptions<T extends Message> {
|
|
containerRef?: React.RefObject<HTMLDivElement | null>
|
|
messages?: T[]
|
|
sessionId?: string
|
|
onScrollStateChange?: (isScrolledUp: boolean) => void
|
|
}
|
|
|
|
interface UseAutoScrollReturn {
|
|
scrollToBottom: () => void
|
|
}
|
|
|
|
export function useAutoScroll<T extends Message>({
|
|
containerRef,
|
|
messages,
|
|
sessionId,
|
|
onScrollStateChange
|
|
}: UseAutoScrollOptions<T>): UseAutoScrollReturn {
|
|
const lastMessageCountRef = useRef(0)
|
|
const hasInitialScrolledRef = useRef(false)
|
|
const userScrolledAtRef = useRef(0)
|
|
const userDisengagedRef = useRef(false)
|
|
const pointerStartYRef = useRef<number | null>(null)
|
|
|
|
const scrollToBottom = useCallback(() => {
|
|
if (!containerRef?.current) return
|
|
userScrolledAtRef.current = 0
|
|
userDisengagedRef.current = false
|
|
containerRef.current.scrollTop = containerRef.current.scrollHeight
|
|
onScrollStateChange?.(false)
|
|
}, [containerRef, onScrollStateChange])
|
|
|
|
useEffect(() => {
|
|
lastMessageCountRef.current = 0
|
|
hasInitialScrolledRef.current = false
|
|
userScrolledAtRef.current = 0
|
|
userDisengagedRef.current = false
|
|
}, [sessionId])
|
|
|
|
useEffect(() => {
|
|
if (!containerRef?.current) return
|
|
|
|
const container = containerRef.current
|
|
|
|
const markDisengaged = () => {
|
|
userScrolledAtRef.current = Date.now()
|
|
userDisengagedRef.current = true
|
|
onScrollStateChange?.(true)
|
|
}
|
|
|
|
const handlePointerDown = (e: PointerEvent) => {
|
|
pointerStartYRef.current = e.clientY
|
|
}
|
|
|
|
const handlePointerMove = (e: PointerEvent) => {
|
|
if (pointerStartYRef.current === null) return
|
|
if (e.clientY > pointerStartYRef.current) {
|
|
markDisengaged()
|
|
}
|
|
}
|
|
|
|
const handlePointerUp = () => {
|
|
pointerStartYRef.current = null
|
|
}
|
|
|
|
const handleWheel = (e: WheelEvent) => {
|
|
if (e.deltaY < 0) {
|
|
markDisengaged()
|
|
}
|
|
}
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (['PageUp', 'ArrowUp', 'Home'].includes(e.key)) {
|
|
markDisengaged()
|
|
}
|
|
}
|
|
|
|
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('wheel', handleWheel, { passive: true })
|
|
container.addEventListener('keydown', handleKeyDown)
|
|
|
|
return () => {
|
|
container.removeEventListener('pointerdown', handlePointerDown)
|
|
container.removeEventListener('pointermove', handlePointerMove)
|
|
container.removeEventListener('pointerup', handlePointerUp)
|
|
container.removeEventListener('pointercancel', handlePointerUp)
|
|
container.removeEventListener('wheel', handleWheel)
|
|
container.removeEventListener('keydown', handleKeyDown)
|
|
}
|
|
}, [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') {
|
|
scrollToBottom()
|
|
return
|
|
}
|
|
}
|
|
|
|
const timeSinceUserScroll = Date.now() - userScrolledAtRef.current
|
|
const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS
|
|
|
|
if (recentlyScrolled || userDisengagedRef.current) {
|
|
return
|
|
}
|
|
|
|
scrollToBottom()
|
|
}, [messages, containerRef, scrollToBottom])
|
|
|
|
return { scrollToBottom }
|
|
}
|