Push notifications not supported in this browser.
; + } + + async function subscribe() { + try { + const registration = await navigator.serviceWorker.ready; + const sub = await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: urlBase64ToUint8Array(VAPID_KEY), + }); + const json = sub.toJSON() as { endpoint: string; keys: { p256dh: string; auth: string } }; + startTransition(async () => { + await subscribeToPush(json, navigator.userAgent); + setEndpoint(json.endpoint); + setStatus("subscribed"); + }); + } catch { + setStatus("denied"); + } + } + + async function unsubscribe() { + if (!endpoint) return; + const registration = await navigator.serviceWorker.ready; + const sub = await registration.pushManager.getSubscription(); + if (sub) await sub.unsubscribe(); + startTransition(async () => { + await unsubscribeFromPush(endpoint); + setEndpoint(null); + setStatus("idle"); + }); + } + + function sendTest() { + startTransition(async () => { + await sendTestNotification(); + setTestSent(true); + setTimeout(() => setTestSent(false), 3000); + }); + } + + if (status === "denied") { + returnNotification permission denied. Enable it in browser settings.
; + } + + if (status === "subscribed") { + return ( +