|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { type ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react' |
| 4 | +import { cn } from '@sim/emcn' |
| 5 | + |
| 6 | +interface ActivityViewportProps { |
| 7 | + children: ReactNode |
| 8 | + isStreaming: boolean |
| 9 | + /** A nested blocking interaction must not be clipped by this ancestor's log viewport. */ |
| 10 | + unbounded?: boolean |
| 11 | +} |
| 12 | + |
| 13 | +const BOTTOM_STICK_THRESHOLD_PX = 8 |
| 14 | + |
| 15 | +export function ActivityViewport({ |
| 16 | + children, |
| 17 | + isStreaming, |
| 18 | + unbounded = false, |
| 19 | +}: ActivityViewportProps) { |
| 20 | + const ref = useRef<HTMLDivElement>(null) |
| 21 | + const rafRef = useRef<number | null>(null) |
| 22 | + const stickToBottomRef = useRef(true) |
| 23 | + const prevScrollTopRef = useRef(0) |
| 24 | + const [hasOverflow, setHasOverflow] = useState(false) |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + if (unbounded) { |
| 28 | + stickToBottomRef.current = true |
| 29 | + return |
| 30 | + } |
| 31 | + const el = ref.current |
| 32 | + if (!el) return |
| 33 | + // Upward user input detaches auto-stick; a downward scroll reaching the |
| 34 | + // bottom re-attaches it (a small upward flick can't re-stick itself). |
| 35 | + const handleWheel = (e: WheelEvent) => { |
| 36 | + if (e.deltaY < 0) stickToBottomRef.current = false |
| 37 | + } |
| 38 | + const handleScroll = () => { |
| 39 | + const distance = el.scrollHeight - el.scrollTop - el.clientHeight |
| 40 | + if (distance < BOTTOM_STICK_THRESHOLD_PX && el.scrollTop > prevScrollTopRef.current) { |
| 41 | + stickToBottomRef.current = true |
| 42 | + } |
| 43 | + prevScrollTopRef.current = el.scrollTop |
| 44 | + } |
| 45 | + el.addEventListener('wheel', handleWheel, { passive: true }) |
| 46 | + el.addEventListener('scroll', handleScroll, { passive: true }) |
| 47 | + return () => { |
| 48 | + el.removeEventListener('wheel', handleWheel) |
| 49 | + el.removeEventListener('scroll', handleScroll) |
| 50 | + } |
| 51 | + }, [unbounded]) |
| 52 | + |
| 53 | + useLayoutEffect(() => { |
| 54 | + const el = ref.current |
| 55 | + if (rafRef.current !== null) { |
| 56 | + window.cancelAnimationFrame(rafRef.current) |
| 57 | + rafRef.current = null |
| 58 | + } |
| 59 | + if (unbounded) { |
| 60 | + setHasOverflow(false) |
| 61 | + return |
| 62 | + } |
| 63 | + if (el) { |
| 64 | + const next = el.scrollHeight > el.clientHeight |
| 65 | + setHasOverflow((prev) => (prev === next ? prev : next)) |
| 66 | + } |
| 67 | + if (!isStreaming) return |
| 68 | + const tick = () => { |
| 69 | + const node = ref.current |
| 70 | + if (!node || !stickToBottomRef.current) { |
| 71 | + rafRef.current = null |
| 72 | + return |
| 73 | + } |
| 74 | + const target = node.scrollHeight - node.clientHeight |
| 75 | + const gap = target - node.scrollTop |
| 76 | + if (gap < 1) { |
| 77 | + rafRef.current = null |
| 78 | + return |
| 79 | + } |
| 80 | + node.scrollTop = node.scrollTop + Math.max(1, gap * 0.18) |
| 81 | + rafRef.current = window.requestAnimationFrame(tick) |
| 82 | + } |
| 83 | + rafRef.current = window.requestAnimationFrame(tick) |
| 84 | + return () => { |
| 85 | + if (rafRef.current !== null) { |
| 86 | + window.cancelAnimationFrame(rafRef.current) |
| 87 | + rafRef.current = null |
| 88 | + } |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + return ( |
| 93 | + <div className='relative'> |
| 94 | + <div |
| 95 | + ref={ref} |
| 96 | + className={cn( |
| 97 | + 'pr-2', |
| 98 | + !unbounded && 'scrollbar-hide max-h-[110px] overflow-y-auto', |
| 99 | + hasOverflow && 'py-1' |
| 100 | + )} |
| 101 | + > |
| 102 | + {children} |
| 103 | + </div> |
| 104 | + {!unbounded && hasOverflow && ( |
| 105 | + <> |
| 106 | + <div className='pointer-events-none absolute top-0 right-2 left-0 h-3 bg-linear-to-b from-[var(--bg)] to-transparent' /> |
| 107 | + <div className='pointer-events-none absolute right-2 bottom-0 left-0 h-3 bg-linear-to-t from-[var(--bg)] to-transparent' /> |
| 108 | + </> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + ) |
| 112 | +} |
0 commit comments