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,6 +1,6 @@
|
||||
import { useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { NOTIFICATION_OPEN_EVENT, notificationChatUrl, showBrowserNotification, taskNoticeKey } from "@/lib/browser-notifications";
|
||||
import { NOTIFICATION_OPEN_EVENT, notificationChatUrl, reconcileNotificationPermission, showBrowserNotification, taskNoticeKey } from "@/lib/browser-notifications";
|
||||
import type { NanobotClient } from "@/lib/nanobot-client";
|
||||
|
||||
export function BrowserNotifications({ client }: { client: Pick<NanobotClient, "onTaskNotice"> }) {
|
||||
@@ -17,20 +17,36 @@ export function BrowserNotifications({ client }: { client: Pick<NanobotClient, "
|
||||
key, url: notificationChatUrl(event.chat_id, notice.temporary),
|
||||
});
|
||||
}), [client, t]);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Permission and click ownership survives authentication and route transitions.
|
||||
export function useBrowserNotificationLifecycle() {
|
||||
useEffect(() => {
|
||||
reconcileNotificationPermission();
|
||||
window.addEventListener("focus", reconcileNotificationPermission);
|
||||
document.addEventListener("visibilitychange", reconcileNotificationPermission);
|
||||
return () => {
|
||||
window.removeEventListener("focus", reconcileNotificationPermission);
|
||||
document.removeEventListener("visibilitychange", reconcileNotificationPermission);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!("serviceWorker" in navigator)) return;
|
||||
const navigate = (event: MessageEvent<unknown>) => {
|
||||
const data = event.data as { type?: string; url?: unknown } | null;
|
||||
if (event.source !== navigator.serviceWorker.controller || data?.type !== "nanobot:notification-click" || typeof data.url !== "string") return;
|
||||
const url = new URL(data.url);
|
||||
if (url.origin === window.location.origin) {
|
||||
window.location.hash = url.hash;
|
||||
window.dispatchEvent(new Event(NOTIFICATION_OPEN_EVENT));
|
||||
}
|
||||
let url: URL;
|
||||
try { url = new URL(data.url); } catch { return; }
|
||||
if (url.origin !== window.location.origin || url.username || url.password || url.search || url.pathname !== "/") return;
|
||||
if (!/^#\/(?:chat|temporary)\/[^?]+$/.test(url.hash)
|
||||
&& url.hash !== "#/settings?section=notifications"
|
||||
&& url.hash !== "#/settings?section=appearance") return;
|
||||
window.location.hash = url.hash;
|
||||
window.dispatchEvent(new Event(NOTIFICATION_OPEN_EVENT));
|
||||
};
|
||||
navigator.serviceWorker.addEventListener("message", navigate);
|
||||
return () => navigator.serviceWorker.removeEventListener("message", navigate);
|
||||
}, []);
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user