'use client' import { useState, useRef } from 'react' import { Download, Upload, History, FileText, Code, FolderOpen } from 'lucide-react' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { toast } from 'sonner' import { BackupList } from '@/components/backup-list' import { PreferencesPanel } from '@/components/preferences-panel' function parseMarkdownToNote(content: string, filename: string) { const lines = content.split('\n') let title = filename.replace(/\.md$/, '') let body = content const firstHeadingMatch = content.match(/^#\s+(.+)$/m) if (firstHeadingMatch) { title = firstHeadingMatch[1].trim() const headingIndex = content.indexOf(firstHeadingMatch[0]) body = content.slice(headingIndex + firstHeadingMatch[0].length).trim() } return { title, content: body, type: 'note', } } export default function SettingsPage() { const [importing, setImporting] = useState(false) const [exporting, setExporting] = useState(null) const fileInputRef = useRef(null) const handleExport = async (format: 'json' | 'markdown' | 'html') => { setExporting(format) try { const response = await fetch(`/api/export-import?format=${format}`) if (!response.ok) { throw new Error('Error al exportar') } const data = await response.json() let blob: Blob let filename: string const date = new Date().toISOString().split('T')[0] if (format === 'json') { blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }) filename = `recall-backup-${date}.json` } else if (format === 'markdown') { if (data.files) { // Multiple files - in the future could be a zip blob = new Blob([data.files.map((f: { content: string }) => f.content).join('\n\n---\n\n')], { type: 'text/markdown' }) } else { blob = new Blob([data.content], { type: 'text/markdown' }) } filename = `recall-export-${date}.md` } else { if (data.files) { blob = new Blob([data.files.map((f: { content: string }) => f.content).join('\n\n')], { type: 'text/html' }) } else { blob = new Blob([data.content], { type: 'text/html' }) } filename = `recall-export-${date}.html` } const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = filename document.body.appendChild(a) a.click() document.body.removeChild(a) URL.revokeObjectURL(url) toast.success(`Notas exportadas en formato ${format.toUpperCase()}`) } catch { toast.error('Error al exportar las notas') } finally { setExporting(null) } } const handleImport = async () => { const file = fileInputRef.current?.files?.[0] if (!file) { toast.error('Selecciona un archivo JSON o MD') return } setImporting(true) try { const text = await file.text() const isMarkdown = file.name.endsWith('.md') let payload: object[] let endpoint = '/api/export-import' if (isMarkdown) { const note = parseMarkdownToNote(text, file.name) payload = [{ markdown: text, filename: file.name }] endpoint = '/api/import-markdown' } else { payload = JSON.parse(text) } const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }) const result = await response.json() if (!response.ok) { throw new Error(result.error || 'Error al importar') } const msg = result.warnings ? `${result.count} nota${result.count !== 1 ? 's' : ''} importada${result.count !== 1 ? 's' : ''} correctamente (con advertencias)` : `${result.count} nota${result.count !== 1 ? 's' : ''} importada${result.count !== 1 ? 's' : ''} correctamente` toast.success(msg) if (fileInputRef.current) { fileInputRef.current.value = '' } } catch (err) { toast.error(err instanceof Error ? err.message : 'Error al importar las notas') } finally { setImporting(false) } } return (

Configuración

{/* Preferences Section */} {/* Backups Section */} Backups y Restauración Los backups automáticos se guardan localmente. También puedes crear un backup manual antes de operaciones riesgosas. {/* Export Section */} Exportar Notas Descarga tus notas en diferentes formatos. Elige el que mejor se adapte a tus necesidades.
{/* Import Section */} Importar Notas Importa notas desde archivos JSON o Markdown. Soporta frontmatter, tags, y enlaces wiki.
) }