import { untrack } from 'svelte'; import { SvelteSet } from 'svelte/reactivity'; import type { Session } from './api'; import { Attention, type AlertKind, type Mail } from './attention'; import { boards } from './boards.svelte'; import { layout } from './layout.svelte'; import { paneTrail } from './tiles/tree'; const KEY = 'albedo.notifications'; class Notifications { enabled = $state(localStorage.getItem(KEY) === 'on'); supported = typeof Notification !== 'undefined'; permission = $state( this.supported ? Notification.permission : 'denied', ); requesting = $state(false); error = $state(''); unread = new SvelteSet(); flashing = new SvelteSet(); #focused = $state(!document.hidden && document.hasFocus()); #attention = new Attention(this.unread, this.flashing); #open = new Map(); #title = document.title; #count = -1; #icon = document.querySelector('link[rel="icon"]'); #originalIcon = this.#icon?.href ?? ''; start() { const focus = () => { this.#focused = !document.hidden && document.hasFocus(); if (this.supported) this.permission = Notification.permission; }; window.addEventListener('focus', focus); window.addEventListener('blur', focus); document.addEventListener('visibilitychange', focus); $effect.root(() => { $effect(() => { const id = this.#reading(); if (id) untrack(() => this.read(id)); }); }); } #quiet(id: string): boolean { if (document.hidden) return false; if (layout.phone) return layout.session === id; if (layout.focus) return layout.focus === id; return !!(boards.active && boards.sessionIds(boards.active).includes(id)); } #reading(): string | null { if (!this.#focused) return null; if (layout.phone) return layout.session; if (layout.focus) return layout.focus; const board = boards.board; const id = board?.selected; const window = id ? board?.windows[id] : undefined; if (!board || !id || window?.type !== 'session') return null; const path = paneTrail(board.root, id); if ( !path || path.some( (tile, at) => tile.kind === 'tabs' && tile.active !== path[at + 1]?.id, ) || (board.expanded && !path.some( (tile) => tile.id === board.expanded || (tile.kind === 'leaf' && tile.window === board.expanded), )) ) return null; return window.data.sessionId; } read(id: string) { this.#attention.read(id); this.#open.get(id)?.close(); this.#open.delete(id); this.#render(); } async toggle() { this.error = ''; if (this.enabled && this.permission === 'granted') { this.enabled = false; for (const notification of this.#open.values()) notification.close(); this.#open.clear(); } else if (this.supported) { this.requesting = true; try { this.permission = await Notification.requestPermission(); this.enabled = this.permission === 'granted'; } catch { this.error = 'the browser could not enable notifications. try again here.'; } finally { this.requesting = false; } } localStorage.setItem(KEY, this.enabled ? 'on' : 'off'); } watched(session: Session) { return ( !session.preferences.archived && (session.preferences.pinned || boards.hasSession(session.id) || layout.focus === session.id || (layout.phone && layout.session === session.id)) ); } changed(previous: Session | undefined, next: Session) { const kind = this.#attention.changed( previous, next, this.watched(next), this.#reading() !== next.id, this.#quiet(next.id), ); if (kind) this.#notify(next, kind); this.#render(); } mail(mail: Mail, sessions: Session[]) { const session = sessions.find( (item) => item.id === mail.receiver_session_id, ); const kind = this.#attention.mail( mail, !!session && this.watched(session), this.#reading() !== mail.receiver_session_id, this.#quiet(mail.receiver_session_id), ); if (kind && session) this.#notify(session, kind); this.#render(); } prune(sessions: Session[]) { this.#attention.prune( new Set( sessions .filter((session) => this.watched(session)) .map((session) => session.id), ), ); this.#render(); } #render() { const count = this.unread.size; if (count === this.#count) return; this.#count = count; document.title = count ? `(${count}) ${this.#title}` : this.#title; if (this.#icon) this.#icon.href = count ? `data:image/svg+xml,${encodeURIComponent('')}` : this.#originalIcon; } #notify(session: Session, kind: AlertKind) { if ( !document.hidden || !this.enabled || !this.supported || Notification.permission !== 'granted' ) return; try { const body = { finished: 'finished a turn', attention: 'needs your attention', mail: 'received agent mail', }[kind]; const notification = new Notification(session.name || 'albedo', { body, tag: `albedo:${session.id}`, }); this.#open.set(session.id, notification); notification.onclose = () => this.#open.delete(session.id); notification.onclick = () => { window.focus(); boards.revealSession(session); notification.close(); }; } catch { this.error = 'this browser could not show a system notification. tab counts still work.'; } } } export const notifications = new Notifications();