mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +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,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. */
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user