/* BAN YARO — Notification Center */ window.Page_notifications = (() => { // ---------------------------------------------------------- // Hilfsfunktionen // ---------------------------------------------------------- /** Relativer Zeitstempel: "vor 2h", "vor 3 Tagen", etc. */ function _relTime(isoStr) { if (!isoStr) return ''; const diff = Date.now() - new Date(isoStr + (isoStr.includes('Z') ? '' : 'Z')).getTime(); const min = Math.floor(diff / 60000); if (min < 1) return 'gerade eben'; if (min < 60) return `vor ${min} Min.`; const h = Math.floor(min / 60); if (h < 24) return `vor ${h}h`; const d = Math.floor(h / 24); if (d < 30) return `vor ${d} Tag${d !== 1 ? 'en' : ''}`; const mo = Math.floor(d / 30); return `vor ${mo} Monat${mo !== 1 ? 'en' : ''}`; } /** Phosphor-Icon-Name je nach Notification-Typ */ function _iconForType(type) { switch (type) { case 'chat_message': return 'chat-circle'; case 'friend_request': return 'user-plus'; case 'health_reminder': return 'first-aid'; case 'milestone': return 'star'; case 'poison_alert': return 'warning'; default: return 'bell'; } } /** Parst n.data sicher — egal ob String oder Objekt */ function _parseData(raw) { if (!raw) return {}; if (typeof raw === 'object') return raw; try { return JSON.parse(raw); } catch (_) { return {}; } } /** Ermittelt Ziel-Seite und optionale Label für den Toast */ function _navTarget(n) { const d = _parseData(n.data); // Explizit gesetzte Zielseite hat Vorrang if (d.page) return { page: d.page, label: d.page }; // Typ-basiertes Fallback switch (n.type) { case 'chat_message': return { page: d.conversation_id ? `chat?id=${d.conversation_id}` : 'chat', label: 'Chat' }; case 'friend_request': return { page: 'friends', label: 'Freunde' }; case 'milestone': return { page: 'diary', label: 'Tagebuch' }; case 'poison_alert': return { page: 'map', label: 'Karte' }; default: return { page: '', label: '' }; } } /** Rendert eine einzelne Notification als HTML-String */ function _renderItem(n) { const unread = !n.read_at; const iconName = unread ? _iconForType(n.type) : 'bell'; const cls = ['notif-item', unread ? 'notif-unread' : ''].filter(Boolean).join(' '); const nav = _navTarget(n); return `
Keine Benachrichtigungen
Keine Benachrichtigungen