Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

2.9 KB
Raw Blame
import { useEffect, useRef } from 'react'

/**
 * Keep a chat list pinned to its bottom edge while the user is following.
 *
 * G33 (2026-08-07): on send the list jumped down "but never all the
 * way". Two mechanisms, both height that appears BELOW the fold without the
 * `dependency` string changing: the Working anchor mounts on a prop flip
 * (isGenerating), and a reasoning model streams into a thinking BLOCK while
 * `lastMessage.content` stays empty, so the trigger never fires and the view
 * parks one anchor-height short of the bottom for the whole thinking phase.
 *
 * So the pin no longer depends on guessing every trigger: a ResizeObserver on
 * the content wrapper re-pins whenever the content grows, as long as the user
 * is following. Scrolling up more than the threshold disengages following
 * (reading history during a run stays possible); `resumeKey` re-engages it —
 * the caller passes the last USER message id, because sending a message is an
 * explicit "take me to the newest", whatever the scroll position was.
 */
export function useAutoScroll(dependency: unknown, resumeKey?: unknown) {
  const ref = useRef<HTMLDivElement>(null)
  const contentRef = useRef<HTMLDivElement>(null)
  const shouldScroll = useRef(true)

  useEffect(() => {
    const el = ref.current
    if (!el) return

    const handleScroll = () => {
      const { scrollTop, scrollHeight, clientHeight } = el
      shouldScroll.current = scrollHeight - scrollTop - clientHeight < 100
    }

    el.addEventListener('scroll', handleScroll)
    return () => el.removeEventListener('scroll', handleScroll)
  }, [])

  // Follow every content-height change, not just the ones a trigger string
  // anticipated. Pinning inside the observer cannot loop: setting scrollTop
  // does not resize the content.
  // Re-attached on every new user message: the content element only mounts
  // once a transcript exists (the Code tab shows its empty state before), and
  // a one-time attach at mount never saw it. The scroll area itself is
  // watched too: a panel opening above it (pending writes in the Code tab)
  // shrinks it, and the bottom, with an approval card on it, slipped out of
  // view while nothing had scrolled (UX pass 30.09.2026).
  useEffect(() => {
    const content = contentRef.current
    const box = ref.current
    if (!content || !box || typeof ResizeObserver === 'undefined') return
    const ro = new ResizeObserver(() => {
      const el = ref.current
      if (shouldScroll.current && el) el.scrollTop = el.scrollHeight
    })
    ro.observe(content)
    ro.observe(box)
    return () => ro.disconnect()
  }, [resumeKey])

  useEffect(() => {
    if (resumeKey === undefined) return
    shouldScroll.current = true
    if (ref.current) ref.current.scrollTop = ref.current.scrollHeight
  }, [resumeKey])

  useEffect(() => {
    if (shouldScroll.current && ref.current) {
      ref.current.scrollTop = ref.current.scrollHeight
    }
  }, [dependency])

  return { ref, contentRef }
}