'use client' import { useAuth } from '@thedaviddias/auth' import { Badge } from '@thedaviddias/design-system/badge' import { Button } from '@thedaviddias/design-system/button' import { logger } from '@thedaviddias/logging' import { AlertCircle, AlertTriangle, Loader2, Trash2 } from 'lucide-react' import { useRouter } from 'next/navigation' import { useId, useState } from 'react' import { toast } from 'sonner' import { Card } from '@/components/ui/card' import { analytics } from '@/lib/analytics' import { fetchWithCSRF } from '@/lib/csrf-client' interface User { id: string email?: string user_metadata?: { github_username?: string user_name?: string } } interface DangerZoneProps { user: User | null } /** * Renders the account deletion danger zone with confirmation flow */ export function DangerZone({ user }: DangerZoneProps) { const { signOut } = useAuth() const router = useRouter() const [isDeleting, setIsDeleting] = useState(false) const [showDangerZone, setShowDangerZone] = useState(false) const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const [deleteConfirmText, setDeleteConfirmText] = useState('') const [error, setError] = useState('') const uniqueId = useId() /** * Handles account deletion after user types DELETE to confirm */ const handleDeleteAccount = async () => { if (deleteConfirmText !== 'DELETE') { setError('Please type DELETE to confirm account deletion') return } setIsDeleting(true) setError('') try { const response = await fetchWithCSRF('/api/user/delete-account', { method: 'DELETE', headers: { 'Content-Type': 'application/json' } }) if (!response.ok) { const data = await response.json() throw new Error(data.error || 'Failed to delete account') } analytics.accountDeleteSuccess('settings-account') toast.success('Account deleted successfully') await signOut() router.push('/') } catch (error) { logger.error(error instanceof Error ? error : new Error(String(error)), { data: error, tags: { type: 'component', component: 'danger-zone' } }) const message = error instanceof Error ? error.message : 'Failed to delete account. Please try again.' setError(message) setIsDeleting(false) } } return ( <>
{showDangerZone && (

Delete Account

Permanently delete your account and all associated data. This action cannot be undone.

• All your favorites will be deleted
• Your submission history will be removed
• This action is immediate and irreversible
)}
{showDeleteConfirm && (
)} ) }