Skip to content

An AI-powered search engine with a generative UI

refactor: improve ChatHistorySection in sidebar

Yoshiki Miura3167d3bfdad0
diff --git a/components/app-sidebar.tsx b/components/app-sidebar.tsx
index 01085a9e..fa0c985a 100644
--- a/components/app-sidebar.tsx
+++ b/components/app-sidebar.tsx
@@ -5,14 +5,14 @@ import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
- SidebarMenuSkeleton,
SidebarTrigger
} from '@/components/ui/sidebar'
-import { getChats } from '@/lib/actions/chat'
import { cn } from '@/lib/utils'
import { Plus } from 'lucide-react'
import Link from 'next/link'
import { Suspense } from 'react'
+import { ChatHistorySection } from './sidebar/chat-history-section'
+import { ChatHistorySkeleton } from './sidebar/chat-history-skeleton'
import { IconLogo } from './ui/icons'
export default function AppSidebar() {
@@ -37,7 +37,7 @@ export default function AppSidebar() {
</SidebarMenuItem>
</SidebarMenu>
<div className="mt-6 flex-1 overflow-y-auto">
- <Suspense fallback={<NavChatsSkeleton />}>
+ <Suspense fallback={<ChatHistorySkeleton />}>
<ChatHistorySection />
</Suspense>
</div>
@@ -45,35 +45,3 @@ export default function AppSidebar() {
</Sidebar>
)
}
-
-function NavChatsSkeleton() {
- return (
- <SidebarMenu>
- {Array.from({ length: 5 }).map((_, idx) => (
- <SidebarMenuItem key={idx}>
- <SidebarMenuSkeleton showIcon />
- </SidebarMenuItem>
- ))}
- </SidebarMenu>
- )
-}
-
-async function ChatHistorySection() {
- const chats = await getChats()
- return (
- <SidebarMenu>
- {chats.map(chat => (
- <SidebarMenuItem key={chat.id}>
- <SidebarMenuButton asChild>
- <Link
- href={`/chat?chatId=${chat.id}`}
- className="flex items-center gap-2 truncate"
- >
- <span>{chat.title}</span>
- </Link>
- </SidebarMenuButton>
- </SidebarMenuItem>
- ))}
- </SidebarMenu>
- )
-}
diff --git a/components/sidebar/chat-history-section.tsx b/components/sidebar/chat-history-section.tsx
new file mode 100644
index 00000000..308ff299
--- /dev/null
+++ b/components/sidebar/chat-history-section.tsx
@@ -0,0 +1,43 @@
+import { ClearHistory } from '@/components/clear-history'
+import { SidebarGroupLabel, SidebarMenu } 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'
+
+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) {
+ return null
+ }
+
+ const chats = await loadChats('anonymous')
+
+ return (
+ <div className="flex flex-col flex-1 h-full">
+ <SidebarGroupLabel className="px-2 text-xs font-normal text-muted-foreground mb-2">
+ History
+ </SidebarGroupLabel>
+ <div className="flex-1 overflow-y-auto mb-2">
+ {!chats?.length ? (
+ <div className="px-2 text-foreground/30 text-sm text-center py-4">
+ No search history
+ </div>
+ ) : (
+ <SidebarMenu>
+ {chats.map(
+ (chat: Chat) => chat && <ChatMenuItem key={chat.id} chat={chat} />
+ )}
+ </SidebarMenu>
+ )}
+ </div>
+ <div className="p-2 mt-auto">
+ <ClearHistory empty={!chats?.length} />
+ </div>
+ </div>
+ )
+}
diff --git a/components/sidebar/chat-history-skeleton.tsx b/components/sidebar/chat-history-skeleton.tsx
new file mode 100644
index 00000000..6768cb12
--- /dev/null
+++ b/components/sidebar/chat-history-skeleton.tsx
@@ -0,0 +1,17 @@
+import {
+ SidebarMenu,
+ SidebarMenuItem,
+ SidebarMenuSkeleton
+} from '@/components/ui/sidebar'
+
+export function ChatHistorySkeleton() {
+ return (
+ <SidebarMenu>
+ {Array.from({ length: 5 }).map((_, idx) => (
+ <SidebarMenuItem key={idx}>
+ <SidebarMenuSkeleton showIcon={false} />
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+}
diff --git a/components/sidebar/chat-menu-item.tsx b/components/sidebar/chat-menu-item.tsx
new file mode 100644
index 00000000..50ce51c2
--- /dev/null
+++ b/components/sidebar/chat-menu-item.tsx
@@ -0,0 +1,33 @@
+'use client'
+
+import { SidebarMenuButton, SidebarMenuItem } from '@/components/ui/sidebar'
+import { Chat } from '@/lib/types'
+import { cn } from '@/lib/utils'
+import Link from 'next/link'
+import { usePathname } from 'next/navigation'
+
+interface ChatMenuItemProps {
+ chat: Chat
+}
+
+export function ChatMenuItem({ chat }: ChatMenuItemProps) {
+ const pathname = usePathname()
+ const isActive = pathname === chat.path
+
+ return (
+ <SidebarMenuItem>
+ <SidebarMenuButton asChild data-active={isActive}>
+ <Link
+ href={chat.path}
+ className={cn(
+ 'flex items-center gap-2 truncate text-sm'
+ // Additional styles for active state can be handled by
+ // the parent component's data-active attribute or sidebar CSS variables
+ )}
+ >
+ <span>{chat.title}</span>
+ </Link>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ )
+}