"use client"; import { MessageSquareIcon, PanelLeftIcon, PenSquareIcon, TrashIcon, } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { toast } from "sonner"; import { useSWRConfig } from "swr"; import { unstable_serialize } from "swr/infinite"; import { getChatHistoryPaginationKey, SidebarHistory, } from "@/components/sidebar-history"; import { SidebarUserNav } from "@/components/sidebar-user-nav"; import { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupContent, SidebarHeader, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarRail, SidebarTrigger, useSidebar, } from "@/components/ui/sidebar"; import type { AuthUser } from "@/lib/auth"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "./ui/alert-dialog"; import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip"; export function AppSidebar({ user }: { user: AuthUser | undefined }) { const router = useRouter(); const { setOpenMobile, toggleSidebar } = useSidebar(); const { mutate } = useSWRConfig(); const [showDeleteAllDialog, setShowDeleteAllDialog] = useState(false); const handleDeleteAll = () => { const deletePromise = fetch( `${process.env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/history`, { method: "DELETE", } ); toast.promise(deletePromise, { loading: "Deleting all chats...", success: () => { mutate(unstable_serialize(getChatHistoryPaginationKey)); setShowDeleteAllDialog(false); router.replace("/"); router.refresh(); return "All chats deleted successfully"; }, error: "Failed to delete all chats", }); }; return ( <>
setOpenMobile(false)}> toggleSidebar()} > Open sidebar
{ setOpenMobile(false); router.push("/"); router.refresh(); }} tooltip="New Chat" > New chat {user && ( setShowDeleteAllDialog(true)} tooltip="Delete All Chats" > Delete all chats )} {user && }
Delete all chats? This action cannot be undone. This will permanently delete all your chats and remove them from our servers. Cancel Delete All ); }