mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
feat(settings): keep the account's settings for a start without a connection
/api/settings is never cached, so an app started offline ran on DEFAULT_SETTINGS until the server answered: Leaflet instead of the map the user chose, and the default units. The answer the server gave last time now stands in until the next one arrives. - Dexie v18 adds accountSettings, one row in the signed-in user's own database, so another account on the device never reads it and logout deletes it with the database. The anonymous database never holds one. A new table only; the upgrade test covers it. - loadSettings lays the kept answer over the defaults while nothing newer arrived and keeps every server answer. A key the server then leaves out goes back to its default, so the server stays the source of truth. - A setting the server accepted is laid over the kept answer, so a change made online is what the next offline start shows. A refused save keeps nothing. Keeping is best effort: where IndexedDB is refused the app starts on the defaults as before.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* The account's settings kept for a start without a connection (FE-DB-ACCOUNTSETTINGS-*).
|
||||
*
|
||||
* The row belongs to the signed-in account's own database: another account on
|
||||
* the device never reads it, the anonymous database never holds it, and logout
|
||||
* takes it with the database.
|
||||
*/
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { cacheAccountSettings, cacheAccountSettingsPatch, cachedAccountSettings } from './cacheAccountSettings';
|
||||
import { deleteCurrentUserDb, offlineDb, reopenAnonymous, reopenForUser } from './offlineDb';
|
||||
|
||||
const LIBERTY = 'https://tiles.openfreemap.org/styles/liberty';
|
||||
const OSM = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';
|
||||
|
||||
afterEach(async () => {
|
||||
vi.restoreAllMocks();
|
||||
for (const user of [701, 702]) {
|
||||
await reopenForUser(user);
|
||||
await deleteCurrentUserDb();
|
||||
}
|
||||
await reopenAnonymous();
|
||||
});
|
||||
|
||||
describe('account settings kept offline', () => {
|
||||
it('FE-DB-ACCOUNTSETTINGS-001: keeps the last answer, the next one replacing it', async () => {
|
||||
await reopenForUser(701);
|
||||
expect(await cachedAccountSettings()).toBeNull();
|
||||
|
||||
await cacheAccountSettings({ map_provider: 'mapbox-gl', distance_unit: 'imperial' });
|
||||
await cacheAccountSettings({ map_provider: 'maplibre-gl', maplibre_style: LIBERTY });
|
||||
|
||||
expect(await cachedAccountSettings()).toEqual({ map_provider: 'maplibre-gl', maplibre_style: LIBERTY });
|
||||
expect(await offlineDb.accountSettings.count()).toBe(1);
|
||||
});
|
||||
|
||||
it('FE-DB-ACCOUNTSETTINGS-002: a saved change is laid over the kept answer', async () => {
|
||||
await reopenForUser(701);
|
||||
await cacheAccountSettings({ map_provider: 'leaflet', map_tile_url: OSM });
|
||||
|
||||
await cacheAccountSettingsPatch({ map_provider: 'maplibre-gl' });
|
||||
|
||||
expect(await cachedAccountSettings()).toEqual({ map_provider: 'maplibre-gl', map_tile_url: OSM });
|
||||
});
|
||||
|
||||
it('FE-DB-ACCOUNTSETTINGS-003: a change saved before any answer was kept is kept on its own', async () => {
|
||||
await reopenForUser(701);
|
||||
|
||||
await cacheAccountSettingsPatch({ map_provider: 'maplibre-gl' });
|
||||
|
||||
expect(await cachedAccountSettings()).toEqual({ map_provider: 'maplibre-gl' });
|
||||
});
|
||||
|
||||
it('FE-DB-ACCOUNTSETTINGS-004: another account on the device reads nothing of the first', async () => {
|
||||
await reopenForUser(701);
|
||||
await cacheAccountSettings({ map_provider: 'maplibre-gl' });
|
||||
|
||||
await reopenForUser(702);
|
||||
expect(await cachedAccountSettings()).toBeNull();
|
||||
|
||||
await reopenForUser(701);
|
||||
expect(await cachedAccountSettings()).toEqual({ map_provider: 'maplibre-gl' });
|
||||
});
|
||||
|
||||
it('FE-DB-ACCOUNTSETTINGS-005: logout takes the kept settings with the database', async () => {
|
||||
await reopenForUser(701);
|
||||
await cacheAccountSettings({ map_provider: 'maplibre-gl' });
|
||||
|
||||
await deleteCurrentUserDb();
|
||||
await reopenForUser(701);
|
||||
|
||||
expect(await cachedAccountSettings()).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-DB-ACCOUNTSETTINGS-006: the anonymous database never holds account settings', async () => {
|
||||
await reopenAnonymous();
|
||||
|
||||
await cacheAccountSettings({ map_provider: 'maplibre-gl' });
|
||||
await cacheAccountSettingsPatch({ map_provider: 'mapbox-gl' });
|
||||
|
||||
expect(await offlineDb.accountSettings.count()).toBe(0);
|
||||
expect(await cachedAccountSettings()).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-DB-ACCOUNTSETTINGS-007: a database that refuses never fails the caller', async () => {
|
||||
await reopenForUser(701);
|
||||
vi.spyOn(offlineDb.accountSettings, 'get').mockRejectedValue(new Error('refused'));
|
||||
vi.spyOn(offlineDb.accountSettings, 'put').mockRejectedValue(new Error('refused'));
|
||||
|
||||
await expect(cachedAccountSettings()).resolves.toBeNull();
|
||||
await expect(cacheAccountSettings({ map_provider: 'maplibre-gl' })).resolves.toBeUndefined();
|
||||
await expect(cacheAccountSettingsPatch({ map_provider: 'maplibre-gl' })).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { Settings } from '../types';
|
||||
import { isAccountDb, offlineDb } from './offlineDb';
|
||||
|
||||
/*
|
||||
* The account's settings as the server last answered them, so an app started
|
||||
* without a connection draws the map the user chose (provider, style, template)
|
||||
* and keeps their units, rather than falling back to the built-in defaults until
|
||||
* the first answer arrives. The server stays the source of truth: its next
|
||||
* answer replaces the row, and a kept row is never sent back.
|
||||
*
|
||||
* The row lives in the signed-in user's own database (offlineDb.ts), so another
|
||||
* account on the device never reads it, and logout deletes it with the database.
|
||||
* It holds what the server sent this user, the map keys included: the trip data
|
||||
* beside it is no less private, and without the CARTO key a stored CARTO
|
||||
* template could not be drawn offline. Secrets the server masks arrive masked.
|
||||
*
|
||||
* Every function here is best effort and never rejects. A device that cannot
|
||||
* keep the row (IndexedDB refused in a private window) behaves as before: it
|
||||
* starts on the defaults until the server answers.
|
||||
*/
|
||||
|
||||
/** The settings the server last answered, or null when this device kept none. */
|
||||
export async function cachedAccountSettings(): Promise<Partial<Settings> | null> {
|
||||
try {
|
||||
if (!isAccountDb()) return null;
|
||||
return (await offlineDb.accountSettings.get('account'))?.settings ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Keeps the server's answer, replacing the one kept before. */
|
||||
export async function cacheAccountSettings(settings: Partial<Settings>): Promise<void> {
|
||||
try {
|
||||
if (!isAccountDb()) return;
|
||||
await offlineDb.accountSettings.put({ key: 'account', settings });
|
||||
} catch {
|
||||
/* Nothing kept: the next start without a connection shows the defaults, as it always did. */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lays settings the server just accepted over the kept answer, so a change made
|
||||
* online is what the next start without a connection shows.
|
||||
*/
|
||||
export async function cacheAccountSettingsPatch(patch: Partial<Settings>): Promise<void> {
|
||||
try {
|
||||
if (!isAccountDb()) return;
|
||||
await offlineDb.transaction('rw', offlineDb.accountSettings, async () => {
|
||||
const kept = (await offlineDb.accountSettings.get('account'))?.settings;
|
||||
await offlineDb.accountSettings.put({ key: 'account', settings: { ...kept, ...patch } });
|
||||
});
|
||||
} catch {
|
||||
/* The kept answer stays as it was; the next server answer replaces it anyway. */
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import type {
|
||||
PackingItem,
|
||||
Place,
|
||||
Reservation,
|
||||
Settings,
|
||||
Tag,
|
||||
TodoItem,
|
||||
Trip,
|
||||
@@ -260,6 +261,18 @@ interface AtlasSnapshotRow {
|
||||
data: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* The account's settings as the server last answered them, one row. Kept so an
|
||||
* offline cold start draws the map provider, style and template the user chose
|
||||
* instead of the built-in defaults; the server stays the source of truth and the
|
||||
* next answer replaces the row. The database is the signed-in user's own, so the
|
||||
* row needs no user key and goes with the database on logout.
|
||||
*/
|
||||
interface CachedAccountSettings {
|
||||
key: 'account';
|
||||
settings: Partial<Settings>;
|
||||
}
|
||||
|
||||
export interface SyncMeta {
|
||||
tripId: number;
|
||||
lastSyncedAt: number | null;
|
||||
@@ -394,6 +407,7 @@ export class TrekOfflineDb extends Dexie {
|
||||
tripAddons!: Table<CachedTripAddons, number>;
|
||||
userMutationQueue!: Table<UserQueuedMutation, string>;
|
||||
atlasSnapshot!: Table<AtlasSnapshotRow, AtlasSnapshotPart>;
|
||||
accountSettings!: Table<CachedAccountSettings, 'account'>;
|
||||
|
||||
constructor(name: string = ANON_DB_NAME) {
|
||||
super(name);
|
||||
@@ -514,6 +528,11 @@ export class TrekOfflineDb extends Dexie {
|
||||
atlasSnapshot: 'part',
|
||||
userMutationQueue: 'id, status, createdAt, entity',
|
||||
});
|
||||
|
||||
// v18: the account's settings as the server last answered them, one row, so
|
||||
// an offline cold start shows the user's map instead of the defaults. A new
|
||||
// table: nothing an install already holds is touched.
|
||||
this.version(18).stores({ accountSettings: 'key' });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -553,6 +572,15 @@ export async function reopenAnonymous(): Promise<void> {
|
||||
await switchTo(ANON_DB_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the open database belongs to a signed-in account. Account data (its
|
||||
* settings) is only ever kept there, never in the anonymous database the next
|
||||
* person on this device would open.
|
||||
*/
|
||||
export function isAccountDb(): boolean {
|
||||
return _db.name !== ANON_DB_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the current user's scoped database entirely and return to the anonymous
|
||||
* DB. Used on logout so no trace of the account's data remains on the device.
|
||||
|
||||
Reference in New Issue
Block a user