'use client' import { useState } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription, DialogFooter } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { History, RotateCcw } from 'lucide-react' import { toast } from 'sonner' interface Version { id: string title: string content: string createdAt: string } interface VersionHistoryProps { noteId: string } export function VersionHistory({ noteId }: VersionHistoryProps) { const [open, setOpen] = useState(false) const [versions, setVersions] = useState([]) const [loading, setLoading] = useState(false) const [restoring, setRestoring] = useState(null) const [confirmRestore, setConfirmRestore] = useState(null) const fetchVersions = async () => { setLoading(true) try { const res = await fetch(`/api/notes/${noteId}/versions`) const data = await res.json() if (data.success) { setVersions(data.data) } } catch { toast.error('Error al cargar las versiones') } finally { setLoading(false) } } const handleOpenChange = (newOpen: boolean) => { setOpen(newOpen) if (newOpen && versions.length === 0) { fetchVersions() } } const handleRestore = async (versionId: string) => { setRestoring(versionId) try { const res = await fetch(`/api/notes/${noteId}/versions/${versionId}`, { method: 'PUT', }) const data = await res.json() if (data.success) { toast.success('Versión restaurada correctamente') setOpen(false) window.location.reload() } else { toast.error(data.error || 'Error al restaurar la versión') } } catch { toast.error('Error al restaurar la versión') } finally { setRestoring(null) setConfirmRestore(null) } } const formatDate = (dateString: string) => { const date = new Date(dateString) return date.toLocaleString('es-ES', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }) } return (
handleOpenChange(true)}>
Historial de versiones Revisa y restaura versiones anteriores de esta nota
{loading ? (
Cargando versiones...
) : versions.length === 0 ? (
No hay versiones guardadas
) : (
{versions.map((version) => (
{formatDate(version.createdAt)}
{version.title}
{version.content.substring(0, 50)}...
{confirmRestore === version.id ? (
¿Restaurar?
) : ( )}
))}
)}
) }