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'; import { useRouter } from 'next/navigation'; const DeleteChat = ({ chatId, chats, setChats, redirect = false, }: { chatId: string; chats: Chat[]; setChats: (chats: Chat[]) => void; redirect?: boolean; }) => { const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false); const [loading, setLoading] = useState(false); const router = useRouter(); 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); if (redirect) { router.push('/'); } } catch (err: any) { toast.error(err.message); } finally { setConfirmationDialogOpen(false); setLoading(false); } }; return ( <> { if (!loading) { setConfirmationDialogOpen(false); } }} >
Delete Confirmation Are you sure you want to delete this chat?
); }; export default DeleteChat;