/** * EraseDataDialog — confirmation modal for the GDPR Art. 17 erasure flow. * * Triggered from Settings → General → "Erase All My Data". Renders three * sequential states: * 1. Warning + phrase input ("type the exact phrase to confirm") * 2. In-flight (calling adapter.eraseData) * 3. Success (shows receipt + "quit and relaunch" instruction) * * The intentional friction (typing the full phrase, not just clicking) is * the user-facing half of the route's confirmation gate. The other half * (server-side phrase + header validation) lives in * packages/server/src/local/routes/data-erase.ts. Both must match — if a * future i18n attempt translates the phrase here, the server stays in * English and a user typing the translated version gets a 400. Don't * translate this gate. * * Side effect on success: nothing visible changes immediately — the wipe * runs at next startup. The dialog presents the receipt + "quit Waggle * and relaunch" copy so the user knows they aren't done yet. */ import { useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { AlertTriangle, X, Trash2, CheckCircle2, Loader2 } from 'lucide-react'; import { adapter } from '@/lib/adapter'; import { DATE_LOCALE } from '@/lib/date-locale'; import { useFocusTrap } from '@/hooks/useFocusTrap'; interface EraseDataDialogProps { open: boolean; onClose: () => void; } const REQUIRED_PHRASE = 'I UNDERSTAND THIS IS PERMANENT'; interface EraseReceipt { requestedAt: string; markerPath: string; dataDirSnapshot: { fileCount: number; totalBytes: number }; instruction: string; } function formatBytes(n: number): string { if (n < 1024) return `${n} B`; if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`; if (n < 1024 * 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`; return `${(n / 1024 / 1024 / 1024).toFixed(2)} GB`; } export default function EraseDataDialog({ open, onClose }: EraseDataDialogProps) { const [phrase, setPhrase] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const [receipt, setReceipt] = useState(null); const phraseMatches = phrase === REQUIRED_PHRASE; const handleErase = async () => { if (!phraseMatches || submitting) return; setSubmitting(true); setError(null); try { const r = await adapter.eraseData(phrase); setReceipt(r); } catch (e) { setError(e instanceof Error ? e.message : 'Erase failed'); } finally { setSubmitting(false); } }; const handleClose = () => { // Don't reset receipt — if the user closes after a successful erase // and reopens, they'll just trigger another (idempotent) request. // Resetting phrase + error keeps the next open clean. setPhrase(''); setError(null); setReceipt(null); onClose(); }; // A11y (WCAG 2.1.1/2.4.3): Escape closes, Tab is trapped within the dialog, // focus moves in on open and restores on close — the same shared hook the // other modal overlays use. Passing `undefined` while submitting preserves // the prior "don't close mid-erase" guard (Escape is inert during the wipe). const dialogRef = useFocusTrap(open, submitting ? undefined : handleClose); return ( {open && (