()
| 47 | }; |
| 48 | |
| 49 | export function NotificationDropdown() { |
| 50 | const [isOpen, setIsOpen] = useState(false); |
| 51 | const [notifications, setNotifications] = useState<Notification[]>([]); |
| 52 | const [unreadCount, setUnreadCount] = useState(0); |
| 53 | const [loading, setLoading] = useState(false); |
| 54 | const dropdownRef = useRef<HTMLDivElement>(null); |
| 55 | const isMobile = useIsMobile(); |
| 56 | |
| 57 | const fetchNotifications = async () => { |
| 58 | try { |
| 59 | setLoading(true); |
| 60 | const res = await fetch("/api/notifications"); |
| 61 | const data = await res.json(); |
| 62 | setNotifications(data.notifications || []); |
| 63 | setUnreadCount(data.unreadCount || 0); |
| 64 | } catch (error) { |
| 65 | console.error("Failed to fetch notifications:", error); |
| 66 | } finally { |
| 67 | setLoading(false); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | useEffect(() => { |
| 72 | fetchNotifications(); |
| 73 | // Poll for new notifications every 30 seconds |
| 74 | const interval = setInterval(fetchNotifications, 30000); |
| 75 | return () => clearInterval(interval); |
| 76 | }, []); |
| 77 | |
| 78 | useEffect(() => { |
| 79 | // Close dropdown when clicking outside |
| 80 | const handleClickOutside = (event: MouseEvent) => { |
| 81 | if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { |
| 82 | setIsOpen(false); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | if (isOpen) { |
| 87 | document.addEventListener("mousedown", handleClickOutside); |
| 88 | } |
| 89 | |
| 90 | return () => { |
| 91 | document.removeEventListener("mousedown", handleClickOutside); |
| 92 | }; |
| 93 | }, [isOpen]); |
| 94 | |
| 95 | const markAsRead = async (id: string) => { |
| 96 | try { |
| 97 | await fetch("/api/notifications", { |
| 98 | method: "PATCH", |
| 99 | headers: { "Content-Type": "application/json" }, |
| 100 | body: JSON.stringify({ id, read: true }), |
| 101 | }); |
| 102 | await fetchNotifications(); |
| 103 | } catch (error) { |
| 104 | console.error("Failed to mark as read:", error); |
| 105 | } |
| 106 | }; |
nothing calls this directly
no test coverage detected