'use client' import { useState, useEffect } from 'react' import { createPortal } from 'react-dom' import { motion, AnimatePresence } from 'framer-motion' import { toast, getAuthErrorMessage, AlertTriangleIcon, XIcon } from '@ciphera-net/ui' import { deleteSite, permanentDeleteSite } from '@/lib/api/sites' interface DeleteSiteModalProps { open: boolean onClose: () => void onDeleted: () => void siteName: string siteDomain: string siteId: string permanentOnly?: boolean } export default function DeleteSiteModal({ open, onClose, onDeleted, siteName, siteDomain, siteId, permanentOnly }: DeleteSiteModalProps) { const [deleteConfirm, setDeleteConfirm] = useState('') const [isDeleting, setIsDeleting] = useState(false) const [showPermanent, setShowPermanent] = useState(!!permanentOnly) const [permanentConfirm, setPermanentConfirm] = useState('') const [isPermanentDeleting, setIsPermanentDeleting] = useState(false) useEffect(() => { if (open && permanentOnly) { setShowPermanent(true) } }, [open, permanentOnly]) const handleClose = () => { setDeleteConfirm('') setShowPermanent(false) setPermanentConfirm('') setIsDeleting(false) setIsPermanentDeleting(false) onClose() } const handleSoftDelete = async () => { if (deleteConfirm !== 'DELETE') return setIsDeleting(true) try { await deleteSite(siteId) toast.success('Site scheduled for deletion. You have 7 days to restore it.') handleClose() onDeleted() } catch (error: unknown) { toast.error(getAuthErrorMessage(error) || 'Failed to delete site') setIsDeleting(false) } } const handlePermanentDelete = async () => { if (permanentConfirm !== siteDomain) return setIsPermanentDeleting(true) try { await permanentDeleteSite(siteId) toast.success('Site permanently deleted') handleClose() onDeleted() } catch (error: unknown) { toast.error(getAuthErrorMessage(error) || 'Failed to permanently delete site') setIsPermanentDeleting(false) } } if (typeof document === 'undefined') return null return createPortal( {open && (

Delete {siteName || 'Site'}?

{!showPermanent ? ( <>

This site will be scheduled for deletion with a 7-day grace period. You can restore it at any time during this period.

All events and analytics data
Report schedules and goals
setDeleteConfirm(e.target.value)} autoComplete="off" className="w-full px-3 py-2 text-sm border border-neutral-300 dark:border-neutral-700 rounded-lg bg-white dark:bg-neutral-800 text-white placeholder-neutral-400 focus:outline-none focus:ring-2 focus:ring-red-500 dark:focus:ring-red-400" placeholder="DELETE" />
) : ( <>

This action is irreversible. The site and all its data will be permanently deleted immediately.

All analytics data will be permanently lost
This cannot be undone
setPermanentConfirm(e.target.value)} autoComplete="off" className="w-full px-3 py-2 text-sm border border-neutral-300 dark:border-neutral-700 rounded-lg bg-white dark:bg-neutral-800 text-white placeholder-neutral-400 focus:outline-none focus:ring-2 focus:ring-red-500 dark:focus:ring-red-400" placeholder={siteDomain} />
)}
)}
, document.body ) }