|
3 | 3 | import React, { useState } from "react"; |
4 | 4 | import { Megaphone } from "lucide-react"; |
5 | 5 | import { AnnouncementModal } from "./AnnouncementModal"; |
| 6 | +import { useNotificationStore } from "@/store/notification-store"; |
| 7 | +import { cn } from "@/lib/utils"; |
6 | 8 |
|
7 | 9 | export const AnnouncementButton: React.FC = () => { |
8 | 10 | const [isModalOpen, setIsModalOpen] = useState(false); |
| 11 | + const { notifications,markAsRead } = useNotificationStore(); |
| 12 | + const unreadAnnouncements = notifications.filter( |
| 13 | + (n) => n.type === "announcement" && !n.read |
| 14 | + ); |
| 15 | + |
| 16 | + const unreadCount = unreadAnnouncements.length; |
9 | 17 |
|
| 18 | + const handleOpen = () => { |
| 19 | + setIsModalOpen(true); |
| 20 | + unreadAnnouncements.forEach((n) => markAsRead(n.id)); |
| 21 | + }; |
| 22 | + |
10 | 23 | return ( |
11 | 24 | <> |
12 | 25 | <button |
13 | | - onClick={() => setIsModalOpen(true)} |
| 26 | + onClick={handleOpen} |
14 | 27 | className="relative rounded-lg p-2 text-gray-600 transition-colors hover:bg-gray-100 dark:text-gray-400 dark:hover:bg-gray-800" |
15 | | - aria-label="View announcements" |
| 28 | + aria-label={`Announcements${unreadCount > 0 ? ` - ${unreadCount} new` : ""}`} |
| 29 | + |
16 | 30 | title="Announcements" |
17 | 31 | > |
18 | | - <Megaphone className="h-5 w-5" /> |
| 32 | + <Megaphone |
| 33 | + className={cn( |
| 34 | + "h-5 w-5 text-gray-600 dark:text-gray-300 transition-transform", |
| 35 | + unreadCount > 0 && "animate-bell-ring" |
| 36 | + )} /> |
| 37 | + {unreadCount > 0 && ( |
| 38 | + <span className="absolute -right-1 -top-1 flex h-5 w-5 items-center justify-center"> |
| 39 | + <span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-red-400 opacity-75"></span> |
| 40 | + <span className="relative inline-flex h-5 w-5 items-center justify-center rounded-full bg-red-500 text-[10px] font-medium text-white"> |
| 41 | + {unreadCount > 99 ? "99+" : unreadCount} |
| 42 | + </span> |
| 43 | + </span> |
| 44 | + )} |
19 | 45 | </button> |
20 | 46 |
|
21 | 47 | <AnnouncementModal |
|
0 commit comments