Skip to content

An AI-powered search engine with a generative UI

Add Toaster component and clear history functionality

Yoshiki Miurafd353107be90
diff --git a/app/layout.tsx b/app/layout.tsx
index 856f5ce4..d1b3caf1 100644
--- a/app/layout.tsx
+++ b/app/layout.tsx
@@ -6,6 +6,7 @@ import { ThemeProvider } from '@/components/theme-provider'
import Header from '@/components/header'
import Footer from '@/components/footer'
import { Sidebar } from '@/components/sidebar'
+import { Toaster } from '@/components/ui/sonner'
const fontSans = FontSans({
subsets: ['latin'],
@@ -57,6 +58,7 @@ export default function RootLayout({
{children}
<Sidebar />
<Footer />
+ <Toaster />
</ThemeProvider>
</body>
</html>
diff --git a/bun.lockb b/bun.lockb
index af3685a1..0e7c92c1 100755
Binary files a/bun.lockb and b/bun.lockb differ
diff --git a/components/clear-history-button.tsx b/components/clear-history-button.tsx
deleted file mode 100644
index c94865a0..00000000
--- a/components/clear-history-button.tsx
+++ /dev/null
@@ -1,37 +0,0 @@
-import {
- AlertDialog,
- AlertDialogAction,
- AlertDialogCancel,
- AlertDialogContent,
- AlertDialogDescription,
- AlertDialogFooter,
- AlertDialogHeader,
- AlertDialogTitle,
- AlertDialogTrigger
-} from '@/components/ui/alert-dialog'
-import { Button } from '@/components/ui/button'
-
-export function ClearHistoryButton() {
- return (
- <AlertDialog>
- <AlertDialogTrigger asChild>
- <Button variant="outline" className="w-full">
- Clear History
- </Button>
- </AlertDialogTrigger>
- <AlertDialogContent>
- <AlertDialogHeader>
- <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
- <AlertDialogDescription>
- This action cannot be undone. This will permanently delete your
- history.
- </AlertDialogDescription>
- </AlertDialogHeader>
- <AlertDialogFooter>
- <AlertDialogCancel>Cancel</AlertDialogCancel>
- <AlertDialogAction>Continue</AlertDialogAction>
- </AlertDialogFooter>
- </AlertDialogContent>
- </AlertDialog>
- )
-}
diff --git a/components/clear-history.tsx b/components/clear-history.tsx
new file mode 100644
index 00000000..4ed9d463
--- /dev/null
+++ b/components/clear-history.tsx
@@ -0,0 +1,65 @@
+'use client'
+
+import { useState, useTransition } from 'react'
+import {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+ AlertDialogTrigger
+} from '@/components/ui/alert-dialog'
+import { Button } from '@/components/ui/button'
+import { clearChats } from '@/lib/actions/chat'
+import { toast } from 'sonner'
+import { Spinner } from './ui/spinner'
+
+type ClearHistoryProps = {
+ empty: boolean
+}
+
+export function ClearHistory({ empty }: ClearHistoryProps) {
+ const [open, setOpen] = useState(false)
+ const [isPending, startTransition] = useTransition()
+ return (
+ <AlertDialog open={open} onOpenChange={setOpen}>
+ <AlertDialogTrigger asChild>
+ <Button variant="outline" className="w-full" disabled={empty}>
+ Clear History
+ </Button>
+ </AlertDialogTrigger>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
+ <AlertDialogDescription>
+ This action cannot be undone. This will permanently delete your
+ history.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel disabled={isPending}>Cancel</AlertDialogCancel>
+ <AlertDialogAction
+ disabled={isPending}
+ onClick={event => {
+ event.preventDefault()
+ startTransition(async () => {
+ const result = await clearChats()
+ if (result?.error) {
+ toast.error(result.error)
+ } else {
+ toast.success('History cleared')
+ }
+ setOpen(false)
+ })
+ }}
+ >
+ {isPending ? <Spinner /> : 'Delete'}
+ </AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ )
+}
diff --git a/components/history.tsx b/components/history.tsx
index c0615284..f8654772 100644
--- a/components/history.tsx
+++ b/components/history.tsx
@@ -12,7 +12,7 @@ import { cn } from '@/lib/utils'
import HistoryItem from './history-item'
import { Chat } from '@/lib/types'
import { History as HistoryIcon } from 'lucide-react'
-import { ClearHistoryButton } from './clear-history-button'
+import { ClearHistory } from './clear-history'
type HistoryProps = {
location: 'sidebar' | 'header'
@@ -52,7 +52,7 @@ export function History({ location, chats }: HistoryProps) {
)}
</div>
<SheetFooter>
- <ClearHistoryButton />
+ <ClearHistory empty={!chats?.length} />
</SheetFooter>
</SheetContent>
</Sheet>
diff --git a/components/ui/sonner.tsx b/components/ui/sonner.tsx
new file mode 100644
index 00000000..452f4d9f
--- /dev/null
+++ b/components/ui/sonner.tsx
@@ -0,0 +1,31 @@
+"use client"
+
+import { useTheme } from "next-themes"
+import { Toaster as Sonner } from "sonner"
+
+type ToasterProps = React.ComponentProps<typeof Sonner>
+
+const Toaster = ({ ...props }: ToasterProps) => {
+ const { theme = "system" } = useTheme()
+
+ return (
+ <Sonner
+ theme={theme as ToasterProps["theme"]}
+ className="toaster group"
+ toastOptions={{
+ classNames: {
+ toast:
+ "group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg",
+ description: "group-[.toast]:text-muted-foreground",
+ actionButton:
+ "group-[.toast]:bg-primary group-[.toast]:text-primary-foreground",
+ cancelButton:
+ "group-[.toast]:bg-muted group-[.toast]:text-muted-foreground",
+ },
+ }}
+ {...props}
+ />
+ )
+}
+
+export { Toaster }
diff --git a/lib/actions/chat.ts b/lib/actions/chat.ts
index eef7eb13..ecfe2d9c 100644
--- a/lib/actions/chat.ts
+++ b/lib/actions/chat.ts
@@ -1,5 +1,7 @@
'use server'
+import { revalidatePath } from 'next/cache'
+import { redirect } from 'next/navigation'
import { type Chat } from '@/lib/types'
import { Redis } from '@upstash/redis'
@@ -41,6 +43,26 @@ export async function getChat(id: string, userId: string = 'anonymous') {
return chat
}
+export async function clearChats(
+ userId: string = 'anonymous'
+): Promise<{ error?: string }> {
+ const chats: string[] = await redis.zrange(`user:chat:${userId}`, 0, -1)
+ if (!chats.length) {
+ return { error: 'No chats to clear' }
+ }
+ const pipeline = redis.pipeline()
+
+ for (const chat of chats) {
+ pipeline.del(chat)
+ pipeline.zrem(`user:chat:${userId}`, chat)
+ }
+
+ await pipeline.exec()
+
+ revalidatePath('/')
+ redirect('/')
+}
+
export async function saveChat(chat: Chat, userId: string = 'anonymous') {
const pipeline = redis.pipeline()
pipeline.hmset(`chat:${chat.id}`, chat)
diff --git a/package.json b/package.json
index 6bb7e3d9..3ba8e37a 100644
--- a/package.json
+++ b/package.json
@@ -41,6 +41,7 @@
"react-textarea-autosize": "^8.5.3",
"rehype-external-links": "^3.0.0",
"remark-gfm": "^4.0.0",
+ "sonner": "^1.4.41",
"tailwind-merge": "^2.2.2",
"tailwindcss-animate": "^1.0.7"
},