Skip to content

An AI-powered search engine with a generative UI

Extract auto-scroll behavior into useAutoScroll hook and refactor ChatMessages

Yoshiki Miurafc1be49488e1
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 92c5ff92..07e3685f 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -1,5 +1,6 @@
+import { useAutoScroll } from '@/lib/hooks/use-auto-scroll'
import { JSONValue, Message } from 'ai'
-import { useEffect, useMemo, useRef, useState } from 'react'
+import { useEffect, useMemo, useState } from 'react'
import { RenderMessage } from './render-message'
import { ToolSection } from './tool-section'
import { Spinner } from './ui/spinner'
@@ -24,51 +25,12 @@ export function ChatMessages({
const [openStates, setOpenStates] = useState<Record<string, boolean>>({})
const manualToolCallId = 'manual-tool-call'
- // Add ref for the messages container
- const messagesEndRef = useRef<HTMLDivElement>(null)
-
- // Add isAutoScroll state to enable/disable auto-scrolling
- const [isAutoScroll, setIsAutoScroll] = useState(true)
-
- // Listen to user scroll to toggle auto-scroll
- useEffect(() => {
- const handleScroll = () => {
- const threshold = 40
- const scrollHeight = document.documentElement.scrollHeight
- const atBottom =
- window.innerHeight + window.scrollY >= scrollHeight - threshold
- setIsAutoScroll(atBottom)
- }
- window.addEventListener('scroll', handleScroll, { passive: true })
- return () => window.removeEventListener('scroll', handleScroll)
- }, [])
-
- // Scroll to bottom function
- const scrollToBottom = () => {
- messagesEndRef.current?.scrollIntoView({
- behavior: messages.length > 5 ? 'instant' : 'smooth'
- })
- }
-
- // Scroll to bottom on mount and when messages change or during streaming
- useEffect(() => {
- if (isAutoScroll) scrollToBottom()
-
- // Set up interval for continuous scrolling during streaming
- let intervalId: ReturnType<typeof setInterval> | undefined
-
- if (
- isAutoScroll &&
- isLoading &&
- messages[messages.length - 1]?.role === 'assistant'
- ) {
- intervalId = setInterval(scrollToBottom, 100)
- }
-
- return () => {
- if (intervalId) clearInterval(intervalId)
- }
- }, [messages.length, isLoading, isAutoScroll])
+ // Use auto-scroll hook for continuous scrolling with user-cancel
+ const { anchorRef } = useAutoScroll({
+ isLoading,
+ dependency: messages.length,
+ isStreaming: () => messages[messages.length - 1]?.role === 'assistant'
+ })
useEffect(() => {
const lastMessage = messages[messages.length - 1]
@@ -154,7 +116,7 @@ export function ChatMessages({
) : (
<Spinner />
))}
- <div ref={messagesEndRef} /> {/* Add empty div as scroll anchor */}
+ <div ref={anchorRef} /> {/* Auto-scroll anchor */}
</div>
)
}
diff --git a/lib/hooks/use-auto-scroll.ts b/lib/hooks/use-auto-scroll.ts
new file mode 100644
index 00000000..515b2eb6
--- /dev/null
+++ b/lib/hooks/use-auto-scroll.ts
@@ -0,0 +1,74 @@
+import { useCallback, useEffect, useRef, useState } from 'react'
+
+interface UseAutoScrollOptions {
+ isLoading: boolean
+ dependency: number
+ isStreaming: () => boolean
+ threshold?: number
+ intervalMs?: number
+}
+
+interface UseAutoScrollReturn {
+ anchorRef: React.RefObject<HTMLDivElement>
+ isAutoScroll: boolean
+}
+
+/**
+ * Custom hook to auto-scroll to a target element and pause when the user scrolls away.
+ */
+export function useAutoScroll({
+ isLoading,
+ dependency,
+ isStreaming,
+ threshold = 162,
+ intervalMs = 100
+}: UseAutoScrollOptions): UseAutoScrollReturn {
+ const anchorRef = useRef<HTMLDivElement>(null)
+ const [isAutoScroll, setIsAutoScroll] = useState(true)
+
+ // Detect user scroll to toggle auto-scroll
+ const handleScroll = useCallback(() => {
+ if (typeof window === 'undefined') return
+ const scrollHeight = document.documentElement.scrollHeight
+ const atBottom =
+ window.innerHeight + window.scrollY >= scrollHeight - threshold
+ setIsAutoScroll(atBottom)
+ }, [threshold])
+
+ useEffect(() => {
+ if (typeof window === 'undefined') return
+ window.addEventListener('scroll', handleScroll, { passive: true })
+ return () => {
+ window.removeEventListener('scroll', handleScroll)
+ }
+ }, [handleScroll])
+
+ // Scroll to anchor element
+ const scrollToBottom = useCallback(() => {
+ anchorRef.current?.scrollIntoView({
+ behavior: dependency > 5 ? 'instant' : 'smooth'
+ })
+ }, [dependency])
+
+ // Auto-scroll on updates and during streaming
+ useEffect(() => {
+ if (!isAutoScroll) return
+ scrollToBottom()
+ let intervalId: ReturnType<typeof setInterval> | undefined
+ if (isAutoScroll && isStreaming() && isLoading) {
+ intervalId = setInterval(scrollToBottom, intervalMs)
+ }
+ return () => {
+ if (intervalId) clearInterval(intervalId)
+ }
+ }, [
+ dependency,
+ isLoading,
+ isAutoScroll,
+ isStreaming,
+ intervalMs,
+ scrollToBottom
+ ])
+
+ return { anchorRef, isAutoScroll }
+}