Replace Web Push and its service worker with page notifications over SSE

This commit is contained in:
Ian Gulliver
2026-10-15 18:34:53 -07:00
parent c7f19f2537
commit a1615693ef
32 changed files with 429 additions and 1175 deletions
+41
View File
@@ -0,0 +1,41 @@
import type { InboxItem, NotificationPrefs } from "./api.js";
import { findTask, focusTask, state } from "./state.js";
function wants(p: NotificationPrefs, item: InboxItem): boolean {
switch (item.kind) {
case "question":
return item.blocking ? p.blocking_questions : p.questions;
case "report":
return p.reports;
case "session_error":
return p.errors;
case "rate_limited":
return p.rate_limits;
}
}
function itemText(item: InboxItem): string {
const p = item.payload;
const text =
item.kind === "question" ? String(p["question"] ?? "")
: item.kind === "report" ? String(p["text"] ?? "")
: item.kind === "session_error" ? `Error: ${String(p["error"] ?? "")}`
: "Rate limited";
const line = text.trim().split("\n")[0] ?? "";
return line.length > 200 ? `${line.slice(0, 199)}…` : line;
}
export function notifyInbox(item: InboxItem): void {
if (!("Notification" in window) || Notification.permission !== "granted" || !state.me || !wants(state.me.notification_prefs, item)) {
return;
}
const title = findTask(item.task_id)?.title;
const n = new Notification(title ? `byre · ${title}` : "byre", { body: itemText(item), tag: item.id });
n.onclick = () => {
window.focus();
if (item.task_id) {
focusTask(item.task_id);
}
n.close();
};
}