Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(tours): zoom the flyover around the rider and keep no stale GPX file

Mauricedc1980712676
diff --git a/client/src/components/Tours/TourDetailDialog.tsx b/client/src/components/Tours/TourDetailDialog.tsx
index 0823010a8..eb83ce1d9 100644
--- a/client/src/components/Tours/TourDetailDialog.tsx
+++ b/client/src/components/Tours/TourDetailDialog.tsx
@@ -676,7 +676,9 @@ export default function TourDetailDialog({
)}
{/* A tour written here offline has no server copy to export until it syncs. */}
{analysis && place.id > 0 && (
- <TourGpxExportButton target={{ tripId: place.trip_id, placeId: place.id, name: place.name }} />
+ <TourGpxExportButton
+ target={{ tripId: place.trip_id, placeId: place.id, name: place.name, revision: tour.revision }}
+ />
)}
{place.id > 0 && (
<TourSaveToCollectionButton target={{ tripId: place.trip_id, placeId: place.id, name: place.name }} />
diff --git a/client/src/components/Tours/planner/TourPlannerPanels.tsx b/client/src/components/Tours/planner/TourPlannerPanels.tsx
index 6edb13a4f..e58c66caa 100644
--- a/client/src/components/Tours/planner/TourPlannerPanels.tsx
+++ b/client/src/components/Tours/planner/TourPlannerPanels.tsx
@@ -330,7 +330,7 @@ function GpxViewRail({
/>
{tour.place_id > 0 && (
<>
- <TourGpxExportBarButton target={{ tripId: planner.tripId, placeId: tour.place_id, name: tour.name }} unsaved={false} />
+ <TourGpxExportBarButton target={{ tripId: planner.tripId, placeId: tour.place_id, name: tour.name, revision: tour.revision }} unsaved={false} />
<TourSaveToCollectionBarButton target={{ tripId: planner.tripId, placeId: tour.place_id, name: tour.name }} unsaved={false} />
</>
)}
diff --git a/client/src/components/Tours/terrain/TourTerrainMap.test.tsx b/client/src/components/Tours/terrain/TourTerrainMap.test.tsx
index 798792ade..26352ad1e 100644
--- a/client/src/components/Tours/terrain/TourTerrainMap.test.tsx
+++ b/client/src/components/Tours/terrain/TourTerrainMap.test.tsx
@@ -23,13 +23,17 @@ const gl = vi.hoisted(() => {
const sources = new Map<string, Record<string, unknown>>();
const markers: MarkerStub[] = [];
const container = document.createElement('div');
- /** A gesture handler: on or off, and whether wheel or pinch zoom around the centre. */
+ /**
+ * A gesture handler: on or off, and whether wheel or pinch zoom around the centre. Like
+ * MapLibre's, enable() on a handler that is already on changes nothing, its options included.
+ */
const handler = () => {
const state = { enabled: true, around: undefined as string | undefined };
return {
state,
isEnabled: vi.fn(() => state.enabled),
enable: vi.fn((options?: { around?: string }) => {
+ if (state.enabled) return;
state.enabled = true;
state.around = options?.around;
}),
@@ -563,4 +567,29 @@ describe('TourTerrainMap', () => {
step(1);
expect(lastCamera()).toEqual(expect.objectContaining({ zoom: 13.4, pitch: 52 }));
});
+
+ it('FE-COMP-TERRAIN-019: wheel and pinch switch to the rider through an off and on, a gesture that was off stays off, and leaving puts them back', async () => {
+ frameClock();
+ gl.map.touchZoomRotate.state.enabled = false;
+ const { unmount } = await renderTerrain({ samples: LONG_TOUR });
+ loadStyle();
+ await act(async () => openTiles());
+ fireEvent.click(await screen.findByRole('button', { name: 'Play flyover' }));
+
+ // An enable() on a handler that is on is ignored, so the anchor only takes after a disable.
+ const { scrollZoom, touchZoomRotate, dragPan } = gl.map;
+ expect(scrollZoom.enable).toHaveBeenLastCalledWith({ around: 'center' });
+ const lastOf = (order: number[]) => order[order.length - 1];
+ expect(lastOf(scrollZoom.disable.mock.invocationCallOrder)).toBeLessThan(
+ lastOf(scrollZoom.enable.mock.invocationCallOrder)
+ );
+ expect(scrollZoom.state).toEqual({ enabled: true, around: 'center' });
+ expect(touchZoomRotate.enable).not.toHaveBeenCalled();
+ expect(touchZoomRotate.state.enabled).toBe(false);
+
+ unmount();
+ expect(scrollZoom.state).toEqual({ enabled: true, around: undefined });
+ expect(touchZoomRotate.state.enabled).toBe(false);
+ expect(dragPan.state.enabled).toBe(true);
+ });
});
diff --git a/client/src/components/Tours/terrain/useTerrainReplay.ts b/client/src/components/Tours/terrain/useTerrainReplay.ts
index 41cb650b6..609741b17 100644
--- a/client/src/components/Tours/terrain/useTerrainReplay.ts
+++ b/client/src/components/Tours/terrain/useTerrainReplay.ts
@@ -35,22 +35,38 @@ interface ChaseLens {
const flyoverLens = (): ChaseLens => ({ zoom: REPLAY_ZOOM, pitch: REPLAY_PITCH });
+/** A zoom gesture MapLibre lets zoom around the centre: it takes that only on its way on. */
+interface ZoomGesture {
+ disable(): void;
+ enable(options?: { around: 'center' }): void;
+}
+
+/**
+ * Switches a running zoom gesture over. MapLibre's enable() returns at once on a handler that
+ * is already on, so the new anchor only holds after a disable first.
+ */
+function reanchor(gesture: ZoomGesture, around: 'center' | 'cursor') {
+ gesture.disable();
+ gesture.enable(around === 'center' ? { around: 'center' } : undefined);
+}
+
/**
* While the flyover runs, wheel and pinch zoom around the rider instead of the cursor, so the
* camera never jumps back to the line after a zoom. Dragging would only pan away from a camera
- * that pulls straight back, so it is off until the flyover stops. Returns the undo.
+ * that pulls straight back, so it is off until the flyover stops. Returns the undo, which puts
+ * wheel and pinch back on the cursor the map was built with; a gesture that was off stays off.
*/
function followGestures(map: MapLibreMap): () => void {
const pan = map.dragPan.isEnabled();
const wheel = map.scrollZoom.isEnabled();
const pinch = map.touchZoomRotate.isEnabled();
if (pan) map.dragPan.disable();
- if (wheel) map.scrollZoom.enable({ around: 'center' });
- if (pinch) map.touchZoomRotate.enable({ around: 'center' });
+ if (wheel) reanchor(map.scrollZoom, 'center');
+ if (pinch) reanchor(map.touchZoomRotate, 'center');
return () => {
if (pan) map.dragPan.enable();
- if (wheel) map.scrollZoom.enable();
- if (pinch) map.touchZoomRotate.enable();
+ if (wheel) reanchor(map.scrollZoom, 'cursor');
+ if (pinch) reanchor(map.touchZoomRotate, 'cursor');
};
}
diff --git a/client/src/components/Tours/useTourGpxExport.test.tsx b/client/src/components/Tours/useTourGpxExport.test.tsx
index e3142258c..7a18c243e 100644
--- a/client/src/components/Tours/useTourGpxExport.test.tsx
+++ b/client/src/components/Tours/useTourGpxExport.test.tsx
@@ -224,6 +224,42 @@ describe('useTourGpxExport, sharing (IO-01)', () => {
expect(repo.exportGpx).toHaveBeenCalledTimes(2);
});
+ it('a saved change to the tour drops the file fetched ahead and fetches the new one (P2C-03)', async () => {
+ share.canShareFile.mockReturnValue(true);
+ share.shareFile.mockResolvedValue('shared');
+ const saved = { ...target, revision: 3 };
+ const { result, rerender } = renderHook(({ prefetch }) => useTourGpxExport(prefetch), {
+ initialProps: { prefetch: saved },
+ });
+ await act(async () => undefined);
+ expect(repo.exportGpx).toHaveBeenCalledTimes(1);
+
+ // The same tour, saved elsewhere and arriving live: name and id unchanged, a new revision.
+ const newer = new Blob(['<gpx>newer</gpx>'], { type: 'application/gpx+xml' });
+ repo.exportGpx.mockResolvedValueOnce({ offline: false, blob: newer, filename: 'Kramer-über.gpx' });
+ rerender({ prefetch: { ...saved, revision: 4 } });
+ await act(async () => undefined);
+ expect(repo.exportGpx).toHaveBeenCalledTimes(2);
+
+ act(() => {
+ void result.current.shareGpx({ ...saved, revision: 4 });
+ });
+ expect(repo.exportGpx).toHaveBeenCalledTimes(2);
+ expect(share.shareFile).toHaveBeenCalledWith(expect.objectContaining({ size: newer.size }), 'Kramer');
+ await act(async () => undefined);
+ });
+
+ it('a file a tap kept is not shared for a newer revision of the tour (P2C-03)', async () => {
+ share.shareFile.mockResolvedValue('shared');
+ const { result } = renderHook(() => useTourGpxExport());
+ await act(() => result.current.shareGpx({ ...target, revision: 1 }));
+ await act(() => result.current.shareGpx({ ...target, revision: 1 }));
+ expect(repo.exportGpx).toHaveBeenCalledTimes(1);
+ await act(() => result.current.shareGpx({ ...target, revision: 2 }));
+ expect(repo.exportGpx).toHaveBeenCalledTimes(2);
+ expect(share.shareFile).toHaveBeenCalledTimes(3);
+ });
+
it('a kept file belongs to its tour: another tour is fetched afresh', async () => {
share.shareFile.mockResolvedValueOnce('needs-gesture').mockResolvedValueOnce('shared');
const { result } = renderHook(() => useTourGpxExport());
diff --git a/client/src/components/Tours/useTourGpxExport.ts b/client/src/components/Tours/useTourGpxExport.ts
index 55c5c16c2..437ec6180 100644
--- a/client/src/components/Tours/useTourGpxExport.ts
+++ b/client/src/components/Tours/useTourGpxExport.ts
@@ -11,6 +11,8 @@ const GPX_TYPE = 'application/gpx+xml';
/** A tour's GPX file kept for the next share, as the bytes and the name the server gave them. */
interface PreparedFile {
placeId: number;
+ /** The tour's revision the file was read at; a file of another one is no longer the saved tour. */
+ revision: number | null;
blob: Blob;
name: string;
}
@@ -23,8 +25,21 @@ export interface TourGpxExportTarget {
tripId: number | string;
placeId: number;
name: string;
+ /**
+ * P2C-03: the tour's revision, raised by every saved change to it. A kept
+ * file belongs to the revision it was read at, so a save (here or arriving
+ * live from elsewhere) drops it. Absent in a tour cached before revisions.
+ */
+ revision?: number | null;
}
+/** The revision a target names, one value for "none" so an absent and a null one agree. */
+const revisionOf = (target: TourGpxExportTarget | null) => target?.revision ?? null;
+
+/** The kept file when it is this tour at this revision, else null. */
+const keptFor = (kept: PreparedFile | null, target: TourGpxExportTarget) =>
+ kept?.placeId === target.placeId && kept.revision === revisionOf(target) ? kept : null;
+
/**
* PR1-06A, IO-01: a saved tour handed over as a GPX file, started on purpose:
* downloaded, or given to the system's share sheet where the platform takes
@@ -56,6 +71,7 @@ export function useTourGpxExport(prefetch: TourGpxExportTarget | null = null) {
const prefetchTripId = prefetch?.tripId ?? null;
const prefetchPlaceId = prefetch?.placeId ?? null;
const prefetchName = prefetch?.name ?? '';
+ const prefetchRevision = revisionOf(prefetch);
useEffect(() => {
if (!canShare || prefetchTripId === null || prefetchPlaceId === null) return;
const controller = new AbortController();
@@ -64,6 +80,7 @@ export function useTourGpxExport(prefetch: TourGpxExportTarget | null = null) {
if (controller.signal.aborted || !('blob' in result)) return;
preparedRef.current = {
placeId: prefetchPlaceId,
+ revision: prefetchRevision,
blob: result.blob,
name: result.filename ?? fallbackName(prefetchName),
};
@@ -76,7 +93,7 @@ export function useTourGpxExport(prefetch: TourGpxExportTarget | null = null) {
// Another tour, or the same one with changes the kept file does not hold.
preparedRef.current = null;
};
- }, [canShare, prefetchTripId, prefetchPlaceId, prefetchName]);
+ }, [canShare, prefetchTripId, prefetchPlaceId, prefetchName, prefetchRevision]);
/** The tour's file from the server, or null when there is none to hand over, said in a toast. */
const fetchFile = useCallback(
@@ -116,12 +133,14 @@ export function useTourGpxExport(prefetch: TourGpxExportTarget | null = null) {
const shareGpx = useCallback(
async (target: TourGpxExportTarget) => {
if (sharingRef.current) return;
- const prepared = preparedRef.current?.placeId === target.placeId ? preparedRef.current : null;
+ const prepared = keptFor(preparedRef.current, target);
// A fresh File for every sheet, so no share is handed one an earlier sheet still holds.
// It goes to the sheet before anything is awaited, inside the tap that asked.
const file = prepared ? new File([prepared.blob], prepared.name, { type: GPX_TYPE }) : await fetchFile(target);
if (!file) return;
- if (!prepared) preparedRef.current = { placeId: target.placeId, blob: file, name: file.name };
+ if (!prepared) {
+ preparedRef.current = { placeId: target.placeId, revision: revisionOf(target), blob: file, name: file.name };
+ }
sharingRef.current = true;
let outcome: FileShareOutcome;
try {