import { useCallback, useEffect, useRef, type UIEvent, type WheelEvent } from 'react' const followDurationMs = 220 interface FollowAnimation { viewport: HTMLDivElement targetTop: number frame: number } export function useTranscriptScroll(onManualScroll: () => void) { const viewportRef = useRef(null) const scrollPosition = useRef(0) const animation = useRef(null) const cancelScroll = useCallback(() => { if (animation.current) cancelAnimationFrame(animation.current.frame) animation.current = null }, []) const setViewport = useCallback( (viewport: HTMLDivElement | null) => { if (viewportRef.current === viewport) return cancelScroll() viewportRef.current = viewport scrollPosition.current = viewport?.scrollTop ?? 0 }, [cancelScroll], ) useEffect(() => cancelScroll, [cancelScroll]) function setScrollTop(viewport: HTMLDivElement, top: number) { viewport.scrollTop = top scrollPosition.current = viewport.scrollTop } function detectManualScroll(viewport: HTMLDivElement) { const top = viewport.scrollTop if (top === scrollPosition.current) return false // A height or width change may clamp the browser's last scroll position. const clampedTop = Math.min( scrollPosition.current, viewport.scrollHeight - viewport.clientHeight, ) scrollPosition.current = top if (top === clampedTop) return false cancelScroll() onManualScroll() return true } function animateFollow(viewport: HTMLDivElement, top: number) { const targetTop = Math.max(0, Math.min(top, viewport.scrollHeight - viewport.clientHeight)) if (animation.current?.viewport === viewport) { if (detectManualScroll(viewport) || animation.current?.targetTop === targetTop) return } cancelScroll() const startTop = viewport.scrollTop scrollPosition.current = startTop if (startTop === targetTop) return if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) { setScrollTop(viewport, targetTop) return } const motion = { viewport, targetTop, frame: 0 } let startedAt: number | undefined const step = (time: number) => { if (animation.current !== motion) return if (viewportRef.current !== viewport) { animation.current = null return } if (detectManualScroll(viewport)) return startedAt ??= time const progress = Math.min(1, (time - startedAt) / followDurationMs) const eased = 1 - (1 - progress) ** 3 setScrollTop(viewport, startTop + (targetTop - startTop) * eased) if (progress === 1) animation.current = null else motion.frame = requestAnimationFrame(step) } animation.current = motion motion.frame = requestAnimationFrame(step) } function scrollTo( target: HTMLElement | null | undefined, { bottomInset = 0, followPlayback = false } = {}, ) { if (!followPlayback) cancelScroll() const viewport = viewportRef.current if (!viewport || !target) return const item = target.getBoundingClientRect() const { top, bottom } = viewport.getBoundingClientRect() if (followPlayback) { const height = bottom - top // Check where the segment will settle so nearby segments don't restart the motion. const pendingTop = animation.current?.viewport === viewport ? animation.current.targetTop : viewport.scrollTop const relativeTop = item.top - top const settledTop = relativeTop + viewport.scrollTop - pendingTop if (settledTop < 0 || settledTop + item.height / 2 >= height * 0.75) { const centerOffset = Math.max(0, (height - item.height) / 2) animateFollow(viewport, viewport.scrollTop + relativeTop - centerOffset) } return } const visibleBottom = bottom - bottomInset if (item.top < top || item.height > visibleBottom - top) { setScrollTop(viewport, viewport.scrollTop + item.top - top) } else if (item.bottom > visibleBottom) { setScrollTop(viewport, viewport.scrollTop + item.bottom - visibleBottom) } } function onScroll(event: UIEvent) { detectManualScroll(event.currentTarget) } function onWheel(event: WheelEvent) { if (event.deltaY !== 0 && !event.ctrlKey && animation.current) { cancelScroll() onManualScroll() } } return { viewportRef, setViewport, scrollTo, cancelScroll, onScroll, onWheel } }