'use client' import { useState, useEffect } from 'react' import { useTranslations } from 'next-intl' import { useRouter } from 'next/navigation' import Link from 'next/link' import { createClient } from '@/lib/supabase/client' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { RetentionNotice } from '@/components/ui/retention-notice' import { ExternalLink, Loader2 } from 'lucide-react' import { SupportLink } from '@/components/ui/support-link' interface Blocker { id: string name: string } export function AccountDangerZone() { const t = useTranslations('settings_account_danger') const router = useRouter() const [email, setEmail] = useState(null) const [blockers, setBlockers] = useState([]) const [blockersLoading, setBlockersLoading] = useState(true) const [showDialog, setShowDialog] = useState(false) const [confirmText, setConfirmText] = useState('') const [isDeleting, setIsDeleting] = useState(false) const [error, setError] = useState(null) useEffect(() => { let cancelled = false async function load() { const supabase = createClient() const { data: { user } } = await supabase.auth.getUser() if (!cancelled) setEmail(user?.email ?? null) try { const res = await fetch('/api/company?owned=true&archived=false') if (res.ok) { const body = await res.json() if (!cancelled) setBlockers(body.data ?? []) } } finally { if (!cancelled) setBlockersLoading(false) } } load() return () => { cancelled = true } }, []) async function handleDelete() { if (!email) return if (confirmText.trim().toLowerCase() !== email.toLowerCase()) return setIsDeleting(true) setError(null) try { const response = await fetch('/api/account/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ confirm_email: email }), }) if (response.status === 409) { const body = await response.json() // Precondition tripped mid-flow — refresh the list and show inline. setBlockers(body.blockers ?? []) setError(body.error || t('delete_failed_blockers')) setIsDeleting(false) setShowDialog(false) return } if (!response.ok) { const body = await response.json().catch(() => ({})) throw new Error(body.error || t('delete_failed_default')) } router.push('/login') } catch (err) { setError(err instanceof Error ? err.message : t('delete_failed_default')) setIsDeleting(false) } } const hasBlockers = blockers.length > 0 const canDelete = !hasBlockers && !blockersLoading return ( <>

{t('heading')}

{hasBlockers && (

{t('blockers_title')}

{t('blockers_description')}

    {blockers.map((b) => (
  • {b.name}
  • ))}
)}

{t('support_question')}{' '}

{error && !showDialog && (

{error}

)}
{ if (isDeleting) return setShowDialog(open) if (!open) { setConfirmText('') setError(null) } }} > {t('dialog_title')} {t('dialog_description')}
setConfirmText(e.target.value)} placeholder={email ?? ''} autoComplete="off" /> {error &&

{error}

}
) }