'use client' /** * @file Notification center: bell icon with dropdown of recent notifications. */ import { useEffect, useState, useRef } from 'react' import Link from 'next/link' import { listNotifications, markNotificationRead, markAllNotificationsRead, type Notification } from '@/lib/api/notifications' import { getAuthErrorMessage } from '@ciphera-net/ui' import { formatTimeAgo, getTypeIcon } from '@/lib/utils/notifications' import { SettingsIcon } from '@ciphera-net/ui' import { SkeletonLine, SkeletonCircle } from '@/components/skeletons' // * Bell icon (simple SVG, no extra deps) function BellIcon({ className }: { className?: string }) { return ( ) } const LOADING_DELAY_MS = 250 const POLL_INTERVAL_MS = 90_000 export default function NotificationCenter({ anchor = 'bottom' }: { anchor?: 'bottom' | 'right' }) { const [open, setOpen] = useState(false) const [notifications, setNotifications] = useState([]) const [unreadCount, setUnreadCount] = useState(0) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const dropdownRef = useRef(null) const fetchUnreadCount = async () => { try { const res = await listNotifications({ limit: 1 }) setUnreadCount(typeof res?.unread_count === 'number' ? res.unread_count : 0) } catch { // Ignore polling errors } } const fetchNotifications = async () => { setError(null) const loadingTimer = setTimeout(() => setLoading(true), LOADING_DELAY_MS) try { const res = await listNotifications({}) setNotifications(Array.isArray(res?.notifications) ? res.notifications : []) setUnreadCount(typeof res?.unread_count === 'number' ? res.unread_count : 0) } catch (err) { setError(getAuthErrorMessage(err as Error) || 'Failed to load notifications') setNotifications([]) setUnreadCount(0) } finally { clearTimeout(loadingTimer) setLoading(false) } } useEffect(() => { if (open) { fetchNotifications() } }, [open]) // * Poll unread count in background (when authenticated) useEffect(() => { fetchUnreadCount() const id = setInterval(fetchUnreadCount, POLL_INTERVAL_MS) return () => clearInterval(id) }, []) // * Close dropdown when clicking outside or pressing Escape useEffect(() => { if (!open) return function handleClickOutside(e: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setOpen(false) } } function handleKeyDown(e: KeyboardEvent) { if (e.key === 'Escape') setOpen(false) } document.addEventListener('mousedown', handleClickOutside) document.addEventListener('keydown', handleKeyDown) return () => { document.removeEventListener('mousedown', handleClickOutside) document.removeEventListener('keydown', handleKeyDown) } }, [open]) const handleMarkRead = async (id: string) => { try { await markNotificationRead(id) setNotifications((prev) => prev.map((n) => (n.id === id ? { ...n, read: true } : n))) setUnreadCount((c) => Math.max(0, c - 1)) } catch { // Ignore; user can retry } } const handleMarkAllRead = async () => { try { await markAllNotificationsRead() setNotifications((prev) => prev.map((n) => ({ ...n, read: true }))) setUnreadCount(0) } catch { // Ignore } } const handleNotificationClick = (n: Notification) => { if (!n.read) { handleMarkRead(n.id) } setOpen(false) } return (
{open && ( )}
) }