From b124e1dcaeb35ab1dcd2a298ebab8fab31f2365f Mon Sep 17 00:00:00 2001 From: WhyAsh5114 Date: Tue, 24 Dec 2024 15:02:37 +0530 Subject: [PATCH] feat: added update data loss warning --- .../layout/ChangelogDialog.svelte | 37 +++++++++++++------ .../(components)/layout/PWAButtons.svelte | 37 ++----------------- .../layout/PWAFunctions.svelte.ts | 33 +++++++++++++++++ .../layout/UpdateDataLossDialog.svelte | 27 ++++++++++++++ .../(components)/layout/UserDropdown.svelte | 1 - src/routes/+layout.svelte | 15 +++++--- src/routes/changelog/+page.server.ts | 3 -- 7 files changed, 99 insertions(+), 54 deletions(-) create mode 100644 src/routes/(components)/layout/PWAFunctions.svelte.ts create mode 100644 src/routes/(components)/layout/UpdateDataLossDialog.svelte diff --git a/src/routes/(components)/layout/ChangelogDialog.svelte b/src/routes/(components)/layout/ChangelogDialog.svelte index 1e8a7455..21f63154 100644 --- a/src/routes/(components)/layout/ChangelogDialog.svelte +++ b/src/routes/(components)/layout/ChangelogDialog.svelte @@ -1,38 +1,53 @@ - + {#if dialogText}
{@html dialogText}
+ {:else}
diff --git a/src/routes/(components)/layout/PWAButtons.svelte b/src/routes/(components)/layout/PWAButtons.svelte index ab8ebe3e..26dcfbdd 100644 --- a/src/routes/(components)/layout/PWAButtons.svelte +++ b/src/routes/(components)/layout/PWAButtons.svelte @@ -1,20 +1,16 @@ diff --git a/src/routes/(components)/layout/PWAFunctions.svelte.ts b/src/routes/(components)/layout/PWAFunctions.svelte.ts new file mode 100644 index 00000000..78e3d2a1 --- /dev/null +++ b/src/routes/(components)/layout/PWAFunctions.svelte.ts @@ -0,0 +1,33 @@ +import { browser } from '$app/environment'; +import { useRegisterSW } from 'virtual:pwa-register/svelte'; + +const sw = browser + ? useRegisterSW({ + onRegisteredSW(swUrl, r) { + if (r) { + setInterval(async () => { + if (!navigator || r.installing) return; + if ('connection' in navigator && !navigator.onLine) return; + + const resp = await fetch(swUrl, { + cache: 'no-store', + headers: { + cache: 'no-store', + 'cache-control': 'no-cache' + } + }); + if (resp.status === 200) await r.update(); + }, 3600000); + } + console.log(`SW Registered: ${r}`); + }, + onRegisterError(error) { + console.log('SW registration error', error); + } + }) + : null; + +export const needRefresh = sw?.needRefresh; +export const updateServiceWorker = sw?.updateServiceWorker; +export const offlineReady = sw?.offlineReady; +export const updateDataLossDialog = $state({ open: false }); diff --git a/src/routes/(components)/layout/UpdateDataLossDialog.svelte b/src/routes/(components)/layout/UpdateDataLossDialog.svelte new file mode 100644 index 00000000..fa5bcd97 --- /dev/null +++ b/src/routes/(components)/layout/UpdateDataLossDialog.svelte @@ -0,0 +1,27 @@ + + + + {#snippet description()} +

Any unsaved data, like a workout in progress, or an unsaved mesocycle will be lost.

+ {/snippet} + +
diff --git a/src/routes/(components)/layout/UserDropdown.svelte b/src/routes/(components)/layout/UserDropdown.svelte index c3b36bfb..eee3a323 100644 --- a/src/routes/(components)/layout/UserDropdown.svelte +++ b/src/routes/(components)/layout/UserDropdown.svelte @@ -8,7 +8,6 @@ function logOut() { signOut(); localStorage.clear(); - localStorage.setItem('changelogShown', 'true'); } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d6e4f483..e51e02a1 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,14 +1,15 @@