This commit is contained in:
2026-03-22 13:01:46 -03:00
parent af0910f428
commit 6694bce736
52 changed files with 4949 additions and 102 deletions
+150
View File
@@ -0,0 +1,150 @@
'use client'
import { useState, useRef } from 'react'
import { Download, Upload } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { toast } from 'sonner'
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 fileInputRef = useRef<HTMLInputElement>(null)
const handleExport = async () => {
try {
const response = await fetch('/api/export-import')
if (!response.ok) {
throw new Error('Error al exportar')
}
const data = await response.json()
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' })
const url = URL.createObjectURL(blob)
const date = new Date().toISOString().split('T')[0]
const a = document.createElement('a')
a.href = url
a.download = `recall-backup-${date}.json`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
toast.success('Notas exportadas correctamente')
} catch {
toast.error('Error al exportar las notas')
}
}
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[]
if (isMarkdown) {
const note = parseMarkdownToNote(text, file.name)
payload = [note]
} else {
payload = JSON.parse(text)
}
const response = await fetch('/api/export-import', {
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')
}
toast.success(`${result.count} nota${result.count !== 1 ? 's' : ''} importada${result.count !== 1 ? 's' : ''} correctamente`)
if (fileInputRef.current) {
fileInputRef.current.value = ''
}
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Error al importar las notas')
} finally {
setImporting(false)
}
}
return (
<main className="container mx-auto py-8 px-4">
<h1 className="text-2xl font-bold mb-6">Configuración</h1>
<div className="grid gap-6 max-w-xl">
<Card>
<CardHeader>
<CardTitle>Exportar notas</CardTitle>
<CardDescription>
Descarga todas tus notas en formato JSON. El archivo incluye títulos, contenido, tipos y tags.
</CardDescription>
</CardHeader>
<CardContent>
<Button onClick={handleExport} className="gap-2">
<Download className="h-4 w-4" />
Exportar
</Button>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Importar notas</CardTitle>
<CardDescription>
Importa notas desde archivos JSON o MD. En archivos MD, el primer heading (#) se usa como título.
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-4">
<input
ref={fileInputRef}
type="file"
accept=".json,.md"
className="block w-full text-sm text-muted-foreground file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border file:border-input file:text-sm file:font-medium file:bg-background hover:file:bg-muted"
/>
<Button
onClick={handleImport}
disabled={importing}
variant="outline"
className="gap-2"
>
<Upload className="h-4 w-4" />
{importing ? 'Importando...' : 'Importar'}
</Button>
</CardContent>
</Card>
</div>
</main>
)
}