fix(webui): refine browser notifications (NAN-227)
Reconcile the original notification fix with the integrated main snapshot. Group notification settings and simplify localized copy and documentation. Preserve permission opt-out and notification click routing across app transitions. Suppress late completion notifications for discarded temporary chats and align notification settings skeletons with the loaded layout.
This commit is contained in:
1 parent
7b2af031c0
commit
11152626f8
34 files changed
+728
-289
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { readLocalPreferences } from "./local-preferences";
|
||||
import { readLocalPreferences, writeLocalPreferences } from "./local-preferences";
|
||||
import type { TaskNotice } from "./nanobot-client";
|
||||
|
||||
export type NotificationProblem = "insecure" | "unsupported" | "denied" | "dismissed" | "permissionFailed" | "workerUnavailable" | "displayFailed";
|
||||
export type NotificationProblem = "insecure" | "unsupported" | "denied" | "revoked" | "dismissed" | "permissionFailed" | "workerUnavailable" | "displayFailed";
|
||||
export type NotificationResult = "submitted" | "skipped" | NotificationProblem;
|
||||
const TIMEOUT_MS = 5_000;
|
||||
export const NOTIFICATION_STATUS_EVENT = "nanobot:notification-status";
|
||||
@@ -12,11 +12,20 @@ export function lastNotificationProblem() { return lastProblem; }
|
||||
// Do not infer browser support from its user agent (including embedded browsers).
|
||||
export function notificationProblem(): NotificationProblem | null {
|
||||
if (!window.isSecureContext) return "insecure";
|
||||
if (typeof Notification === "undefined" || !("serviceWorker" in navigator)) return "unsupported";
|
||||
if (typeof Notification === "undefined") return "unsupported";
|
||||
if (Notification.permission === "denied") return "denied";
|
||||
if (!("serviceWorker" in navigator)) return "workerUnavailable";
|
||||
return null;
|
||||
}
|
||||
|
||||
export function reconcileNotificationPermission(): void {
|
||||
const preferences = readLocalPreferences();
|
||||
if (!preferences.browserNotifications || (typeof Notification !== "undefined" && Notification.permission === "granted")) return;
|
||||
writeLocalPreferences({ ...preferences, browserNotifications: false });
|
||||
lastProblem = typeof Notification === "undefined" ? "unsupported" : "revoked";
|
||||
window.dispatchEvent(new Event(NOTIFICATION_STATUS_EVENT));
|
||||
}
|
||||
|
||||
function bounded<T>(operation: Promise<T>, timeoutMs = TIMEOUT_MS): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = window.setTimeout(() => reject(new Error("Notification timeout")), timeoutMs);
|
||||
@@ -31,6 +40,24 @@ async function activeWorker(): Promise<ServiceWorker | null> {
|
||||
return registration?.active ?? null;
|
||||
}
|
||||
|
||||
async function workerRequest(worker: ServiceWorker, message: object): Promise<unknown> {
|
||||
const channel = new MessageChannel();
|
||||
try {
|
||||
return await bounded(new Promise<unknown>((resolve) => {
|
||||
channel.port1.onmessage = ({ data }: MessageEvent<unknown>) => resolve(data);
|
||||
worker.postMessage(message, [channel.port2]);
|
||||
}));
|
||||
} finally { channel.port1.close(); }
|
||||
}
|
||||
|
||||
export async function notificationWorkerProblem(): Promise<"workerUnavailable" | null> {
|
||||
try {
|
||||
const worker = await activeWorker();
|
||||
return worker && await workerRequest(worker, { type: "nanobot:notification-probe" }) === "ready"
|
||||
? null : "workerUnavailable";
|
||||
} catch { return "workerUnavailable"; }
|
||||
}
|
||||
|
||||
export async function enableBrowserNotifications(): Promise<NotificationProblem | null> {
|
||||
const problem = notificationProblem();
|
||||
if (problem) return problem;
|
||||
@@ -40,8 +67,9 @@ export async function enableBrowserNotifications(): Promise<NotificationProblem
|
||||
? "granted" : await bounded(Notification.requestPermission(), 60_000);
|
||||
if (permission !== "granted") return permission === "denied" ? "denied" : "dismissed";
|
||||
} catch { return "permissionFailed"; }
|
||||
try { return await activeWorker() ? null : "workerUnavailable"; }
|
||||
catch { return "workerUnavailable"; }
|
||||
const workerProblem = await notificationWorkerProblem();
|
||||
if (!workerProblem) lastProblem = null;
|
||||
return workerProblem;
|
||||
}
|
||||
|
||||
export function taskNoticeKey(notice: TaskNotice): string | null {
|
||||
@@ -74,6 +102,7 @@ export async function showBrowserNotification(body: string, options: {
|
||||
}
|
||||
|
||||
async function submitNotification(body: string, options: { key: string; url: string; test?: boolean }): Promise<NotificationResult> {
|
||||
reconcileNotificationPermission();
|
||||
const eligible = () => options.test || (readLocalPreferences().browserNotifications && document.visibilityState !== "visible");
|
||||
if (!eligible()) return "skipped";
|
||||
const problem = notificationProblem();
|
||||
@@ -84,14 +113,8 @@ async function submitNotification(body: string, options: { key: string; url: str
|
||||
catch { return "workerUnavailable"; }
|
||||
if (!worker) return "workerUnavailable";
|
||||
if (!eligible()) return "skipped";
|
||||
const channel = new MessageChannel();
|
||||
try {
|
||||
return await bounded(new Promise<NotificationResult>((resolve) => {
|
||||
channel.port1.onmessage = ({ data }: MessageEvent<unknown>) => {
|
||||
resolve(data === "submitted" || data === "skipped" ? data : "displayFailed");
|
||||
};
|
||||
worker.postMessage({ type: "nanobot:notify", body, ...options }, [channel.port2]);
|
||||
}));
|
||||
} catch { return "displayFailed"; }
|
||||
finally { channel.port1.close(); }
|
||||
const data = await workerRequest(worker, { type: "nanobot:notify", body, ...options });
|
||||
return data === "submitted" || data === "skipped" ? data : "displayFailed";
|
||||
} catch { return "workerUnavailable"; }
|
||||
}
|
||||
Reference in new issue
Block a user