import { Trash } from 'lucide-react'; import { Description, Dialog, DialogBackdrop, DialogPanel, DialogTitle, Transition, TransitionChild, } from '@headlessui/react'; import { Fragment, useState } from 'react'; import { toast } from 'sonner'; import { Chat } from '@/app/library/page'; const DeleteChat = ({ chatId, chats, setChats, }: { chatId: string; chats: Chat[]; setChats: (chats: Chat[]) => void; }) => { const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false); const [loading, setLoading] = useState(false); const handleDelete = async () => { setLoading(true); try { const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/chats/${chatId}`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', }, }, ); if (res.status != 200) { throw new Error('Failed to delete chat'); } const newChats = chats.filter((chat) => chat.id !== chatId); setChats(newChats); } catch (err: any) { toast.error(err.message); } finally { setConfirmationDialogOpen(false); setLoading(false); } }; return ( <> { setConfirmationDialogOpen(true); }} className="bg-transparent text-red-400 hover:scale-105 transition duration-200" > { if (!loading) { setConfirmationDialogOpen(false); } }} > Delete Confirmation Are you sure you want to delete this chat? { if (!loading) { setConfirmationDialogOpen(false); } }} className="text-black/50 dark:text-white/50 text-sm hover:text-black/70 hover:dark:text-white/70 transition duration-200" > Cancel Delete > ); }; export default DeleteChat;