'use client' import { useEffect, useState } from 'react' import NotificationDropdown from './NotificationDropdown' import { useWS } from '@/app/lib/wsStore' import { useSession } from 'next-auth/react' import { useTeamManager } from '../hooks/useTeamManager' import { useRouter } from 'next/navigation' type Notification = { id: string text: string read: boolean actionType?: string actionData?: string createdAt?: string } export default function NotificationCenter() { const { data: session } = useSession() const [notifications, setNotifications] = useState([]) const [open, setOpen] = useState(false) const { socket, connect } = useWS() const { markAllAsRead, markOneAsRead, handleInviteAction } = useTeamManager({}, null) const router = useRouter() const [previewText, setPreviewText] = useState(null) const [showPreview, setShowPreview] = useState(false) const [animateBell, setAnimateBell] = useState(false) useEffect(() => { const steamId = session?.user?.steamId if (!steamId) return const loadNotifications = async () => { try { const res = await fetch('/api/notifications/user') if (!res.ok) throw new Error('Fehler beim Laden') const data = await res.json() const loaded = data.notifications.map((n: any) => ({ id: n.id, text: n.message, read: n.read, actionType: n.actionType, actionData: n.actionData, createdAt: n.createdAt, })) setNotifications(loaded) } catch (err) { console.error('[NotificationCenter] Fehler beim Laden:', err) } } loadNotifications() connect(steamId) }, [session?.user?.steamId, connect]) useEffect(() => { if (!socket) return const handleMessage = (event: MessageEvent) => { const data = JSON.parse(event.data) if (data.type === 'heartbeat') return const isNotificationType = [ 'notification', 'invitation', 'team-invite', 'team-joined', 'team-member-joined', 'team-kick', 'team-kick-other', 'team-left', 'team-member-left', 'team-leader-changed', 'team-join-request' ].includes(data.type) if (!isNotificationType) return const newNotification: Notification = { id: data.id, text: data.message || 'Neue Benachrichtigung', read: false, actionType: data.actionType, actionData: data.actionData, createdAt: data.createdAt, } setNotifications(prev => [newNotification, ...prev]) setPreviewText(newNotification.text) setShowPreview(true) setAnimateBell(true) setTimeout(() => { setShowPreview(false) setTimeout(() => { setPreviewText(null) }, 300) setAnimateBell(false) }, 3000) } socket.addEventListener('message', handleMessage) return () => socket.removeEventListener('message', handleMessage) }, [socket]) return (
{/* Dropdown */} {open && ( { await markAllAsRead() setNotifications(prev => prev.map(n => ({ ...n, read: true }))) }} onSingleRead={async (id) => { await markOneAsRead(id) setNotifications(prev => prev.map(n => (n.id === id ? { ...n, read: true } : n))) }} onClose={() => setOpen(false)} onAction={async (action, id) => { await handleInviteAction(action, id) setNotifications(prev => prev.map(n => n.actionData === id ? { ...n, read: true, actionType: undefined, actionData: undefined } : n ) ) if (action === 'accept') router.refresh() }} /> )}
) }