Skip to content

An AI-powered search engine with a generative UI

feat: add clear history dropdown to sidebar

Yoshiki Miura8724824a1e64
diff --git a/components/sidebar/chat-history-section.tsx b/components/sidebar/chat-history-section.tsx
index 5d53c94f..7ab6c451 100644
--- a/components/sidebar/chat-history-section.tsx
+++ b/components/sidebar/chat-history-section.tsx
@@ -1,10 +1,13 @@
-import { ClearHistory } from '@/components/clear-history'
-import { SidebarGroupLabel, SidebarMenu } from '@/components/ui/sidebar'
+import {
+ SidebarGroup,
+ SidebarGroupLabel,
+ SidebarMenu
+} from '@/components/ui/sidebar'
import { getChats } from '@/lib/actions/chat'
import { Chat } from '@/lib/types'
-import { History } from 'lucide-react'
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)
@@ -16,14 +19,17 @@ export async function ChatHistorySection() {
return null
}
+ // Replace with your own user ID
const chats = await loadChats('anonymous')
return (
<div className="flex flex-col flex-1 h-full">
- <SidebarGroupLabel className="px-2 mb-2 flex items-center gap-1.5">
- <History size={14} />
- <span>History</span>
- </SidebarGroupLabel>
+ <SidebarGroup>
+ <div className="flex items-center justify-between w-full">
+ <SidebarGroupLabel className="p-0">History</SidebarGroupLabel>
+ <ClearHistoryAction empty={!chats?.length} />
+ </div>
+ </SidebarGroup>
<div className="flex-1 overflow-y-auto mb-2">
{!chats?.length ? (
<div className="px-2 text-foreground/30 text-sm text-center py-4">
@@ -37,9 +43,6 @@ export async function ChatHistorySection() {
</SidebarMenu>
)}
</div>
- <div className="p-2 mt-auto">
- <ClearHistory empty={!chats?.length} />
- </div>
</div>
)
}
diff --git a/components/sidebar/clear-history-action.tsx b/components/sidebar/clear-history-action.tsx
new file mode 100644
index 00000000..4911bd95
--- /dev/null
+++ b/components/sidebar/clear-history-action.tsx
@@ -0,0 +1,85 @@
+'use client'
+
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+ AlertDialogTrigger
+} from '@/components/ui/alert-dialog'
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger
+} from '@/components/ui/dropdown-menu'
+import { SidebarGroupAction } from '@/components/ui/sidebar'
+import { Spinner } from '@/components/ui/spinner'
+import { clearChats } from '@/lib/actions/chat'
+import { MoreHorizontal, Trash2 } from 'lucide-react'
+import { useRouter } from 'next/navigation'
+import { useState, useTransition } from 'react'
+import { toast } from 'sonner'
+
+interface ClearHistoryActionProps {
+ empty: boolean
+}
+
+export function ClearHistoryAction({ empty }: ClearHistoryActionProps) {
+ const [isPending, start] = useTransition()
+ const [open, setOpen] = useState(false)
+ const router = useRouter()
+
+ const onClear = () =>
+ start(async () => {
+ const res = await clearChats()
+ res?.error ? toast.error(res.error) : toast.success('History cleared')
+ setOpen(false)
+ router.refresh()
+ })
+
+ return (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <SidebarGroupAction disabled={empty} className="static size-7 p-1">
+ <MoreHorizontal size={16} />
+ <span className="sr-only">History Actions</span>
+ </SidebarGroupAction>
+ </DropdownMenuTrigger>
+
+ <DropdownMenuContent align="end">
+ <AlertDialog open={open} onOpenChange={setOpen}>
+ <AlertDialogTrigger asChild>
+ <DropdownMenuItem
+ disabled={empty || isPending}
+ className="gap-2 text-destructive focus:text-destructive"
+ onSelect={event => event.preventDefault()} // Prevent closing dropdown
+ >
+ <Trash2 size={14} /> Clear History
+ </DropdownMenuItem>
+ </AlertDialogTrigger>
+
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
+ <AlertDialogDescription>
+ This action cannot be undone. It will permanently delete your
+ history.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel disabled={isPending}>Cancel</AlertDialogCancel>
+ <AlertDialogAction disabled={isPending} onClick={onClear}>
+ {isPending ? <Spinner /> : 'Clear'}
+ </AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )
+}