import React, { useState, useEffect } from 'react' import { createPortal } from 'react-dom' import { useNavigate } from 'react-router' import { Bell, Trash2, CheckCheck } from 'lucide-react' import { useTranslation } from '../../i18n' import { useInAppNotificationStore } from '../../store/inAppNotificationStore.ts' import { useAuthStore } from '../../store/authStore' import NotificationDayList from '../Notifications/NotificationDayList' import ConfirmDialog from '../shared/ConfirmDialog' export default function InAppNotificationBell(): React.ReactElement { const { t } = useTranslation() const navigate = useNavigate() const isAuthenticated = useAuthStore(s => s.isAuthenticated) const { notifications, unreadCount, isLoading, fetchNotifications, fetchUnreadCount, markAllRead, deleteAll } = useInAppNotificationStore() const [open, setOpen] = useState(false) const [confirmingDeleteAll, setConfirmingDeleteAll] = useState(false) useEffect(() => { if (isAuthenticated) { fetchUnreadCount() } }, [isAuthenticated]) const handleOpen = () => { if (!open) { fetchNotifications(true) } setOpen(v => !v) } const handleShowAll = () => { setOpen(false) navigate('/notifications') } const displayCount = unreadCount > 99 ? '99+' : unreadCount return (
{open && createPortal( <> {/* Click-away catcher only — the bell button itself closes the panel again from the keyboard. */}
setOpen(false)} />
{/* Header: the dialogs' head, a tile, the name and how many are unread, the tools at the end. */}
{t('notifications.title')} {unreadCount > 0 && ( {unreadCount} )}
{unreadCount > 0 && ( )} {notifications.length > 0 && ( )}
{/* Notification list */}
{isLoading && notifications.length === 0 ? (
) : notifications.length === 0 ? (

{t('notifications.empty')}

{t('notifications.emptyDescription')}

) : ( setOpen(false)} compact /> )}
{/* Footer */}
, document.body )} setConfirmingDeleteAll(false)} onConfirm={() => { setConfirmingDeleteAll(false); void deleteAll() }} title={t('notifications.deleteAllConfirmTitle')} message={t('notifications.deleteAllConfirmText')} confirmLabel={t('notifications.deleteAll')} />
) }