import { useSetAtom } from "jotai"; import { useState } from "react"; import { syncActionAtom } from "../atoms"; import { localNoteTypeRepository } from "../db/repositories"; interface NoteType { id: string; name: string; } interface DeleteNoteTypeModalProps { isOpen: boolean; noteType: NoteType | null; onClose: () => void; onNoteTypeDeleted: () => void; } export function DeleteNoteTypeModal({ isOpen, noteType, onClose, onNoteTypeDeleted, }: DeleteNoteTypeModalProps) { const [error, setError] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const triggerSync = useSetAtom(syncActionAtom); const handleClose = () => { setError(null); onClose(); }; const handleDelete = async () => { if (!noteType) return; setError(null); setIsDeleting(true); try { if (await localNoteTypeRepository.hasNotes(noteType.id)) { setError("Cannot delete note type with existing notes."); return; } const deleted = await localNoteTypeRepository.delete(noteType.id); if (!deleted) { setError("Note type not found."); return; } onNoteTypeDeleted(); onClose(); void triggerSync().catch(() => {}); } catch { setError("Failed to delete note type. Please try again."); } finally { setIsDeleting(false); } }; if (!isOpen || !noteType) { return null; } return (
{ if (e.target === e.currentTarget) { handleClose(); } }} onKeyDown={(e) => { if (e.key === "Escape") { handleClose(); } }} >

Delete Note Type

{error && (
{error}
)}

Are you sure you want to delete{" "} {noteType.name}?

Note types with existing notes cannot be deleted. Remove all notes using this type first.

); }