okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-022-notice: drop a stale update notice after a version changee59e64ec5

main
2.0 KiB32 linesraw
1import type { UpdateCheckResult } from './types';
2
3/**
4 * The last update check, kept on this computer so Settings can show it without touching the network. A check runs only when the
5 * person presses "Check for updates" (or "Update engine" / "Update Strata"). The result is kept together with the versions it was
6 * for, and it is ignored once the installed version changes (an update, a roll back), so an old "Update available" never lingers.
7 */
8const KEY = 'strata.update-check.v1';
9
10interface Stored { result: UpdateCheckResult; appVersion: string | null; engineVersion: string | null; controlVersion?: string | null }
11export interface InstalledVersions { appVersion?: string | null; engineVersion?: string | null; controlVersion?: string | null }
12
13const same = (a: string | null | undefined, b: string | null | undefined) => (a ?? null) === (b ?? null);
14
15export function readUpdateCache(now: InstalledVersions): UpdateCheckResult | null {
16  try {
17    const stored = JSON.parse(localStorage.getItem(KEY) ?? 'null') as Stored | null;
18    if (!stored?.result?.strata?.status || !stored.result.engine?.status || !stored.result.checkedAt) return null;
19    // Still loading (no app version yet): show nothing rather than a result that may belong to the version before an update.
20    if (!now.appVersion) return null;
21    if (!same(stored.appVersion, now.appVersion) || !same(stored.engineVersion, now.engineVersion) || !same(stored.controlVersion, now.controlVersion)) return null;
22    return stored.result;
23  } catch { return null; }
24}
25
26export function saveUpdateCache(result: UpdateCheckResult, now: InstalledVersions) {
27  try { localStorage.setItem(KEY, JSON.stringify({ result, appVersion: now.appVersion ?? null, engineVersion: now.engineVersion ?? null, controlVersion: now.controlVersion ?? null } satisfies Stored)); } catch { /* Not remembering a result is harmless. */ }
28}
29
30export function clearUpdateCache() {
31  try { localStorage.removeItem(KEY); } catch { /* Nothing to clear. */ }
32}