From cd8fe6c9ccc2e24958885d5f90f7ee3509fa4c3f Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 17 Oct 2023 19:28:30 -0500 Subject: [PATCH] WIP: service worker --- package.json | 2 +- public/sw.ts | 54 ++++++++++++++++++++++++++++++++ src/components/ResetRouter.tsx | 56 +++++++++++++++++++++++++++++++--- vite.config.ts | 6 +++- 4 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 public/sw.ts diff --git a/package.json b/package.json index 844eedf0..f98fec96 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "@kobalte/tailwindcss": "^0.5.0", "@modular-forms/solid": "^0.18.1", "@mutinywallet/barcode-scanner": "5.0.0-beta.3", - "@mutinywallet/mutiny-wasm": "0.4.23", + "@mutinywallet/mutiny-wasm": "^0.4.23", "@mutinywallet/waila-wasm": "^0.2.1", "@nostr-dev-kit/ndk": "^0.8.11", "@solid-primitives/upload": "^0.0.111", diff --git a/public/sw.ts b/public/sw.ts new file mode 100644 index 00000000..0bef9ed4 --- /dev/null +++ b/public/sw.ts @@ -0,0 +1,54 @@ +// /// +// /// +// +// import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm"; +// +// // SERVICE WORKER SETUP STUFF +// declare let self: ServiceWorkerGlobalScope +// +// const entries = self.__WB_MANIFEST +// +// if (import.meta.env.DEV) +// entries.push({ url: '/', revision: Math.random().toString() }) +// +// // allow only fallback in dev: we don't want to cache anything +// let allowlist: undefined | RegExp[] +// if (import.meta.env.DEV) +// allowlist = [/^\/$/] +// +// // deny api and server page calls +// let denylist: undefined | RegExp[] +// if (import.meta.env.PROD) { +// denylist = [ +// /^\/sw.js$/, +// // exclude webmanifest: has its own cache +// /^\/manifest-(.*).webmanifest$/, +// ] +// } +// +// +// ACTUAL LOGIC +console.log("hello from the service worker?") + +self.addEventListener('push', (event) => { + console.log("push event", event) + const data = event.data.json(); + const options = { + body: data.body, + icon: data.icon, + badge: data.badge + }; + event.waitUntil( + self.registration.showNotification(data.title, options) + ); +}); + + + + + + + + + + diff --git a/src/components/ResetRouter.tsx b/src/components/ResetRouter.tsx index 4428d4fb..a630f0d6 100644 --- a/src/components/ResetRouter.tsx +++ b/src/components/ResetRouter.tsx @@ -1,17 +1,63 @@ import { Button, InnerCard, NiceP, VStack } from "~/components"; import { useI18n } from "~/i18n/context"; import { useMegaStore } from "~/state/megaStore"; +import {MutinyWallet} from "@mutinywallet/mutiny-wasm"; + +// if ('serviceWorker' in navigator) { +// navigator.serviceWorker.register('sw.ts').then(registration => { +// console.log('Service Worker registered with scope:', registration.scope); +// }).catch(error => { +// console.error('Service Worker registration failed:', error); +// }); +// } + +async function subscribeUserToPush() { + console.log("waiting for service worker"); + const registration = await navigator.serviceWorker.ready; + console.log("using push manager"); + const subscription = await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: urlBase64ToUint8Array('BJbNCspGEEdyrj4hI6DD5OBlXpEgVzfaWwZP72p0EiSUTQKXyWauOKGzi-_NWq0dT31s3r5MRPvYVeussdEBygM') + }); + console.log("talking to mutiny notification service"); + try { + // Send the subscription to your server + await MutinyWallet.test_register_web_push("https://auth-staging.mutinywallet.com", "http://localhost:8080", JSON.stringify(subscription)); + console.log("registered") + } catch (e) { + console.error(e) + } +} + +function urlBase64ToUint8Array(base64String) { + const padding = '='.repeat((4 - base64String.length % 4) % 4); + const base64 = (base64String + padding) + .replace(/\-/g, '+') + .replace(/_/g, '/'); + const rawData = window.atob(base64); + const outputArray = new Uint8Array(rawData.length); + for (let i = 0; i < rawData.length; ++i) { + outputArray[i] = rawData.charCodeAt(i); + } + return outputArray; +} + export function ResetRouter() { const i18n = useI18n(); const [state, _] = useMegaStore(); async function reset() { - try { - await state.mutiny_wallet?.reset_router(); - } catch (e) { - console.error(e); - } + // Request notification permission from the user + Notification.requestPermission().then(async permission => { + if (permission === 'granted') { + console.log('Notification permission granted.'); + await subscribeUserToPush(); + console.log('subscribed.'); + } else { + console.error('Notification permission denied.'); + } + }); } return ( diff --git a/vite.config.ts b/vite.config.ts index 053fb2d4..4345a1fe 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -13,9 +13,13 @@ const commitHash = child.execSync("git rev-parse --short HEAD").toString().trim( const pwaOptions: Partial = { base: "/", + injectRegister: 'inline', + filename: 'sw.ts', + strategies: 'injectManifest', registerType: "autoUpdate", devOptions: { - enabled: false + enabled: true, + type: "module" }, includeAssets: ["favicon.ico", "robots.txt"], manifest: manifest