Skip to content

An AI-powered search engine with a generative UI

Refactor chat component and history container

Yoshiki Miura010ed8e0e1c7
diff --git a/components/chat.tsx b/components/chat.tsx
index 04f37544..2ace4575 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -1,10 +1,10 @@
'use client'
import { useEffect } from 'react'
-import { usePathname, useRouter, useSearchParams } from 'next/navigation'
+import { usePathname } from 'next/navigation'
import { ChatPanel } from './chat-panel'
import { ChatMessages } from './chat-messages'
-import { useUIState, useAIState } from 'ai/rsc'
+import { useUIState } from 'ai/rsc'
type ChatProps = {
id?: string
@@ -12,10 +12,8 @@ type ChatProps = {
}
export function Chat({ id, query }: ChatProps) {
- const router = useRouter()
const path = usePathname()
const [messages] = useUIState()
- const [aiState] = useAIState()
useEffect(() => {
if (
@@ -26,13 +24,6 @@ export function Chat({ id, query }: ChatProps) {
}
}, [id, path, messages, query])
- useEffect(() => {
- if (aiState.messages[aiState.messages.length - 1]?.type === 'followup') {
- // Refresh the page to chat history updates
- router.refresh()
- }
- }, [aiState, router])
-
return (
<div className="px-8 sm:px-12 pt-12 md:pt-14 pb-14 md:pb-24 max-w-3xl mx-auto flex flex-col space-y-3 md:space-y-4">
<ChatMessages messages={messages} />
diff --git a/components/history-container.tsx b/components/history-container.tsx
index 0bf37705..16488e53 100644
--- a/components/history-container.tsx
+++ b/components/history-container.tsx
@@ -1,5 +1,6 @@
import React from 'react'
import { History } from './history'
+import { HistoryList } from './history-list'
type HistoryContainerProps = {
location: 'sidebar' | 'header'
@@ -9,8 +10,12 @@ const HistoryContainer: React.FC<HistoryContainerProps> = async ({
location
}) => {
return (
- <div className="sm:hidden block">
- <History location={location} />
+ <div
+ className={location === 'header' ? 'block sm:hidden' : 'hidden sm:block'}
+ >
+ <History location={location}>
+ <HistoryList userId="anonymous" />
+ </History>
</div>
)
}
diff --git a/components/history-list.tsx b/components/history-list.tsx
index 807e0403..12ced3e3 100644
--- a/components/history-list.tsx
+++ b/components/history-list.tsx
@@ -18,7 +18,7 @@ export async function HistoryList({ userId }: HistoryListProps) {
return (
<div className="flex flex-col flex-1 space-y-3 h-full">
- <div className="flex flex-col gap-2 flex-1 overflow-y-auto">
+ <div className="flex flex-col space-y-0.5 flex-1 overflow-y-auto">
{!chats?.length ? (
<div className="text-foreground/30 text-sm text-center py-4">
No search history
diff --git a/components/history-skelton.tsx b/components/history-skelton.tsx
index c0e257e5..e21c1d85 100644
--- a/components/history-skelton.tsx
+++ b/components/history-skelton.tsx
@@ -3,7 +3,7 @@ import { Skeleton } from './ui/skeleton'
export function HistorySkeleton() {
return (
- <div className="flex flex-col flex-1 space-y-4 overflow-auto">
+ <div className="flex flex-col flex-1 space-y-1.5 overflow-auto">
{Array.from({ length: 3 }).map((_, i) => (
<Skeleton key={i} className="w-full h-12 rounded" />
))}
diff --git a/components/history.tsx b/components/history.tsx
index 1920d467..a7e99672 100644
--- a/components/history.tsx
+++ b/components/history.tsx
@@ -1,3 +1,7 @@
+'use client'
+
+import { useTransition } from 'react'
+import { useRouter } from 'next/navigation'
import {
Sheet,
SheetContent,
@@ -9,17 +13,28 @@ import { Button } from '@/components/ui/button'
import { ChevronLeft, Menu } from 'lucide-react'
import { cn } from '@/lib/utils'
import { History as HistoryIcon } from 'lucide-react'
-import { HistoryList } from './history-list'
import { Suspense } from 'react'
import { HistorySkeleton } from './history-skelton'
type HistoryProps = {
location: 'sidebar' | 'header'
+ children?: React.ReactNode
}
-export function History({ location }: HistoryProps) {
+export function History({ location, children }: HistoryProps) {
+ const router = useRouter()
+ const [isPending, startTransition] = useTransition()
+
+ const onOpenChange = (open: boolean) => {
+ if (open) {
+ startTransition(() => {
+ router.refresh()
+ })
+ }
+ }
+
return (
- <Sheet>
+ <Sheet onOpenChange={onOpenChange}>
<SheetTrigger asChild>
<Button
variant="ghost"
@@ -39,9 +54,7 @@ export function History({ location }: HistoryProps) {
</SheetTitle>
</SheetHeader>
<div className="my-2 h-full pb-12 md:pb-10">
- <Suspense fallback={<HistorySkeleton />}>
- <HistoryList userId="anonymous" />
- </Suspense>
+ <Suspense fallback={<HistorySkeleton />}>{children}</Suspense>
</div>
</SheetContent>
</Sheet>
diff --git a/components/sidebar.tsx b/components/sidebar.tsx
index 8843f0dc..b70c76c7 100644
--- a/components/sidebar.tsx
+++ b/components/sidebar.tsx
@@ -1,9 +1,9 @@
-import { History } from './history'
+import HistoryContainer from './history-container'
export async function Sidebar() {
return (
<div className="h-screen p-2 fixed top-0 right-0 flex-col justify-center pb-24 hidden sm:flex">
- <History location="sidebar" />
+ <HistoryContainer location="sidebar" />
</div>
)
}