"use client"; import { useState, useEffect } from "react"; import { lmsApi, Notification } from "@/lib/api"; import { Bell, X, Calendar, Info, AlertTriangle, CheckCircle2 } from "lucide-react"; import Link from "next/link"; export default function NotificationCenter() { const [notifications, setNotifications] = useState([]); const [isOpen, setIsOpen] = useState(false); const [loading, setLoading] = useState(false); const fetchNotifications = async () => { setLoading(true); try { const data = await lmsApi.getNotifications(); setNotifications(data); } catch (err) { console.error("Failed to fetch notifications", err); } finally { setLoading(false); } }; useEffect(() => { fetchNotifications(); // Poll every 5 minutes const interval = setInterval(fetchNotifications, 300000); return () => clearInterval(interval); }, []); const markAsRead = async (id: string) => { try { await lmsApi.markNotificationAsRead(id); setNotifications(prev => prev.map(n => n.id === id ? { ...n, is_read: true } : n)); } catch (err) { console.error("Failed to mark as read", err); } }; const unreadCount = notifications.filter(n => !n.is_read).length; const getIcon = (type: string) => { switch (type) { case 'deadline': return ; case 'warning': return ; case 'success': return ; default: return ; } }; return (
{isOpen && ( <>
setIsOpen(false)} />

Notificaciones

{loading && notifications.length === 0 ? (
Cargando...
) : notifications.length === 0 ? (
No hay notificaciones
) : (
{notifications.map((n) => (
!n.is_read && markAsRead(n.id)} >
{getIcon(n.notification_type)}

{n.title}

{n.message}

{new Date(n.created_at).toLocaleDateString()} {n.link_url && ( setIsOpen(false)} > Ver detalles → )}
))}
)}
{notifications.length > 0 && (
)}
)}
); }