'use client' import { useState } from 'react' import { Button } from '@/components/ui/button' import { Download, Upload, Trash2, Loader2, RefreshCw, Sparkles, Database } from 'lucide-react' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' import { useRouter } from 'next/navigation' export default function DataSettingsPage() { const { t } = useLanguage() const router = useRouter() const [isExporting, setIsExporting] = useState(false) const [isImporting, setIsImporting] = useState(false) const [isDeleting, setIsDeleting] = useState(false) const [isReindexing, setIsReindexing] = useState(false) const [isCleaningUp, setIsCleaningUp] = useState(false) const handleExport = async () => { setIsExporting(true) try { const response = await fetch('/api/notes/export') if (response.ok) { const blob = await response.blob() const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `memento-export-${new Date().toISOString().split('T')[0]}.json` document.body.appendChild(a) a.click() document.body.removeChild(a) window.URL.revokeObjectURL(url) toast.success(t('dataManagement.export.success')) } else { throw new Error() } } catch { toast.error(t('dataManagement.export.failed')) } finally { setIsExporting(false) } } const handleImport = async (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (!file) return setIsImporting(true) try { const formData = new FormData() formData.append('file', file) const response = await fetch('/api/notes/import', { method: 'POST', body: formData }) if (response.ok) { const result = await response.json() toast.success(t('dataManagement.import.success', { count: result.count })) router.refresh() } else { const error = await response.json() throw new Error(error.error || 'Import failed') } } catch (err: any) { toast.error(err.message || t('dataManagement.import.failed')) } finally { setIsImporting(false) event.target.value = '' } } const handleReindex = async () => { setIsReindexing(true) try { const response = await fetch('/api/notes/reindex', { method: 'POST' }) if (response.ok) { const result = await response.json() toast.success(t('dataManagement.indexing.success', { count: result.count })) } else { throw new Error() } } catch { toast.error(t('dataManagement.indexing.failed')) } finally { setIsReindexing(false) } } const handleCleanup = async () => { setIsCleaningUp(true) try { const response = await fetch('/api/notes/cleanup', { method: 'POST' }) if (response.ok) { const result = await response.json() toast.success(t('dataManagement.cleanup.success', { count: result.deletedLabels })) } else { throw new Error() } } catch { toast.error(t('dataManagement.cleanup.failed')) } finally { setIsCleaningUp(false) } } const handleDeleteAll = async () => { if (!confirm(t('dataManagement.delete.confirm'))) return setIsDeleting(true) try { const response = await fetch('/api/notes/delete-all', { method: 'POST' }) if (response.ok) { toast.success(t('dataManagement.delete.success')) router.refresh() } else { throw new Error() } } catch { toast.error(t('dataManagement.delete.failed')) } finally { setIsDeleting(false) } } return (

{t('dataManagement.toolsDescription')}

{/* Export card */}

{t('dataManagement.export.title')}

{t('dataManagement.export.description')}

{/* Import card */}

{t('dataManagement.import.title')}

{t('dataManagement.import.description')}

{/* Reindex card */}

{t('dataManagement.indexing.title')}

{t('dataManagement.indexing.description')}

{/* Cleanup card */}

{t('dataManagement.cleanup.title')}

{t('dataManagement.cleanup.description')}

{/* Danger zone */}

{t('dataManagement.dangerZone')}

{t('dataManagement.dangerZoneDescription')}

{t('dataManagement.delete.title')}

{t('dataManagement.delete.description')}

) }