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:
Maurice
2026-11-30 14:43:37 +01:00
parent 9cbbbdb2e9
commit 33f6540ec3
6 changed files with 342 additions and 1 deletions
@@ -60,6 +60,7 @@ const HISTORY: Record<number, StoreSpec> = {
15: { tripTags: 'tripId' },
16: { tripAddons: 'tripId' },
17: { atlasSnapshot: 'part', userMutationQueue: 'id, status, createdAt, entity' },
18: { accountSettings: 'key' },
};
const HISTORY_VERSIONS = Object.keys(HISTORY)
@@ -164,6 +165,10 @@ function rowsAt(version: number): Record<string, Record<string, unknown>> {
schemaVersion: 1,
buildVersion: '5.3.0',
},
accountSettings: {
key: 'account',
settings: { map_provider: 'maplibre-gl', maplibre_style: 'https://tiles.openfreemap.org/styles/bright' },
},
};
const tables = schemaAt(version);
return Object.fromEntries(Object.entries(all).filter(([table]) => table in tables));
@@ -456,6 +461,20 @@ const UPGRADE_CASES: Record<number, UpgradeCase> = {
expect(await db.userMutationQueue.orderBy('createdAt').primaryKeys()).toEqual(['b', 'a']);
},
},
18: {
intent: 'adds the kept account settings, and leaves every table an install holds as it was',
check: async (db) => {
// Nothing is kept until the server answers once after the upgrade.
expect(await db.accountSettings.count()).toBe(0);
// One row: the next answer replaces the one kept before.
await db.accountSettings.put({ key: 'account', settings: { map_provider: 'mapbox-gl' } });
await db.accountSettings.put({ key: 'account', settings: { map_provider: 'maplibre-gl' } });
expect(await db.accountSettings.count()).toBe(1);
expect((await db.accountSettings.get('account'))?.settings).toEqual({ map_provider: 'maplibre-gl' });
// The account queue from v17 is untouched by the new table.
expect(await db.userMutationQueue.count()).toBe(1);
},
},
};
describe('offlineDb upgrades', () => {