diff options
| author | Adam Soltys <[email protected]> | 2026-03-06 22:47:13 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-03-06 22:47:13 -0800 |
| commit | deba11267523c29237bca31d1deb5a0e6b659810 (patch) | |
| tree | f46d9855d4e2eb970e8ee8dacc65bac6f8678abc | |
| parent | 884df7678db775b913aefb0441a7c14d4e10f51a (diff) | |
| download | coinos-ui-deba11267523c29237bca31d1deb5a0e6b659810.tar.gz coinos-ui-deba11267523c29237bca31d1deb5a0e6b659810.zip | |
fix push notification registration and server-side MLS user index
- Fix push toggle: use $effect instead of run() to avoid silent failures
- Add error handling and console logging for push init/toggle
- Hide push section when service worker unavailable (dev mode)
- Simplify MLS user search to fetch from server API and cache in IndexedDB
Co-Authored-By: Claude Opus 4.6 <[email protected]>
| -rw-r--r-- | src/routes/(app)/settings/account/+page.svelte | 76 |
1 files changed, 50 insertions, 26 deletions
diff --git a/src/routes/(app)/settings/account/+page.svelte b/src/routes/(app)/settings/account/+page.svelte index 115f46c5..dd602c1a 100644 --- a/src/routes/(app)/settings/account/+page.svelte +++ b/src/routes/(app)/settings/account/+page.svelte @@ -26,46 +26,68 @@ pm: any, subscription: any; + let permission = $state(); + let pmReady = $state(false); + onMount(async () => { if (!browser) return; - pm = navigator?.serviceWorker && (await navigator.serviceWorker.getRegistration())?.pushManager; + try { + pm = navigator?.serviceWorker && (await navigator.serviceWorker.getRegistration())?.pushManager; + if (!pm) { pmReady = true; return; } - permission = await pm.permissionState({ - userVisibleOnly: true, - applicationServerKey: PUBLIC_VAPID_PUBKEY, - }); + permission = await pm.permissionState({ + userVisibleOnly: true, + applicationServerKey: PUBLIC_VAPID_PUBKEY, + }); - if (permission === "granted") { - subscription = await pm.getSubscription(); - if (subscriptions.includes(JSON.stringify(subscription))) push = true; + if (permission === "granted") { + subscription = await pm.getSubscription(); + if (subscriptions.includes(JSON.stringify(subscription))) push = true; + } + } catch (e) { + console.error("push init failed", e); } + + pmReady = true; }); - let permission = $state(); - let updateNotifications = async (push) => { - if (!browser || !pm) return (push = false); + const togglePush = async (on: boolean) => { + if (!pm) return; + try { + if (subscription && !on) { + await post("/subscription/delete", { subscription }); + return; + } - if (subscription && !push) { - return post("/subscription/delete", { subscription }); - } - - if (push && permission !== "denied") { - subscription = await pm.subscribe({ - userVisibleOnly: true, - applicationServerKey: PUBLIC_VAPID_PUBKEY, - }); - } + if (on && permission !== "denied") { + subscription = await pm.subscribe({ + userVisibleOnly: true, + applicationServerKey: PUBLIC_VAPID_PUBKEY, + }); + } - if (subscription) { - await post("/subscription", { subscription }); + if (subscription) { + await post("/subscription", { subscription }); + } + } catch (e) { + console.error("push toggle failed", e); + push = false; } }; - run(() => { - user.language = $locale; + + let pushInitialized = false; + $effect(() => { + // Track push so this effect re-runs when it changes + const val = push; + if (!pmReady) return; + // Skip the first run after pmReady (initial state from onMount) + if (!pushInitialized) { pushInitialized = true; return; } + togglePush(val); }); + run(() => { - updateNotifications(push); + user.language = $locale; }); $effect(() => { @@ -111,6 +133,7 @@ <p class="text-secondary mt-1 w-9/12">{$t("user.settings.notificationsDesc")}</p> </div> +{#if pm} <div> <div class="flex justify-between items-center"> <span class="font-bold">{$t("user.settings.pushNotifications")}</span> @@ -126,6 +149,7 @@ {/if} </p> </div> +{/if} <div> <div class="flex justify-between items-center"> |
