import { useState, useRef, useEffect } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Bell } from "lucide-react"; import { getUnreadCount } from "../api/notifications"; import NotificationDropdown from "./NotificationDropdown"; export default function NotificationBell() { const [open, setOpen] = useState(false); const ref = useRef(null); const queryClient = useQueryClient(); const { data } = useQuery({ queryKey: ["notifications", "unread-count"], queryFn: getUnreadCount, refetchInterval: 30000, // Poll every 30 seconds }); const count = data?.unread_count ?? 0; // Close dropdown on outside click useEffect(() => { function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); } } document.addEventListener("mousedown", handleClick); return () => document.removeEventListener("mousedown", handleClick); }, []); return (
{open && setOpen(false)} />}
); }