|
| 1 | +import { useState } from "react" |
| 2 | +import { useNavigate } from "react-router-dom" |
| 3 | +import { Bell } from "lucide-react" |
| 4 | +import { useTranslation } from "react-i18next" |
| 5 | +import { useNotifications, useUnreadCount, useNotificationMutations } from "@/hooks/use-notifications" |
| 6 | +import { |
| 7 | + DropdownMenu, |
| 8 | + DropdownMenuContent, |
| 9 | + DropdownMenuItem, |
| 10 | + DropdownMenuSeparator, |
| 11 | + DropdownMenuTrigger, |
| 12 | +} from "@/components/ui/dropdown-menu" |
| 13 | + |
| 14 | +const severityColors: Record<string, string> = { |
| 15 | + info: "bg-blue-500", |
| 16 | + warning: "bg-yellow-500", |
| 17 | + critical: "bg-red-500", |
| 18 | +} |
| 19 | + |
| 20 | +function timeAgo(dateStr: string): string { |
| 21 | + const now = Date.now() |
| 22 | + const then = new Date(dateStr).getTime() |
| 23 | + const diffMs = now - then |
| 24 | + const diffMin = Math.floor(diffMs / 60000) |
| 25 | + if (diffMin < 1) return "just now" |
| 26 | + if (diffMin < 60) return `${diffMin}m ago` |
| 27 | + const diffHrs = Math.floor(diffMin / 60) |
| 28 | + if (diffHrs < 24) return `${diffHrs}h ago` |
| 29 | + const diffDays = Math.floor(diffHrs / 24) |
| 30 | + return `${diffDays}d ago` |
| 31 | +} |
| 32 | + |
| 33 | +export function NotificationBell() { |
| 34 | + const { t } = useTranslation() |
| 35 | + const navigate = useNavigate() |
| 36 | + const { count, reset, decrement } = useUnreadCount() |
| 37 | + const { notifications, reload } = useNotifications(10) |
| 38 | + const { markRead, markAllRead } = useNotificationMutations() |
| 39 | + const [open, setOpen] = useState(false) |
| 40 | + |
| 41 | + const handleOpen = (isOpen: boolean) => { |
| 42 | + setOpen(isOpen) |
| 43 | + if (isOpen) { |
| 44 | + reload() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const handleMarkRead = async (id: string) => { |
| 49 | + await markRead(id) |
| 50 | + decrement(1) |
| 51 | + reload() |
| 52 | + } |
| 53 | + |
| 54 | + const handleMarkAllRead = async () => { |
| 55 | + await markAllRead() |
| 56 | + reset() |
| 57 | + reload() |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <DropdownMenu open={open} onOpenChange={handleOpen}> |
| 62 | + <DropdownMenuTrigger asChild> |
| 63 | + <button className="relative rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground transition-colors"> |
| 64 | + <Bell className="size-4" /> |
| 65 | + {count > 0 && ( |
| 66 | + <span className="absolute -top-0.5 -right-0.5 flex size-4 items-center justify-center rounded-full bg-red-500 text-[10px] font-medium text-white"> |
| 67 | + {count > 99 ? "99+" : count} |
| 68 | + </span> |
| 69 | + )} |
| 70 | + </button> |
| 71 | + </DropdownMenuTrigger> |
| 72 | + <DropdownMenuContent align="end" className="w-80"> |
| 73 | + <div className="flex items-center justify-between px-3 py-2"> |
| 74 | + <span className="text-sm font-medium">{t('notifications.title')}</span> |
| 75 | + {count > 0 && ( |
| 76 | + <button |
| 77 | + onClick={handleMarkAllRead} |
| 78 | + className="text-xs text-muted-foreground hover:text-foreground transition-colors" |
| 79 | + > |
| 80 | + {t('notifications.markAllRead')} |
| 81 | + </button> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + <DropdownMenuSeparator /> |
| 85 | + {notifications.length === 0 ? ( |
| 86 | + <div className="px-3 py-6 text-center text-sm text-muted-foreground"> |
| 87 | + {t('notifications.empty')} |
| 88 | + </div> |
| 89 | + ) : ( |
| 90 | + <> |
| 91 | + {notifications.map((n) => ( |
| 92 | + <DropdownMenuItem |
| 93 | + key={n.id} |
| 94 | + className="flex items-start gap-2.5 px-3 py-2.5 cursor-pointer" |
| 95 | + onClick={() => { |
| 96 | + if (!n.read) handleMarkRead(n.id) |
| 97 | + }} |
| 98 | + > |
| 99 | + <div className={`mt-1.5 h-2 w-2 shrink-0 rounded-full ${severityColors[n.severity] || severityColors.info}`} /> |
| 100 | + <div className="min-w-0 flex-1"> |
| 101 | + <div className={`text-sm truncate ${n.read ? "text-muted-foreground" : "font-medium"}`}> |
| 102 | + {n.title} |
| 103 | + </div> |
| 104 | + <div className="text-xs text-muted-foreground mt-0.5 truncate"> |
| 105 | + {n.body} |
| 106 | + </div> |
| 107 | + <div className="text-xs text-muted-foreground/60 mt-0.5"> |
| 108 | + {timeAgo(n.created_at)} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </DropdownMenuItem> |
| 112 | + ))} |
| 113 | + <DropdownMenuSeparator /> |
| 114 | + <DropdownMenuItem |
| 115 | + className="justify-center text-xs text-muted-foreground cursor-pointer" |
| 116 | + onClick={() => { |
| 117 | + setOpen(false) |
| 118 | + navigate("/console/notifications") |
| 119 | + }} |
| 120 | + > |
| 121 | + {t('notifications.viewAll')} |
| 122 | + </DropdownMenuItem> |
| 123 | + </> |
| 124 | + )} |
| 125 | + </DropdownMenuContent> |
| 126 | + </DropdownMenu> |
| 127 | + ) |
| 128 | +} |
0 commit comments