Skip to content

An AI-powered search engine with a generative UI

fix: refresh sidebar history when new chat is created

Yoshiki Miura256571532e97
diff --git a/components/chat.tsx b/components/chat.tsx
index 5baffc4f..20dedbb4 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -7,7 +7,8 @@ import { cn } from '@/lib/utils'
import { useChat } from '@ai-sdk/react'
import { ChatRequestOptions } from 'ai'
import { Message } from 'ai/react'
-import { useEffect, useRef } from 'react'
+import { useRouter } from 'next/navigation'
+import { useEffect, useRef, useTransition } from 'react'
import { toast } from 'sonner'
import { ChatMessages } from './chat-messages'
import { ChatPanel } from './chat-panel'
@@ -24,6 +25,8 @@ export function Chat({
models?: Model[]
}) {
const scrollContainerRef = useRef<HTMLDivElement>(null)
+ const router = useRouter()
+ const [isPending, startTransition] = useTransition()
const {
messages,
@@ -45,7 +48,10 @@ export function Chat({
id
},
onFinish: () => {
- window.history.replaceState({}, '', `/search/${id}`)
+ router.replace(`/search/${id}`)
+ startTransition(() => {
+ router.refresh()
+ })
},
onError: error => {
toast.error(`Error in chat: ${error.message}`)
diff --git a/components/sidebar/chat-history-section.tsx b/components/sidebar/chat-history-section.tsx
index 7ab6c451..b4cb67c0 100644
--- a/components/sidebar/chat-history-section.tsx
+++ b/components/sidebar/chat-history-section.tsx
@@ -5,14 +5,9 @@ import {
} from '@/components/ui/sidebar'
import { getChats } from '@/lib/actions/chat'
import { Chat } from '@/lib/types'
-import { cache } from 'react'
import { ChatMenuItem } from './chat-menu-item'
import { ClearHistoryAction } from './clear-history-action'
-const loadChats = cache(async (userId?: string) => {
- return await getChats(userId)
-})
-
export async function ChatHistorySection() {
const enableSaveChatHistory = process.env.ENABLE_SAVE_CHAT_HISTORY === 'true'
if (!enableSaveChatHistory) {
@@ -20,7 +15,7 @@ export async function ChatHistorySection() {
}
// Replace with your own user ID
- const chats = await loadChats('anonymous')
+ const chats = await getChats('anonymous')
return (
<div className="flex flex-col flex-1 h-full">