Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit 19d1752

Browse files
committed
WIP: service worker
1 parent 57df13a commit 19d1752

File tree

7 files changed

+111
-13
lines changed

7 files changed

+111
-13
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
"@capacitor/clipboard": "^5.0.6",
4949
"@capacitor/core": "^5.2.2",
5050
"@capacitor/filesystem": "^5.1.4",
51-
"@capacitor/share": "^5.0.6",
5251
"@capacitor/haptics": "^5.0.6",
52+
"@capacitor/share": "^5.0.6",
5353
"@capacitor/toast": "^5.0.6",
5454
"@kobalte/core": "^0.9.8",
5555
"@kobalte/tailwindcss": "^0.5.0",
5656
"@modular-forms/solid": "^0.18.1",
5757
"@mutinywallet/barcode-scanner": "5.0.0-beta.3",
58-
"@mutinywallet/mutiny-wasm": "0.4.25",
58+
"@mutinywallet/mutiny-wasm": "^0.4.25",
5959
"@mutinywallet/waila-wasm": "^0.2.1",
6060
"@nostr-dev-kit/ndk": "^0.8.11",
6161
"@solid-primitives/upload": "^0.0.111",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/sw.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// /// <reference lib="WebWorker" />
2+
// /// <reference types="vite/client" />
3+
//
4+
// import initMutinyWallet, { MutinyWallet } from "@mutinywallet/mutiny-wasm";
5+
//
6+
// // SERVICE WORKER SETUP STUFF
7+
// declare let self: ServiceWorkerGlobalScope
8+
//
9+
// const entries = self.__WB_MANIFEST
10+
//
11+
// if (import.meta.env.DEV)
12+
// entries.push({ url: '/', revision: Math.random().toString() })
13+
//
14+
// // allow only fallback in dev: we don't want to cache anything
15+
// let allowlist: undefined | RegExp[]
16+
// if (import.meta.env.DEV)
17+
// allowlist = [/^\/$/]
18+
//
19+
// // deny api and server page calls
20+
// let denylist: undefined | RegExp[]
21+
// if (import.meta.env.PROD) {
22+
// denylist = [
23+
// /^\/sw.js$/,
24+
// // exclude webmanifest: has its own cache
25+
// /^\/manifest-(.*).webmanifest$/,
26+
// ]
27+
// }
28+
//
29+
//
30+
// ACTUAL LOGIC
31+
console.log("hello from the service worker?")
32+
33+
self.addEventListener('push', (event) => {
34+
console.log("push event", event)
35+
if (!(self.Notification && self.Notification.permission === "granted")) {
36+
return;
37+
}
38+
39+
const data = event.data?.json() ?? {};
40+
const options = {
41+
body: data.body,
42+
icon: data.icon,
43+
badge: data.badge
44+
};
45+
event.waitUntil(
46+
self.registration.showNotification(data.title, options)
47+
);
48+
});

src/components/ResetRouter.tsx

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,59 @@
11
import { Button, InnerCard, NiceP, VStack } from "~/components";
22
import { useI18n } from "~/i18n/context";
33
import { useMegaStore } from "~/state/megaStore";
4+
import {MutinyWallet} from "@mutinywallet/mutiny-wasm";
5+
6+
async function subscribeUserToPush(mutinyWallet: MutinyWallet) {
7+
console.log("waiting for service worker");
8+
const registration = await navigator.serviceWorker.ready;
9+
console.log("using push manager");
10+
const subscription = await registration.pushManager.subscribe({
11+
userVisibleOnly: true,
12+
applicationServerKey: urlBase64ToUint8Array('BJbNCspGEEdyrj4hI6DD5OBlXpEgVzfaWwZP72p0EiSUTQKXyWauOKGzi-_NWq0dT31s3r5MRPvYVeussdEBygM')
13+
});
14+
console.log("talking to mutiny notification service");
15+
try {
16+
// Send the subscription to your server
17+
await mutinyWallet.register_web_push(subscription);
18+
console.log("registered")
19+
} catch (e) {
20+
console.error(e)
21+
}
22+
}
23+
24+
function urlBase64ToUint8Array(base64String) {
25+
const padding = '='.repeat((4 - base64String.length % 4) % 4);
26+
const base64 = (base64String + padding)
27+
.replace(/\-/g, '+')
28+
.replace(/_/g, '/');
29+
const rawData = window.atob(base64);
30+
const outputArray = new Uint8Array(rawData.length);
31+
for (let i = 0; i < rawData.length; ++i) {
32+
outputArray[i] = rawData.charCodeAt(i);
33+
}
34+
return outputArray;
35+
}
36+
437

538
export function ResetRouter() {
639
const i18n = useI18n();
740
const [state, _] = useMegaStore();
841

942
async function reset() {
10-
try {
11-
await state.mutiny_wallet?.reset_router();
12-
} catch (e) {
13-
console.error(e);
14-
}
43+
// Request notification permission from the user
44+
Notification.requestPermission().then(async permission => {
45+
if (permission === 'granted') {
46+
console.log('Notification permission granted.');
47+
if (state.mutiny_wallet) {
48+
await subscribeUserToPush(state.mutiny_wallet);
49+
console.log('subscribed.');
50+
} else {
51+
console.error('no mutiny wallet');
52+
}
53+
} else {
54+
console.error('Notification permission denied.');
55+
}
56+
});
1557
}
1658

1759
return (

src/logic/mutinyWalletSetup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export async function setupMutinyWallet(
235235
subscriptions,
236236
storage,
237237
scorer,
238+
"http://127.0.0.1:8080",
238239
// Do not connect peers
239240
undefined,
240241
// Do not skip device lock

src/routes/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import {
2323
import { useI18n } from "~/i18n/context";
2424
import { FeedbackLink } from "~/routes/Feedback";
2525
import { useMegaStore } from "~/state/megaStore";
26+
import { registerSW } from 'virtual:pwa-register';
2627

2728
export default function App() {
29+
registerSW({ immediate: true });
30+
2831
const i18n = useI18n();
2932
const [state, _actions] = useMegaStore();
3033

vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ const commitHash = process.env.VITE_COMMIT_HASH ?? child.execSync("git rev-parse
1313

1414
const pwaOptions: Partial<VitePWAOptions> = {
1515
base: "/",
16+
injectRegister: 'inline',
17+
filename: 'sw.ts',
18+
strategies: 'injectManifest',
1619
registerType: "autoUpdate",
1720
devOptions: {
18-
enabled: false
21+
enabled: true,
22+
type: "module"
1923
},
2024
includeAssets: ["favicon.ico", "robots.txt"],
2125
manifest: manifest

0 commit comments

Comments
 (0)