/**
* PersonaFlow - Notification System
* In-app bell icon with real-time polling for shared agent activity
*/
const Notifications = {
items: [],
readIds: new Set(),
pollInterval: null,
POLL_MS: 30000, // 30 seconds
init() {
// Load read notification IDs from localStorage
try {
const stored = localStorage.getItem('pf-notif-read');
this.readIds = new Set(JSON.parse(stored || '[]'));
} catch (e) {
this.readIds = new Set();
}
this.injectBell();
this.poll();
this.pollInterval = setInterval(() => this.poll(), this.POLL_MS);
},
stop() {
if (this.pollInterval) {
clearInterval(this.pollInterval);
this.pollInterval = null;
}
},
injectBell() {
// Insert bell button into the sidebar header
const sidebarHeader = document.querySelector('.sidebar-header');
if (!sidebarHeader || document.getElementById('notif-bell')) return;
const bellWrapper = document.createElement('div');
bellWrapper.id = 'notif-bell-wrapper';
bellWrapper.style.cssText = 'position:relative;cursor:pointer;margin-left:auto;';
bellWrapper.innerHTML = `
`;
sidebarHeader.appendChild(bellWrapper);
// ─────────────────────────────────────────────────────────────────
// IMPORTANT: attach the panel to
so it is NEVER clipped by
// the sidebar's overflow or width constraints. We position it
// dynamically via getBoundingClientRect() each time it opens.
// ─────────────────────────────────────────────────────────────────
const panel = document.createElement('div');
panel.id = 'notif-panel';
panel.style.cssText = `
display: none;
position: fixed;
width: 320px;
background: var(--surface, #ffffff);
border: 1px solid var(--gray-200, #e5e7eb);
border-radius: 14px;
box-shadow: 0 12px 48px rgba(0,0,0,0.18);
z-index: 99999;
overflow: hidden;
`;
panel.innerHTML = `
Notifications
No notifications yet
`;
document.body.appendChild(panel);
// Close panel when clicking outside both the bell and the panel
document.addEventListener('click', (e) => {
const wrapper = document.getElementById('notif-bell-wrapper');
if (!wrapper?.contains(e.target) && !panel.contains(e.target)) {
panel.style.display = 'none';
}
});
},
async poll() {
try {
const data = await API.get('/notifications');
this.items = data.notifications || [];
this.renderBadge();
} catch (e) {
// Silently ignore polling failures
}
},
renderBadge() {
const unread = this.items.filter(n => !this.readIds.has(n.id));
const badge = document.getElementById('notif-badge');
if (!badge) return;
if (unread.length > 0) {
badge.style.display = 'flex';
badge.textContent = unread.length > 9 ? '9+' : unread.length;
} else {
badge.style.display = 'none';
}
},
togglePanel() {
const panel = document.getElementById('notif-panel');
const bell = document.getElementById('notif-bell');
if (!panel || !bell) return;
const isOpen = panel.style.display === 'block';
if (isOpen) {
panel.style.display = 'none';
return;
}
// ── Position the panel relative to the bell button's screen rect ──
const rect = bell.getBoundingClientRect();
const panelWidth = 320;
const margin = 8; // gap between bell and panel top edge
// Place the panel just below the bell
let top = rect.bottom + margin;
// Align left edge of panel with left edge of bell (then clamp to viewport)
let left = rect.left;
// If it would overflow the right edge of the viewport, shift left
if (left + panelWidth > window.innerWidth - 12) {
left = window.innerWidth - panelWidth - 12;
}
// Never go off-screen left
if (left < 8) left = 8;
// If it would overflow the bottom, open upward instead
const panelMaxHeight = 420; // header + list max-height
if (top + panelMaxHeight > window.innerHeight) {
top = rect.top - panelMaxHeight - margin;
}
panel.style.top = `${top}px`;
panel.style.left = `${left}px`;
panel.style.display = 'block';
this.renderPanel();
},
renderPanel() {
const list = document.getElementById('notif-list');
if (!list) return;
if (this.items.length === 0) {
list.innerHTML = `