({
open,
onClose,
anchorRef,
onUnreadCountChange,
}: {
open: boolean;
onClose: () => void;
anchorRef: React.RefObject<HTMLButtonElement | null>;
onUnreadCountChange?: (count: number) => void;
})
| 22 | }; |
| 23 | |
| 24 | export default function NotificationPanel({ |
| 25 | open, |
| 26 | onClose, |
| 27 | anchorRef, |
| 28 | onUnreadCountChange, |
| 29 | }: { |
| 30 | open: boolean; |
| 31 | onClose: () => void; |
| 32 | anchorRef: React.RefObject<HTMLButtonElement | null>; |
| 33 | onUnreadCountChange?: (count: number) => void; |
| 34 | }) { |
| 35 | const { t: tr } = useI18n(); |
| 36 | const presence = useAnimatedVisibility(open, 160); |
| 37 | const [events, setEvents] = useState<any[]>([]); |
| 38 | const [loading, setLoading] = useState(false); |
| 39 | const [filter, setFilter] = useState<string>(''); |
| 40 | const panelRef = useRef<HTMLDivElement>(null); |
| 41 | const navigate = useNavigate(); |
| 42 | |
| 43 | const load = useCallback(async () => { |
| 44 | setLoading(true); |
| 45 | try { |
| 46 | const params = filter ? `type=${filter}` : ''; |
| 47 | const data = await api.getEvents(params); |
| 48 | setEvents(data); |
| 49 | |
| 50 | // Auto mark all as read on open |
| 51 | const hasUnread = Array.isArray(data) && data.some((e: any) => !e.read); |
| 52 | if (hasUnread) { |
| 53 | api.markAllEventsRead().catch(() => {}); |
| 54 | onUnreadCountChange?.(0); |
| 55 | } |
| 56 | } catch { /* ignore */ } |
| 57 | finally { setLoading(false); } |
| 58 | }, [filter, onUnreadCountChange]); |
| 59 | |
| 60 | useEffect(() => { |
| 61 | if (open) load(); |
| 62 | }, [open, load]); |
| 63 | |
| 64 | useEffect(() => { |
| 65 | const handler = (e: MouseEvent) => { |
| 66 | if ( |
| 67 | panelRef.current && !panelRef.current.contains(e.target as Node) && |
| 68 | anchorRef.current && !anchorRef.current.contains(e.target as Node) |
| 69 | ) { |
| 70 | onClose(); |
| 71 | } |
| 72 | }; |
| 73 | if (open) document.addEventListener('mousedown', handler); |
| 74 | return () => document.removeEventListener('mousedown', handler); |
| 75 | }, [open, onClose, anchorRef]); |
| 76 | |
| 77 | const clearAll = async () => { |
| 78 | await api.clearEvents(); |
| 79 | setEvents([]); |
| 80 | onUnreadCountChange?.(0); |
| 81 | }; |
nothing calls this directly
no test coverage detected