import { useState } from "react"; import { ApiClientError, apiClient } from "../api"; interface DeleteNoteModalProps { isOpen: boolean; deckId: string; noteId: string | null; onClose: () => void; onNoteDeleted: () => void; } export function DeleteNoteModal({ isOpen, deckId, noteId, onClose, onNoteDeleted, }: DeleteNoteModalProps) { const [error, setError] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const handleClose = () => { setError(null); onClose(); }; const handleDelete = async () => { if (!noteId) return; setError(null); setIsDeleting(true); try { const authHeader = apiClient.getAuthHeader(); if (!authHeader) { throw new ApiClientError("Not authenticated", 401); } const res = await fetch(`/api/decks/${deckId}/notes/${noteId}`, { method: "DELETE", headers: authHeader, }); if (!res.ok) { const errorBody = await res.json().catch(() => ({})); throw new ApiClientError( (errorBody as { error?: string }).error || `Request failed with status ${res.status}`, res.status, ); } onNoteDeleted(); onClose(); } catch (err) { if (err instanceof ApiClientError) { setError(err.message); } else { setError("Failed to delete note. Please try again."); } } finally { setIsDeleting(false); } }; if (!isOpen || !noteId) { return null; } return (
{ if (e.target === e.currentTarget) { handleClose(); } }} onKeyDown={(e) => { if (e.key === "Escape") { handleClose(); } }} >

Delete Note

{error && (
{error}
)}

Are you sure you want to delete this note?

This will delete all cards generated from this note. This action cannot be undone.

); }