An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add user scroll detection to cancel auto-scrolling during message streaming
Yoshiki Miuraa7e527588baf
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex 1dda3085..92c5ff92 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -27,6 +27,22 @@ export function ChatMessages({// Add ref for the messages containerconst 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 functionconst scrollToBottom = () => {messagesEndRef.current?.scrollIntoView({@@ -36,19 +52,23 @@ export function ChatMessages({// Scroll to bottom on mount and when messages change or during streaminguseEffect(() => {- scrollToBottom()+ if (isAutoScroll) scrollToBottom()// Set up interval for continuous scrolling during streaminglet intervalId: ReturnType<typeof setInterval> | undefined- if (isLoading && messages[messages.length - 1]?.role === 'user') {+ if (+ isAutoScroll &&+ isLoading &&+ messages[messages.length - 1]?.role === 'assistant'+ ) {intervalId = setInterval(scrollToBottom, 100)}return () => {if (intervalId) clearInterval(intervalId)}- }, [messages.length, isLoading, messages])+ }, [messages.length, isLoading, isAutoScroll])useEffect(() => {const lastMessage = messages[messages.length - 1]