'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() { 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 useEffect(() => { function handleClickOutside(e: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setOpen(false) } } if (open) { document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) } }, [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 && (

Notifications

{unreadCount > 0 && ( )}
{loading && (
{Array.from({ length: 4 }).map((_, i) => (
))}
)} {error && (
{error}
)} {!loading && !error && (notifications?.length ?? 0) === 0 && (
No notifications yet
)} {!loading && !error && (notifications?.length ?? 0) > 0 && (
    {(notifications ?? []).map((n) => (
  • {n.link_url ? ( handleNotificationClick(n)} className={`block px-4 py-3 hover:bg-neutral-50 dark:hover:bg-neutral-800/50 transition-colors ${!n.read ? 'bg-brand-orange/5 dark:bg-brand-orange/10' : ''}`} >
    {getTypeIcon(n.type)}

    {n.title}

    {n.body && (

    {n.body}

    )}

    {formatTimeAgo(n.created_at)}

    ) : (
    handleNotificationClick(n)} onKeyDown={(e) => e.key === 'Enter' && handleNotificationClick(n)} className={`block px-4 py-3 hover:bg-neutral-50 dark:hover:bg-neutral-800/50 cursor-pointer ${!n.read ? 'bg-brand-orange/5 dark:bg-brand-orange/10' : ''}`} >
    {getTypeIcon(n.type)}

    {n.title}

    {n.body && (

    {n.body}

    )}

    {formatTimeAgo(n.created_at)}

    )}
  • ))}
)}
setOpen(false)} className="text-sm text-brand-orange hover:underline" > View all setOpen(false)} className="flex items-center gap-2 text-sm text-neutral-500 dark:text-neutral-400 hover:text-brand-orange dark:hover:text-brand-orange transition-colors" > Manage settings
)}
) }