Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 1x | import React, { useState, useRef, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { Bell, Check, CheckCheck, Trash2, X, Ticket, Calendar, MessageSquare } from 'lucide-react';
import {
useNotifications,
useUnreadNotificationCount,
useMarkNotificationRead,
useMarkAllNotificationsRead,
useClearAllNotifications,
} from '../hooks/useNotifications';
import { Notification } from '../api/notifications';
interface NotificationDropdownProps {
variant?: 'light' | 'dark';
onTicketClick?: (ticketId: string) => void;
}
const NotificationDropdown: React.FC<NotificationDropdownProps> = ({ variant = 'dark', onTicketClick }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const { data: notifications = [], isLoading } = useNotifications({ limit: 20 });
const { data: unreadCount = 0 } = useUnreadNotificationCount();
const markReadMutation = useMarkNotificationRead();
const markAllReadMutation = useMarkAllNotificationsRead();
const clearAllMutation = useClearAllNotifications();
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
const handleNotificationClick = (notification: Notification) => {
// Mark as read
if (!notification.read) {
markReadMutation.mutate(notification.id);
}
// Handle ticket notifications specially - open modal instead of navigating
if (notification.target_type === 'ticket' && onTicketClick) {
const ticketId = notification.data?.ticket_id;
if (ticketId) {
onTicketClick(String(ticketId));
setIsOpen(false);
return;
}
}
// Navigate to target if available
if (notification.target_url) {
navigate(notification.target_url);
setIsOpen(false);
}
};
const handleMarkAllRead = () => {
markAllReadMutation.mutate();
};
const handleClearAll = () => {
clearAllMutation.mutate();
};
const getNotificationIcon = (targetType: string | null) => {
switch (targetType) {
case 'ticket':
return <Ticket size={16} className="text-blue-500" />;
case 'event':
case 'appointment':
return <Calendar size={16} className="text-green-500" />;
default:
return <MessageSquare size={16} className="text-gray-500" />;
}
};
const formatTimestamp = (timestamp: string) => {
const date = new Date(timestamp);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
if (diffMins < 1) return t('notifications.justNow', 'Just now');
if (diffMins < 60) return t('notifications.minutesAgo', '{{count}}m ago', { count: diffMins });
if (diffHours < 24) return t('notifications.hoursAgo', '{{count}}h ago', { count: diffHours });
if (diffDays < 7) return t('notifications.daysAgo', '{{count}}d ago', { count: diffDays });
return date.toLocaleDateString();
};
const buttonClasses = variant === 'light'
? 'p-2 rounded-md text-white/80 hover:text-white hover:bg-white/10 transition-colors'
: 'relative p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 transition-colors rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700';
return (
<div className="relative" ref={dropdownRef}>
{/* Bell Button */}
<button
onClick={() => setIsOpen(!isOpen)}
className={buttonClasses}
aria-label={t('notifications.openNotifications', 'Open notifications')}
>
<Bell size={20} />
{unreadCount > 0 && (
<span className="absolute top-1 right-1 flex items-center justify-center min-w-[18px] h-[18px] px-1 text-[10px] font-bold text-white bg-red-500 rounded-full">
{unreadCount > 99 ? '99+' : unreadCount}
</span>
)}
</button>
{/* Dropdown */}
{isOpen && (
<div className="absolute right-0 mt-2 w-80 sm:w-96 bg-white dark:bg-gray-800 rounded-xl shadow-xl border border-gray-200 dark:border-gray-700 z-50 overflow-hidden">
{/* Header */}
<div className="px-4 py-3 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
<h3 className="text-sm font-semibold text-gray-900 dark:text-white">
{t('notifications.title', 'Notifications')}
</h3>
<div className="flex items-center gap-2">
{unreadCount > 0 && (
<button
onClick={handleMarkAllRead}
disabled={markAllReadMutation.isPending}
className="p-1.5 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
title={t('notifications.markAllRead', 'Mark all as read')}
>
<CheckCheck size={16} />
</button>
)}
<button
onClick={() => setIsOpen(false)}
className="p-1.5 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
>
<X size={16} />
</button>
</div>
</div>
{/* Notification List */}
<div className="max-h-96 overflow-y-auto">
{isLoading ? (
<div className="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
{t('common.loading')}
</div>
) : notifications.length === 0 ? (
<div className="px-4 py-8 text-center">
<Bell size={32} className="mx-auto text-gray-300 dark:text-gray-600 mb-2" />
<p className="text-sm text-gray-500 dark:text-gray-400">
{t('notifications.noNotifications', 'No notifications yet')}
</p>
</div>
) : (
<div className="divide-y divide-gray-100 dark:divide-gray-700">
{notifications.map((notification) => (
<button
key={notification.id}
onClick={() => handleNotificationClick(notification)}
className={`w-full px-4 py-3 text-left hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors ${
!notification.read ? 'bg-blue-50/50 dark:bg-blue-900/10' : ''
}`}
>
<div className="flex items-start gap-3">
<div className="mt-0.5">
{getNotificationIcon(notification.target_type)}
</div>
<div className="flex-1 min-w-0">
<p className={`text-sm ${!notification.read ? 'font-medium' : ''} text-gray-900 dark:text-white`}>
<span className="font-medium">{notification.actor_display || 'System'}</span>
{' '}
{notification.verb}
</p>
{notification.target_display && (
<p className="text-sm text-gray-500 dark:text-gray-400 truncate mt-0.5">
{notification.target_display}
</p>
)}
<p className="text-xs text-gray-400 dark:text-gray-500 mt-1">
{formatTimestamp(notification.timestamp)}
</p>
</div>
{!notification.read && (
<span className="w-2 h-2 bg-blue-500 rounded-full flex-shrink-0 mt-2"></span>
)}
</div>
</button>
))}
</div>
)}
</div>
{/* Footer */}
{notifications.length > 0 && (
<div className="px-4 py-3 border-t border-gray-200 dark:border-gray-700 flex items-center justify-between">
<button
onClick={handleClearAll}
disabled={clearAllMutation.isPending}
className="text-xs text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 flex items-center gap-1"
>
<Trash2 size={12} />
{t('notifications.clearRead', 'Clear read')}
</button>
<button
onClick={() => {
navigate('/notifications');
setIsOpen(false);
}}
className="text-xs text-brand-600 hover:text-brand-700 dark:text-brand-400 font-medium"
>
{t('notifications.viewAll', 'View all')}
</button>
</div>
)}
</div>
)}
</div>
);
};
export default NotificationDropdown;
|