feat(offline): cache the trip's area so place search works with no network

An offline instance had the trip's own places and nothing else, so "find
a pharmacy near the hotel" had nothing to answer from: every lookup went
to a server.

The index can hand over a bounding box in one request, so it does, once,
next to the map tiles. A trip to Rostock is roughly a megabyte, refreshed
only when the trip's area actually moves. Search and autocomplete fall
back to it at the API layer, which is why all ten call sites got the
offline path without any of them learning about it, and the answer says
`source: 'offline-cache'` rather than pretending to be live.

Country packages stay what they were: a SQLite file for anyone who would
rather run their own copy of the service. A browser cannot query one
without shipping a WASM SQLite, and Germany alone would not fit in
IndexedDB, so that door stays where it belongs. /v1/packages lists them,
because "you can leave" only means something if the door is visible.
This commit is contained in:
Maurice
2026-09-06 00:36:53 +02:00
committed by Maurice
parent 715993feaf
commit 87a765b763
10 changed files with 710 additions and 2 deletions
+43
View File
@@ -68,6 +68,14 @@ export interface SyncMeta {
tilesBbox: [number, number, number, number] | null;
/** Non-photo files available offline for this trip after the last sync. */
filesCachedCount: number;
/**
* The rounded bbox the cached area places were fetched for. Compared rather
* than a timestamp: adding a place inside the area the trip already covers
* should not re-download it, and moving the trip should.
*
* Optional so a row written before this landed still reads.
*/
areaPlacesKey?: string;
}
export interface BlobCacheEntry {
@@ -86,6 +94,33 @@ export interface BlobCacheEntry {
cachedAt: number;
}
/**
* A place from the TREK Places index, cached for the area of one trip.
*
* NOT a trip place — those are in `places` and belong to the user. These are
* candidates: the shops, restaurants and stations that happen to be near where
* the trip goes, taken once so search still answers on a plane. They are
* disposable and are re-fetched whenever the trip's area changes.
*
* The key is the GERS id, which is stable across index rebuilds; `tripId` is
* indexed so the whole set can be dropped with the trip.
*/
export interface CachedAreaPlace {
/** GERS id, without the `gers:` prefix the server puts on `osm_id`. */
gers: string;
tripId: number;
name: string;
/** Lowercased, diacritics folded — what an offline search actually matches on. */
searchName: string;
address: string;
lat: number | null;
lng: number | null;
category: string | null;
website: string | null;
phone: string | null;
cachedAt: number;
}
/** An uploaded booking-import source file, kept so the review flow can attach it to the
* created bookings even after a page reload during the (background) parse. Keyed by job. */
export interface ImportSourceFile {
@@ -141,6 +176,7 @@ class TrekOfflineDb extends Dexie {
syncMeta!: Table<SyncMeta, number>;
blobCache!: Table<BlobCacheEntry, string>;
importFiles!: Table<ImportSourceFile, [string, string]>;
areaPlaces!: Table<CachedAreaPlace, string>;
constructor(name: string = ANON_DB_NAME) {
super(name);
@@ -181,6 +217,13 @@ class TrekOfflineDb extends Dexie {
this.version(4).stores({
importFiles: '[jobId+fileName], jobId, createdAt',
});
// v5: places from the TREK Places index for the trip's area, so search
// answers offline. `searchName` is indexed because that is what an offline
// query filters on; `tripId` so the set drops with its trip.
this.version(5).stores({
areaPlaces: 'gers, tripId, searchName',
});
}
}