import { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { getUserHistory, deleteHistoryItem } from '../services/docService'; import Step3FinalGraph from '../components/editor/Step3FinalGraph'; import { FiEye, FiTrash2, FiBarChart2, FiInbox, FiClock } from 'react-icons/fi'; export default function History() { const [historyItems, setHistoryItems] = useState([]); const [isLoading, setIsLoading] = useState(true); const [expandedId, setExpandedId] = useState(null); useEffect(() => { fetchHistory(); }, []); const fetchHistory = async () => { setIsLoading(true); try { const data = await getUserHistory(); const items = Array.isArray(data) ? data : data.history || data.items || []; setHistoryItems(items.reverse()); } catch (error) { console.error("Error fetching history:", error); alert("Hubo un problema al cargar el historial."); } finally { setIsLoading(false); } }; const handleDelete = async (id) => { if (!window.confirm('¿Seguro que quieres borrar este modelo definitivamente?')) return; try { await deleteHistoryItem(id); setHistoryItems(prev => prev.filter(item => item._id !== id && item.id !== id)); if (expandedId === id) setExpandedId(null); } catch (error) { alert("Error al borrar: " + error); } }; const toggleExpand = (id) => { setExpandedId(expandedId === id ? null : id); }; return (
{/* Cabecera */}

Mi Historial

Aquí están todas las gráficas y modelos que has guardado.

+ Nuevo Modelo
{/* Lista de Historial */} {isLoading ? (

Cargando tus gráficas...

) : historyItems.length === 0 ? (

Aún no has guardado ningún modelo.

Ve al editor, crea una gráfica y dale a "Guardar".

) : (
{historyItems.map((item) => { const itemId = item._id || item.id; const isExpanded = expandedId === itemId; return (
{/* Cabecera de la Card */}

{item.name || 'Modelo sin título'}

{item.created_at ? `Guardado el ${new Date(item.created_at).toLocaleDateString('es-ES', { day: '2-digit', month: 'long', year: 'numeric' })}` : 'Guardado en el historial' }

{/* Contenido Desplegable (La gráfica)*/}
{isExpanded ? ( ) : (
)}
); })}
)}
); }