// frontend/src/components/NotificationCenter.tsx import { useEffect, useMemo, useRef, useState } from 'react' import { BellIcon, EnvelopeOpenIcon, FaceSmileIcon, } from '@heroicons/react/20/solid' import Button from './Button' import Tabs, { type TabItem } from './Tabs' import type { Notification } from './types' type NotificationCenterProps = { notifications: Notification[] unreadCount: number onMarkRead: (notificationId: string) => void | Promise onMarkAllRead: () => void | Promise onNotificationClick?: (notification: Notification) => void } type NotificationTab = 'unread' | 'all' function formatNotificationDate(value: string) { try { return new Intl.DateTimeFormat('de-DE', { dateStyle: 'short', timeStyle: 'short', }).format(new Date(value)) } catch { return value } } export default function NotificationCenter({ notifications, unreadCount, onMarkRead, onMarkAllRead, onNotificationClick, }: NotificationCenterProps) { const [open, setOpen] = useState(false) const [activeTab, setActiveTab] = useState('unread') const containerRef = useRef(null) const filteredNotifications = useMemo(() => { if (activeTab === 'unread') { return notifications.filter((notification) => !notification.readAt) } return notifications }, [activeTab, notifications]) const notificationTabs: TabItem[] = [ { name: 'Ungelesen', href: '#', current: activeTab === 'unread', count: unreadCount > 0 ? unreadCount : undefined, onClick: () => setActiveTab('unread'), }, { name: 'Alle', href: '#', current: activeTab === 'all', count: notifications.length > 0 ? notifications.length : undefined, onClick: () => setActiveTab('all'), }, ] useEffect(() => { if (!open) { return } function handlePointerDown(event: PointerEvent) { const target = event.target if (!(target instanceof Node)) { return } if (containerRef.current?.contains(target)) { return } setOpen(false) } function handleKeyDown(event: KeyboardEvent) { if (event.key === 'Escape') { setOpen(false) } } document.addEventListener('pointerdown', handlePointerDown) document.addEventListener('keydown', handleKeyDown) return () => { document.removeEventListener('pointerdown', handlePointerDown) document.removeEventListener('keydown', handleKeyDown) } }, [open]) function getEmptyText() { if (activeTab === 'unread') { return 'Keine ungelesenen Benachrichtigungen vorhanden.' } return 'Keine Benachrichtigungen vorhanden.' } function handleNotificationClick(notification: Notification) { void onMarkRead(notification.id) onNotificationClick?.(notification) setOpen(false) } return (
{open && (

Benachrichtigungen

{unreadCount > 0 && ( )}
{filteredNotifications.length === 0 ? (
) : ( filteredNotifications.map((notification) => { const unread = !notification.readAt return ( ) }) )}
)}
) }