diff --git a/app/api/room/delete/route.ts b/app/api/room/delete/route.ts deleted file mode 100644 index aa22e167c..000000000 --- a/app/api/room/delete/route.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { NextRequest } from "next/server"; -import supabase from "@/lib/supabase/serverClient"; - -export async function POST(req: NextRequest) { - try { - const { roomId } = await req.json(); - - if (!roomId) { - return Response.json( - { message: "Missing required parameter: roomId" }, - { status: 400 } - ); - } - - // First delete any related data - // Delete messages in this room - const { error: messagesError } = await supabase - .from("messages") - .delete() - .eq("room_id", roomId); - - if (messagesError) { - console.error("Error deleting messages:", messagesError); - // Continue with the deletion process even if this fails - } - - // Delete room_reports associations - const { error: reportError } = await supabase - .from("room_reports") - .delete() - .eq("room_id", roomId); - - if (reportError) { - console.error("Error deleting room reports:", reportError); - // Continue with the deletion process even if this fails - } - - // Finally delete the room itself - const { error } = await supabase - .from("rooms") - .delete() - .eq("id", roomId); - - if (error) { - console.error("Error deleting room:", error); - return Response.json( - { message: "Failed to delete room", error: error.message }, - { status: 500 } - ); - } - - return Response.json({ message: "Room deleted successfully" }, { status: 200 }); - } catch (error) { - console.error("Error in /api/room/delete:", error); - const message = error instanceof Error ? error.message : "Server error"; - return Response.json({ message }, { status: 500 }); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; \ No newline at end of file diff --git a/components/Sidebar/Modals/DeleteConfirmationModal.tsx b/components/Sidebar/Modals/DeleteConfirmationModal.tsx index 33abc4bc0..f4499fdc7 100644 --- a/components/Sidebar/Modals/DeleteConfirmationModal.tsx +++ b/components/Sidebar/Modals/DeleteConfirmationModal.tsx @@ -2,6 +2,7 @@ import { useState, useEffect } from "react"; import Modal from "@/components/Modal"; import type { Conversation } from "@/types/Chat"; import type { ArtistAgent } from "@/lib/supabase/getArtistAgents"; +import { useDeleteChat } from "@/hooks/useDeleteChat"; interface DeleteConfirmationModalProps { isOpen: boolean; @@ -17,92 +18,69 @@ const getChatName = (item: Conversation | ArtistAgent): string => isChatRoom(ite const getChatId = (item: Conversation | ArtistAgent): string => isChatRoom(item) ? item.id : item.agentId; const DeleteConfirmationModal = ({ isOpen, onClose, chatRoom, chatRooms, onDelete }: DeleteConfirmationModalProps) => { - const [isDeleting, setIsDeleting] = useState(false); const [error, setError] = useState(""); const [deletingProgress, setDeletingProgress] = useState<{ current: number; total: number } | null>(null); - + const { deleteChat, isDeleting } = useDeleteChat(); + // Reset state when modal opens/closes useEffect(() => { if (!isOpen) { - // Small delay to prevent visual flicker if modal is reopened quickly const timer = setTimeout(() => { - setIsDeleting(false); setError(""); setDeletingProgress(null); }, 200); return () => clearTimeout(timer); } }, [isOpen]); - + // Determine if this is bulk delete or single delete const isBulkDelete = chatRooms && chatRooms.length > 0; const chatsToDelete = isBulkDelete ? chatRooms : (chatRoom ? [chatRoom] : []); - + if (!isOpen || chatsToDelete.length === 0) return null; - + const chatCount = chatsToDelete.length; const isSingleDelete = chatCount === 1; const chatName = isSingleDelete ? getChatName(chatsToDelete[0]) : `${chatCount} chats`; - const buttonText = isDeleting - ? (deletingProgress ? `Deleting ${deletingProgress.current}/${deletingProgress.total}...` : 'Deleting...') + const buttonText = isDeleting + ? (deletingProgress ? `Deleting ${deletingProgress.current}/${deletingProgress.total}...` : 'Deleting...') : 'Delete'; - + const handleDelete = async () => { - setIsDeleting(true); setError(""); setDeletingProgress({ current: 0, total: chatCount }); - + try { const failedChats: string[] = []; - - // Delete each chat sequentially + for (let i = 0; i < chatsToDelete.length; i++) { const chat = chatsToDelete[i]; setDeletingProgress({ current: i + 1, total: chatCount }); - + try { - const roomId = getChatId(chat); - const response = await fetch('/api/room/delete', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ roomId }), - }); - - const result = await response.json(); - - if (!response.ok) { - throw new Error(result.message || 'Failed to delete chat'); - } + await deleteChat(getChatId(chat)); } catch (chatError) { console.error(`Error deleting chat ${getChatName(chat)}:`, chatError); failedChats.push(getChatName(chat)); } } - - // If some deletions failed, show error + if (failedChats.length > 0) { setError(`Failed to delete: ${failedChats.join(', ')}`); - setIsDeleting(false); setDeletingProgress(null); - // Still call onDelete to refresh the list await onDelete(); return; } - - // Call the onDelete callback to update the UI and wait for it to complete + await onDelete(); - - // Only close the modal after deletion and UI refresh are complete onClose(); } catch (error) { console.error('Error deleting chats:', error); setError(error instanceof Error ? error.message : 'Failed to delete chats. Please try again.'); - setIsDeleting(false); setDeletingProgress(null); } }; - // Don't allow closing the modal during deletion const handleModalClose = () => { if (!isDeleting) { onClose(); @@ -118,13 +96,13 @@ const DeleteConfirmationModal = ({ isOpen, onClose, chatRoom, chatRooms, onDelet

Are you sure you want to delete {isSingleDelete ? `"${chatName}"` : chatName}? This action cannot be undone.

- + {error && (
{error}
)} - +