2026-10-11 18:00:37 +08:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
2026-10-20 13:08:13 +08:00
|
|
|
import { NOTIFICATION_OPEN_EVENT, notificationChatUrl, reconcileNotificationPermission, showBrowserNotification, taskNoticeKey } from "@/lib/browser-notifications";
|
2026-10-11 18:00:37 +08:00
|
|
|
import type { NanobotClient } from "@/lib/nanobot-client";
|
|
|
|
|
|
|
|
|
|
export function BrowserNotifications({ client }: { client: Pick<NanobotClient, "onTaskNotice"> }) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
useEffect(() => client.onTaskNotice((notice) => {
|
|
|
|
|
const key = taskNoticeKey(notice);
|
|
|
|
|
if (!key) return;
|
|
|
|
|
const event = notice.event;
|
|
|
|
|
const outcome = event.event === "turn_end"
|
|
|
|
|
? event.outcome === "failed" ? "failed" : event.outcome === "interrupted" ? "interrupted" : "completed"
|
|
|
|
|
: event.status === "failed" ? "failed" : "interrupted";
|
|
|
|
|
// Never expose conversation text, titles, model errors, or paths on a lock screen.
|
|
|
|
|
void showBrowserNotification(t(`settings.notifications.${outcome}`), {
|
|
|
|
|
key, url: notificationChatUrl(event.chat_id, notice.temporary),
|
|
|
|
|
});
|
|
|
|
|
}), [client, t]);
|
2026-10-20 13:08:13 +08:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
2026-10-11 18:00:37 +08:00
|
|
|
|
|
|
|
|
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;
|
2026-10-20 13:08:13 +08:00
|
|
|
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));
|
2026-10-11 18:00:37 +08:00
|
|
|
};
|
|
|
|
|
navigator.serviceWorker.addEventListener("message", navigate);
|
|
|
|
|
return () => navigator.serviceWorker.removeEventListener("message", navigate);
|
|
|
|
|
}, []);
|
|
|
|
|
}
|