'use client' import { Activity, Bell, ClipboardList, UserPlus, UserRoundCheck, type LucideIcon, } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/navigation' import { useCallback, useEffect, useState, useTransition } from 'react' import { openCampaignNotifications, type NotificationBellData, } from '@/app/(campaign)/campanha/actions/notifications' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@/components/ui/dialog' import { Drawer, DrawerContent, DrawerDescription, DrawerTitle } from '@/components/ui/Drawer' import { useIsMobile } from '@/hooks/use-mobile' import { CAMPAIGN_NOTIFICATION_LOAD_ERROR_MESSAGE } from '@/lib/campaignNotificationCopy' import type { NotificationListItem } from '@/lib/notificationContract' import { notificationTypeLabels, type NotificationType } from '@/lib/notificationContract' import { formatRelativeAge } from '@/utilities/formatRelativeAge' const notificationIcons: Record = { municipality_update: ClipboardList, new_supporter: UserPlus, activity_attention: Activity, invite_accepted: UserRoundCheck, } type CampaignNotificationBellProps = NotificationBellData /** * The notification bell (C108): opening the panel loads the list AND marks * everything as read in one server call — the open gesture is the read * gesture, so the badge zeroes without a separate button. Desktop renders a * centered Dialog (X + click-outside close), mobile a bottom sheet. */ export const CampaignNotificationBell = ({ unreadCount: initialUnreadCount, vapidPublicKey: _vapidPublicKey, }: CampaignNotificationBellProps) => { const router = useRouter() const isMobile = useIsMobile() const [open, setOpen] = useState(false) const [unreadCount, setUnreadCount] = useState(initialUnreadCount) const [items, setItems] = useState([]) const [isPending, startTransition] = useTransition() const [loadError, setLoadError] = useState(null) const nowMs = Date.now() useEffect(() => { setUnreadCount(initialUnreadCount) }, [initialUnreadCount]) const loadPanel = useCallback(() => { startTransition(async () => { setLoadError(null) const result = await openCampaignNotifications() if ('status' in result && result.status === 'success') { setItems(result.items) setUnreadCount(0) if (result.markedCount > 0) { // Re-sync the sibling bell instance rendered by the other header. // (A cross-tab mark racing between this render and the open action // yields markedCount 0 and a stale sibling badge until the next // navigation — narrow, self-healing, not worth a refresh per open.) router.refresh() } } else { setLoadError(result.message ?? CAMPAIGN_NOTIFICATION_LOAD_ERROR_MESSAGE) } }) }, [router]) useEffect(() => { if (!open) return loadPanel() }, [open, loadPanel]) const unreadLabel = unreadCount === 0 ? 'Notificações' : unreadCount === 1 ? '1 notificação não lida' : `${unreadCount} notificações não lidas` const panelItems = ( <> {loadError ? (

{loadError}

) : null} {isPending && items.length === 0 ? ( <>
) : null} {!loadError && items.length === 0 && !isPending ? (

Nenhuma notificação por aqui.

) : null} {items.map((item) => { const Icon = notificationIcons[item.type] return ( setOpen(false)} className="flex gap-3 rounded-lg border p-3 transition-colors hover:bg-muted/50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring" > {item.payload.title} {item.payload.detail} {notificationTypeLabels[item.type]} · ) })} ) return ( <> {isMobile ? ( Notificações Avisos da campanha para você
{panelItems}
) : ( Notificações Avisos da campanha para você
{panelItems}
)} ) }