'use client'; import { useMutation, useQuery } from '@tanstack/react-query'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { Icons } from '@/components/icons'; import { Button } from '@/components/ui/button'; import { NotificationCard, type NotificationAction } from '@/components/ui/notification-card'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Separator } from '@/components/ui/separator'; import { markAllNotificationsReadMutation, markNotificationReadMutation } from '../api/mutations'; import { notificationsQueryOptions } from '../api/queries'; const MAX_VISIBLE = 5; function buildNotificationActions(linkUrl: string | null): NotificationAction[] { if (!linkUrl) { return []; } return [{ id: `open:${linkUrl}`, label: 'Open', type: 'redirect', style: 'primary' }]; } export function NotificationCenter() { const router = useRouter(); const notificationsQuery = useQuery(notificationsQueryOptions({ page: 1, limit: MAX_VISIBLE })); const markRead = useMutation(markNotificationReadMutation); const markAllRead = useMutation(markAllNotificationsReadMutation); const notifications = notificationsQuery.data?.items ?? []; const unreadCount = notificationsQuery.data?.unreadCount ?? 0; return (

Notifications

{unreadCount > 0 && ( {unreadCount} new )} {unreadCount > 0 && ( )}
{notificationsQuery.isError ? (

Unable to load notifications

) : notificationsQuery.isLoading ? (
{Array.from({ length: 3 }).map((_, index) => (
))}
) : notifications.length === 0 ? (

No notifications yet

) : (
{notifications.map((notification) => ( markRead.mutate(id)} onAction={async (notificationId, actionId) => { if (actionId.startsWith('open:')) { await markRead.mutateAsync(notificationId); router.push(actionId.replace('open:', '')); } }} /> ))}
)} ); }