Replace Web Push and its service worker with page notifications over SSE
This commit is contained in:
@@ -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();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user