aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/routes/(app)/settings/account/+page.svelte76
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">