37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
import { findTask, focusTask, state } from "./state.js";
|
|||
|
|
function wants(p, item) {
|
||
|
|
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) {
|
||
|
|
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) {
|
||
|
|
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();
|
||
|
|
};
|
||
|
|
}
|
||
|
|
//# sourceMappingURL=notifications.js.map
|