import { Combobox as ComboboxPrimitive } from '@base-ui/react'; import { router, usePage, WhenVisible } from '@inertiajs/react'; import { format } from 'date-fns'; import { Bell, CheckCheck, Loader2, Trash2 } from 'lucide-react'; import { useState } from 'react'; import { ConfirmDialog } from '@/components/confirm-dialog'; import { Button } from '@/components/ui/button'; import { Combobox, ComboboxContent } from '@/components/ui/combobox'; import { cn } from '@/lib/utils'; import { destroy, markAllRead, clearAll, read } from '@/routes/notifications'; import type { Notification, NotificationPage } from '@/types/notification'; export function NotificationBell() { const { props } = usePage<{ notifications?: NotificationPage }>(); const unreadCount = props.unreadNotificationsCount ?? 0; const notifications = props.notifications ?? { data: [], current_page: 1, last_page: 1, per_page: 15, total: 0, }; const [open, setOpen] = useState(false); const [clearAllOpen, setClearAllOpen] = useState(false); const hasMore = notifications.current_page < notifications.last_page; const hasNotifications = notifications.data.length > 0; const hasUnread = notifications.data.some((n) => !n.is_read); function handleMarkAllRead() { router.patch(markAllRead().url, {}, { preserveScroll: true }); } function handleClearAll() { router.delete(clearAll().url, { preserveScroll: true, onSuccess: () => setClearAllOpen(false), }); } function handleMarkRead(notification: Notification) { router.patch(read(notification.id).url, {}, { preserveScroll: true }); } function handleDelete(notification: Notification) { router.delete(destroy(notification.id).url, { preserveScroll: true }); } return ( <> } > {unreadCount > 0 && ( {unreadCount > 99 ? '99+' : unreadCount} )} Notifikasi Notifikasi setClearAllOpen(true)} > {hasNotifications ? ( {notifications.data.map((notification) => ( {!notification.is_read && ( )} {notification.title ?? '-'} {notification.content && ( {notification.content} )} {format( new Date( notification.created_at, ), 'd MMM yyyy, HH:mm', )} {!notification.is_read && ( handleMarkRead( notification, ) } > )} handleDelete(notification) } > ))} {hasMore && ( } > )} ) : ( Belum ada notifikasi. )} > ); }
{notification.title ?? '-'}
{notification.content}
{format( new Date( notification.created_at, ), 'd MMM yyyy, HH:mm', )}