"use client"; import { useState, useEffect } from "react"; import { lmsApi, Notification } from "@/lib/api"; import { useAuth } from "@/context/AuthContext"; 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 { user } = useAuth(); const fetchNotifications = async () => { if (!user) return; setLoading(true); try { const data = await lmsApi.getNotifications(); setNotifications(data); } catch (err) { console.error("Failed to fetch notifications", err); } finally { setLoading(false); } }; useEffect(() => { if (user) { fetchNotifications(); // Poll every 5 minutes const interval = setInterval(fetchNotifications, 300000); return () => clearInterval(interval); } }, [user]); 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)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { !n.is_read && markAsRead(n.id); } }} aria-label={`Notification: ${n.title}. ${n.message}`} >

    {n.title}

    {n.message}

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