diff --git a/src/lib/stores/offlineSync.ts b/src/lib/stores/offlineSync.ts index 271a3b9..1ff4f09 100644 --- a/src/lib/stores/offlineSync.ts +++ b/src/lib/stores/offlineSync.ts @@ -265,3 +265,27 @@ export function startOfflineSync(getUserId: () => string | null): () => void { window.removeEventListener('online', schedule); }; } + +/** Read details only from the snapshot currently claimed by this account. */ +export async function readOfflineEntry(userId: string, pokedexId: string, entryId: string) { + if (typeof window === 'undefined' || !('caches' in window)) return null; + const meta = (await readOfflineMeta()) as (OfflineMeta & { dataCache?: string }) | null; + if ( + meta?.userId !== userId || + meta.format !== OFFLINE_META_FORMAT || + !meta.dataCache?.startsWith(`${OFFLINE_CACHE_PREFIX}data-v1-${userId}-`) + ) + return null; + const response = await ( + await caches.open(meta.dataCache) + ).match(`/__offline/snapshot/${encodeURIComponent(userId)}`); + const snapshot = (await response?.json()) as OfflineSnapshot | undefined; + const current = await readOfflineMeta(); + if (current?.userId !== userId || snapshot?.userId !== userId || snapshot.version !== 1) + return null; + return ( + snapshot.pokedexes + .find((dex) => dex.pokedex._id === pokedexId) + ?.entries.find((row) => row.pokedexEntry._id === entryId) ?? null + ); +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d4217fa..e916cc7 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -52,6 +52,7 @@ if (navigator.onLine) return; const target = event.target instanceof Element ? event.target : null; if (!target?.closest('button, input, textarea, select, form')) return; + if (target.closest('[data-offline-action]')) return; event.preventDefault(); event.stopImmediatePropagation(); }; @@ -313,10 +314,10 @@