// NotificationBell Component // Bell icon with unread count badge and dropdown for notifications const { useState, useRef, useEffect } = React; function NotificationBell({ onNotificationClick, onShowAll }) { // Get notification context - handle case where it might not be available let notificationContext; try { notificationContext = useNotifications(); } catch (e) { console.error('NotificationBell: useNotifications not available', e); // Return empty bell if context not ready return ( ); } const { notifications, unreadCount, loading, markAsRead, markAllAsRead, fetchNotifications } = notificationContext; const [isOpen, setIsOpen] = useState(false); const dropdownRef = useRef(null); const buttonRef = useRef(null); // Close dropdown when clicking outside useEffect(() => { const handleClickOutside = (event) => { if ( dropdownRef.current && !dropdownRef.current.contains(event.target) && buttonRef.current && !buttonRef.current.contains(event.target) ) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); // Refresh notifications when dropdown opens useEffect(() => { if (isOpen) { fetchNotifications(20); } }, [isOpen, fetchNotifications]); const toggleDropdown = () => { setIsOpen(prev => !prev); }; const handleNotificationClick = async (notification) => { // Mark as read if (!notification.is_read) { await markAsRead(notification.id); } // Close dropdown setIsOpen(false); // Navigate to the relevant item if (onNotificationClick) { onNotificationClick(notification); } }; const handleMarkAllRead = async () => { await markAllAsRead(); }; const formatTime = (dateString) => { if (!dateString) return ''; const date = new Date(dateString); const now = new Date(); const diffMs = now - date; const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMs / 3600000); const diffDays = Math.floor(diffMs / 86400000); if (diffMins < 1) return 'Adesso'; if (diffMins < 60) return `${diffMins}m fa`; if (diffHours < 24) return `${diffHours}h fa`; if (diffDays < 7) return `${diffDays}g fa`; return date.toLocaleDateString('it-IT', { day: '2-digit', month: 'short' }); }; const getNotificationIcon = (type) => { switch (type) { case 'project_status_changed': return ( ); case 'project_assigned': return ( ); default: return ( ); } }; return (
Caricamento...
Nessuna notifica