An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Show scroll-down button in ChatPanel when user is not at bottom
Yoshiki Miura7a2eaf76436d
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex 07e3685f..8178b305 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -1,4 +1,3 @@-import { useAutoScroll } from '@/lib/hooks/use-auto-scroll'import { JSONValue, Message } from 'ai'import { useEffect, useMemo, useState } from 'react'import { RenderMessage } from './render-message'@@ -12,6 +11,8 @@ interface ChatMessagesProps {isLoading: booleanchatId?: stringaddToolResult?: (params: { toolCallId: string; result: any }) => void+ /** Ref for anchoring auto-scroll position */+ anchorRef: React.RefObject<HTMLDivElement>}export function ChatMessages({@@ -20,18 +21,12 @@ export function ChatMessages({onQuerySelect,isLoading,chatId,- addToolResult+ addToolResult,+ anchorRef}: ChatMessagesProps) {const [openStates, setOpenStates] = useState<Record<string, boolean>>({})const manualToolCallId = 'manual-tool-call'- // 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]if (lastMessage?.role === 'user') {@@ -116,7 +111,7 @@ export function ChatMessages({) : (<Spinner />))}- <div ref={anchorRef} /> {/* Auto-scroll anchor */}+ <div ref={anchorRef} /> {/* Anchor for auto-scroll */}</div>)}diff --git a/components/chat-panel.tsx b/components/chat-panel.tsxindex c854e69f..0411e87c 100644--- a/components/chat-panel.tsx+++ b/components/chat-panel.tsx@@ -3,7 +3,7 @@import { Model } from '@/lib/types/models'import { cn } from '@/lib/utils'import { Message } from 'ai'-import { ArrowUp, MessageCirclePlus, Square } from 'lucide-react'+import { ArrowDown, ArrowUp, MessageCirclePlus, Square } from 'lucide-react'import { useRouter } from 'next/navigation'import { useEffect, useRef, useState } from 'react'import Textarea from 'react-textarea-autosize'@@ -24,6 +24,8 @@ interface ChatPanelProps {stop: () => voidappend: (message: any) => voidmodels?: Model[]+ /** Whether auto-scroll is currently active (at bottom) */+ isAutoScroll: boolean}export function ChatPanel({@@ -36,7 +38,8 @@ export function ChatPanel({query,stop,append,- models+ models,+ isAutoScroll}: ChatPanelProps) {const [showEmptyScreen, setShowEmptyScreen] = useState(false)const router = useRouter()@@ -149,6 +152,24 @@ export function ChatPanel({onBlur={() => setShowEmptyScreen(false)}/>+ {/* Scroll-down button: show when user is not at bottom */}+ {!isAutoScroll && (+ <Button+ type="button"+ variant="outline"+ size="icon"+ className="absolute top-3 right-3 z-10"+ onClick={() =>+ window.scrollTo({+ top: document.documentElement.scrollHeight,+ behavior: 'smooth'+ })+ }+ >+ <ArrowDown size={20} />+ </Button>+ )}+{/* Bottom menu area */}<div className="flex items-center justify-between p-3"><div className="flex items-center gap-2">diff --git a/components/chat.tsx b/components/chat.tsxindex 590ab01d..50884e00 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -1,6 +1,7 @@'use client'import { CHAT_ID } from '@/lib/constants'+import { useAutoScroll } from '@/lib/hooks/use-auto-scroll'import { Model } from '@/lib/types/models'import { useChat } from '@ai-sdk/react'import { Message } from 'ai/react'@@ -50,6 +51,13 @@ export function Chat({const isLoading = status === 'submitted' || status === 'streaming'+ // Manage auto-scroll and user scroll cancel+ const { anchorRef, isAutoScroll } = useAutoScroll({+ isLoading,+ dependency: messages.length,+ isStreaming: () => status === 'streaming'+ })+useEffect(() => {setMessages(savedMessages)}, [id])@@ -76,6 +84,7 @@ export function Chat({isLoading={isLoading}chatId={id}addToolResult={addToolResult}+ anchorRef={anchorRef}/><ChatPanelinput={input}@@ -88,6 +97,7 @@ export function Chat({query={query}append={append}models={models}+ isAutoScroll={isAutoScroll}/></div>)