"use client" import { useState } from "react" import { useRouter, usePathname } from "next/navigation" interface DeletePhotoButtonProps { photoId: string onDelete?: () => void variant?: "icon" | "button" } export default function DeletePhotoButton({ photoId, onDelete, variant = "icon" }: DeletePhotoButtonProps) { const router = useRouter() const pathname = usePathname() const [loading, setLoading] = useState(false) const [error, setError] = useState("") const handleDelete = async () => { if (!confirm("Are you sure you want to delete this photo? This action cannot be undone.")) { return } setLoading(true) setError("") try { const response = await fetch(`/api/photos/${photoId}`, { method: "DELETE", }) const data = await response.json() if (!response.ok) { setError(data.error || "Failed to delete photo") return } // Call optional callback if (onDelete) { onDelete() } else { // If we're on a photo detail page, redirect to photos list if (pathname?.startsWith("/photos/") && pathname !== "/photos") { router.push("/photos") } else { // Otherwise just refresh the current page router.refresh() } } } catch (err) { setError("An error occurred. Please try again.") console.error("Delete error:", err) } finally { setLoading(false) } } if (variant === "button") { return (
{error}
)}{error}
)}