feat(tours): tours read and write through the offline core

A tourRepo replaces the direct toursApi calls: reads fall back to a new
Dexie tours table, and a tour saved or edited offline is written there and
queued like a place, under one temporary id for the place and its facet.
The queue moves both to the real id on replay and drops both when the
server refuses the create. Trip sync caches each trip's tours while the
addon is on, so a tour no longer reads as a plain place offline.

The tour metrics move to shared so the offline copy shows the server's
numbers, and useTourPlaceIds keeps one Set per change as intended.
This commit is contained in:
Maurice
2026-10-07 21:30:45 +02:00
parent f7edd849ab
commit e0efa0314a
23 changed files with 839 additions and 133 deletions
+3 -15
View File
@@ -11,19 +11,7 @@ export function haversineKm(lat1: number, lng1: number, lat2: number, lng2: numb
}
/**
* The same distance in metres.
*
* There were three implementations of this in the tree: this one, a copy in
* maps.helpers.ts and a private one in places.helpers.ts that differed by a
* single clamp. The clamped form is the one kept — `asin` of a value a hair over
* 1, which floating point does produce for near-antipodal points, is NaN, and a
* NaN distance silently fails every comparison it feeds rather than throwing.
* The same distance in metres. The clamped form lives in `@trek/shared`, where
* the client's tour metrics read it too.
*/
export function haversineMetres(aLat: number, aLng: number, bLat: number, bLng: number): number {
const toRad = (deg: number) => (deg * Math.PI) / 180;
const dLat = toRad(bLat - aLat);
const dLng = toRad(bLng - aLng);
const h =
Math.sin(dLat / 2) ** 2 + Math.cos(toRad(aLat)) * Math.cos(toRad(bLat)) * Math.sin(dLng / 2) ** 2;
return 2 * 6371000 * Math.asin(Math.min(1, Math.sqrt(h)));
}
export { haversineMetres } from '@trek/shared';
+3 -45
View File
@@ -3,52 +3,10 @@
* PlacesService prepares and persists imported places; these helpers derive
* metrics from the resulting [[lat,lng,ele?]] geometry.
*/
import { haversineMetres } from '../common/geo';
import type { GeometryPoint } from '@trek/shared';
/** One decoded `route_geometry` point: `[lat, lng]` or `[lat, lng, ele]`. */
export type GeometryPoint = [number, number] | [number, number, number];
export interface TourMetrics {
/** Sum of segment haversine distances, in km. */
distanceKm: number;
/** Sum of positive elevation deltas, in metres. Null when the track carries no elevation. */
elevationGainM: number | null;
/** Sum of negative elevation deltas (as a positive number), in metres. Null when no elevation. */
elevationLossM: number | null;
/** Whether every point in the geometry carried a 3rd (elevation) value. */
hasElevation: boolean;
}
/**
* Derives distance/elevation from a parsed `route_geometry` array. Never throws —
* A track with fewer than 2 points, or with missing/mixed
* elevation, still yields a usable (if partial) result rather than blocking import.
*/
export function computeTourMetrics(points: GeometryPoint[]): TourMetrics {
if (points.length < 2) {
return { distanceKm: 0, elevationGainM: null, elevationLossM: null, hasElevation: false };
}
const hasElevation = points.every(p => p.length === 3 && Number.isFinite(p[2]));
let distanceKm = 0;
let gain = 0;
let loss = 0;
for (let i = 1; i < points.length; i++) {
distanceKm += haversineMetres(points[i - 1][0], points[i - 1][1], points[i][0], points[i][1]) / 1000;
if (hasElevation) {
const delta = (points[i][2] as number) - (points[i - 1][2] as number);
if (delta > 0) gain += delta;
else loss += -delta;
}
}
return {
distanceKm,
elevationGainM: hasElevation ? gain : null,
elevationLossM: hasElevation ? loss : null,
hasElevation,
};
}
// The metrics are shared with the client, which computes them for a tour saved offline.
export { computeTourMetrics, type GeometryPoint, type TourMetrics } from '@trek/shared';
/**
* Parses a `places.route_geometry` JSON string into typed points. Returns an empty