"use client"; // components/DeleteConfirmModal.tsx // Generic delete confirmation modal. // Accepts any PocketBase record and calls DELETE on confirm. // On success: invalidates list cache so the table refreshes. import { useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { pbDelete } from "@/lib/api"; interface Props { /** "ysterbaard_directors" | "ysterbaard_companies" */ collection: "ysterbaard_directors" | "ysterbaard_companies"; /** The record to delete — used only for display (id + name fields) */ record: { id: string; surname?: string; first_names?: string; enterprise_name?: string; [key: string]: unknown }; open: boolean; onClose: () => void; } export default function DeleteConfirmModal({ collection, record, open, onClose }: Props) { const qc = useQueryClient(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); if (!open) return null; const isDirector = collection === "ysterbaard_directors"; const displayName = isDirector ? `${record.surname ?? ""}, ${record.first_names ?? ""}`.trim() : (record.enterprise_name as string) ?? record.id; const handleConfirm = async () => { setError(null); setLoading(true); try { await pbDelete({ collection, id: record.id }); // Match the actual query keys used by the list pages // ("companies" / "directors"), not just "pb-list". await qc.invalidateQueries({ queryKey: ["pb-list", collection] }); await qc.invalidateQueries({ queryKey: ["companies"] }); await qc.invalidateQueries({ queryKey: ["directors"] }); await qc.invalidateQueries({ queryKey: ["company", record.id] }); await qc.invalidateQueries({ queryKey: ["director", record.id] }); onClose(); } catch (err) { setError(err instanceof Error ? err.message : "Delete failed. Please try again."); } finally { setLoading(false); } }; return (
{ if (e.target === e.currentTarget) onClose(); }} className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-4" role="dialog" aria-modal="true" aria-label={`Delete ${isDirector ? "director" : "company"}`} >
{/* Header */}

Delete {isDirector ? "director" : "company"}?

This will permanently delete {displayName} from PocketBase. This action cannot be undone.

{/* Body */}
{error && (
{error}
)}
); }