From 66bff5c3425d90aef218b662aff43fac3ecfd83c Mon Sep 17 00:00:00 2001 From: Maurice Date: Thu, 29 Oct 2026 16:47:57 +0100 Subject: [PATCH] fix(journey): drop the entry connection where a recorded day shows the way The journey map joins every located entry with a faint dashed line in the order of its pins. It drew that line through everything, also across days whose real route is on the map as a kept recording or the live Dawarich trail, so straight guesses fanned out over the recorded track. The phone has no layer switches, so the line could not even be hidden there. A recorded track now carries the day it belongs to (an optional `date` on the track contract, set by every builder of recorded lines: kept days, the shared page's recordings, the live Dawarich trail and the day import preview). A pure model splits the connection into the stretches still open and those whose days were all recorded, the night between two recorded days included. Both renderers draw only the open stretches with the entries; the covered ones sit on a layer of their own that shows while the reader hides what was recorded, so the guess comes back exactly where nothing else explains the way. The live trail is now matched to a kept day by that date instead of reading the track name as one. Closes #2888 --- .../components/Journey/JourneyMap.test.tsx | 49 +++++++- client/src/components/Journey/JourneyMap.tsx | 26 ++-- .../components/Journey/JourneyMapGL.test.tsx | 58 ++++++--- .../src/components/Journey/JourneyMapGL.tsx | 63 ++++++---- .../Journey/journeyLeafletLayers.ts | 20 ++- .../Journey/journeyMapConnections.test.ts | 116 ++++++++++++++++++ .../Journey/journeyMapConnections.ts | 93 ++++++++++++++ .../Journey/useJourneyDayImport.test.ts | 2 + .../components/Journey/useJourneyDayImport.ts | 3 +- .../Journey/useJourneyMapLayers.test.ts | 2 + .../components/Journey/useJourneyMapLayers.ts | 3 +- .../Journey/useJourneyRecordings.test.ts | 1 + .../Journey/useJourneyRecordings.ts | 1 + .../src/hooks/useDawarichJournalTrail.test.ts | 2 + client/src/hooks/useDawarichJournalTrail.ts | 1 + .../useJourneyDetailRecordings.test.ts | 6 +- .../useJourneyDetailRecordings.ts | 2 +- shared/src/journey/journey.schema.spec.ts | 15 +++ shared/src/journey/journey.schema.ts | 5 + 19 files changed, 403 insertions(+), 65 deletions(-) create mode 100644 client/src/components/Journey/journeyMapConnections.test.ts create mode 100644 client/src/components/Journey/journeyMapConnections.ts diff --git a/client/src/components/Journey/JourneyMap.test.tsx b/client/src/components/Journey/JourneyMap.test.tsx index 3e56ba393..26f645133 100644 --- a/client/src/components/Journey/JourneyMap.test.tsx +++ b/client/src/components/Journey/JourneyMap.test.tsx @@ -1,4 +1,4 @@ -// FE-COMP-JOURNEYMAP-001 to FE-COMP-JOURNEYMAP-055 +// FE-COMP-JOURNEYMAP-001 to FE-COMP-JOURNEYMAP-057 vi.mock('../../api/websocket', () => ({ connect: vi.fn(), @@ -50,7 +50,7 @@ vi.mock('leaflet', () => { map: vi.fn(() => mockMap), tileLayer: vi.fn(() => ({ addTo: vi.fn(), setUrl: vi.fn() })), marker: vi.fn(() => mockMarker), - polyline: vi.fn(() => { const line: any = { addTo: vi.fn(() => line), bindTooltip: vi.fn(() => line) }; return line }), + polyline: vi.fn(() => { const line: any = { addTo: vi.fn(() => line), bindTooltip: vi.fn(() => line), remove: vi.fn() }; return line }), divIcon: vi.fn(() => ({})), latLngBounds: vi.fn(() => ({})), layerGroup: vi.fn(() => ({ addLayer: vi.fn(), addTo: vi.fn(), remove: vi.fn() })), @@ -61,7 +61,7 @@ vi.mock('leaflet', () => { map: vi.fn(() => mockMap), tileLayer: vi.fn(() => ({ addTo: vi.fn(), setUrl: vi.fn() })), marker: vi.fn(() => mockMarker), - polyline: vi.fn(() => { const line: any = { addTo: vi.fn(() => line), bindTooltip: vi.fn(() => line) }; return line }), + polyline: vi.fn(() => { const line: any = { addTo: vi.fn(() => line), bindTooltip: vi.fn(() => line), remove: vi.fn() }; return line }), divIcon: vi.fn(() => ({})), latLngBounds: vi.fn(() => ({})), control: { attribution: vi.fn(() => ({ addTo: vi.fn() })) }, @@ -930,6 +930,49 @@ describe('JourneyMap', () => { map.latLngToContainerPoint.mockImplementation(() => ({ x: 0, y: 0 })); } }); + + describe('the line between the entries on recorded days (#2888)', () => { + const days = [ + { id: 'a', lat: 48.1, lng: 11.5, title: 'A', mood: null, entry_date: '2026-10-29' }, + { id: 'b', lat: 48.2, lng: 11.6, title: 'B', mood: null, entry_date: '2026-10-29' }, + { id: 'c', lat: 48.3, lng: 11.7, title: 'C', mood: null, entry_date: '2026-10-30' }, + ]; + const tracks = [ + { place_id: -1, trip_id: 0, name: '2026-10-29', color: '#7c3aed', points: [[48.1, 11.5], [48.2, 11.6]] as [number, number][], kind: 'dawarich_live' as const, date: '2026-10-29' }, + ]; + const connectionLines = () => { + const polyline = vi.mocked(L.polyline).mock; + return polyline.calls + .map((call, i) => ({ coords: call[0], dashed: (call[1] as { dashArray?: string } | undefined)?.dashArray === '4 6', line: polyline.results[i].value as { addTo: Mock; remove: Mock } })) + .filter(call => call.dashed); + }; + + it('FE-COMP-JOURNEYMAP-056: leaves the recorded day to its track and keeps the jump to the next day', () => { + render(); + + const lines = connectionLines(); + expect(lines.map(l => l.coords)).toEqual([ + [[48.2, 11.6], [48.3, 11.7]], + [[48.1, 11.5], [48.2, 11.6]], + ]); + expect(lines[0].line.addTo).toHaveBeenCalled(); + expect(lines[1].line.addTo).not.toHaveBeenCalled(); + }); + + it('FE-COMP-JOURNEYMAP-057: hiding what was recorded puts the covered stretch back, hiding the entries takes it off again', () => { + mockedMarkerWithRemove(); + const shown = { plan: true, recorded: true, entries: true }; + const { rerender } = render(); + const covered = connectionLines()[1].line; + + rerender(); + expect(covered.addTo).toHaveBeenCalledTimes(1); + + rerender(); + expect(covered.remove).toHaveBeenCalledTimes(1); + expect(L.map).toHaveBeenCalledTimes(1); + }); + }); }); /** The shared marker spy with the `remove` a hidden layer is taken off by. */ diff --git a/client/src/components/Journey/JourneyMap.tsx b/client/src/components/Journey/JourneyMap.tsx index 4d3071e5f..8e18c78df 100644 --- a/client/src/components/Journey/JourneyMap.tsx +++ b/client/src/components/Journey/JourneyMap.tsx @@ -11,7 +11,8 @@ import { escapeHtml, type JourneyTrack } from '@trek/shared' import { ensureJourneyPopupStyle, formatMarkerDate, journeyPopupHtml } from './journeyMapPopup' import { PLANNED_BADGE_SVG, type JourneyMapMark } from './journeyMapMarks' import { isPlannedTrack, type JourneyMapLayerVisibility } from './journeyMapLayers' -import { NOTHING_DRAWN, drawLeafletMarks, leafletWorldPx, switchLeafletLayers, type DrawnLayers, type LeafletLayerKind } from './journeyLeafletLayers' +import { NOTHING_DRAWN, drawLeafletMarks, isLeafletLayerShown, leafletWorldPx, switchLeafletLayers, type DrawnLayers, type LeafletLayerKind } from './journeyLeafletLayers' +import { entryConnections } from './journeyMapConnections' export interface MapMarkerItem { id: string @@ -347,7 +348,7 @@ function JourneyMap( iconPlannedRef.current = plannedRef.current const place = (kind: LeafletLayerKind, layer: T): T => { drawn[kind].push(layer) - if (visibilityRef.current[kind]) layer.addTo(map) + if (isLeafletLayerShown(kind, visibilityRef.current)) layer.addTo(map) return layer } @@ -403,17 +404,18 @@ function JourneyMap( coords.forEach(c => trackCoords.push(c)) } - // route trail: dashed line connecting entries in time order, same as JourneyMapGL - if (items.length > 1) { - const routeCoords = items.map(i => [i.lat, i.lng] as L.LatLngTuple) - place('entries', L.polyline(routeCoords, { - color: dark ? '#71717A' : '#A1A1AA', - weight: 1.5, - opacity: 0.5, - dashArray: '4 6', - lineCap: 'round', lineJoin: 'round', - })) + // The faint line between the entries in their order, same as JourneyMapGL, left + // off the stretches a recorded day already draws as travelled (#2888). + const connections = entryConnections(items.map(i => ({ lat: i.lat, lng: i.lng, date: i.time })), stableTracks) + const connectionStyle: L.PolylineOptions = { + color: dark ? '#71717A' : '#A1A1AA', + weight: 1.5, + opacity: 0.5, + dashArray: '4 6', + lineCap: 'round', lineJoin: 'round', } + for (const line of connections.open) place('entries', L.polyline(line, connectionStyle)) + for (const line of connections.covered) place('coveredConnections', L.polyline(line, connectionStyle)) // place markers items.forEach((item, i) => { diff --git a/client/src/components/Journey/JourneyMapGL.test.tsx b/client/src/components/Journey/JourneyMapGL.test.tsx index cfaf838d6..6bb7d0068 100644 --- a/client/src/components/Journey/JourneyMapGL.test.tsx +++ b/client/src/components/Journey/JourneyMapGL.test.tsx @@ -1,4 +1,4 @@ -// FE-COMP-JMAPGL-001 to FE-COMP-JMAPGL-040, FE-COMP-JMAPGL-2453 +// FE-COMP-JMAPGL-001 to FE-COMP-JMAPGL-042, FE-COMP-JMAPGL-2453 type Spy = ReturnType @@ -255,25 +255,14 @@ describe('JourneyMapGL', () => { render() expect(gl.map.addSource).toHaveBeenCalledWith('journey-route', expect.objectContaining({ type: 'geojson' })) - const source = vi.mocked(gl.map.addSource).mock.calls[0][1] as { data: { geometry: { coordinates: number[][] } } } - expect(source.data.geometry.coordinates).toEqual([[2.3522, 48.8566], [13.405, 52.52]]) + const source = vi.mocked(gl.map.addSource).mock.calls[0][1] as { data: { geometry: { coordinates: number[][][] } } } + expect(source.data.geometry.coordinates).toEqual([[[2.3522, 48.8566], [13.405, 52.52]]]) expect(gl.map.addLayer).toHaveBeenCalledWith(expect.objectContaining({ id: 'journey-route-line', source: 'journey-route', })) }) - it('FE-COMP-JMAPGL-008: an existing route source is updated instead of re-added', () => { - withToken() - const setData = vi.fn() - gl.map.getSource.mockReturnValue({ setData }) - - render() - - expect(gl.map.addSource).not.toHaveBeenCalled() - expect(setData).toHaveBeenCalledWith(expect.objectContaining({ type: 'Feature' })) - }) - it('FE-COMP-JMAPGL-038: a planned GPX track goes on the dashed layer, a recorded route on the solid one (JRN-01)', () => { withToken() render( @@ -764,4 +753,45 @@ describe('JourneyMapGL', () => { expect(gl.markers[0].element.innerHTML).toContain('#16a34a') expect(mapboxgl.Map).toHaveBeenCalledTimes(1) }) + + it('FE-COMP-JMAPGL-041: a recorded day takes the line between its entries off the map, the rest stays (#2888)', () => { + withToken() + const days = [ + { id: 'a', lat: 48.1, lng: 11.5, entry_date: '2026-10-29' }, + { id: 'b', lat: 48.2, lng: 11.6, entry_date: '2026-10-29' }, + { id: 'c', lat: 48.3, lng: 11.7, entry_date: '2026-10-30' }, + { id: 'd', lat: 48.4, lng: 11.8, entry_date: '2026-10-30' }, + ] + render( + , + ) + + const data = (id: string) => (vi.mocked(gl.map.addSource).mock.calls.find(c => c[0] === id)?.[1] as { data: { geometry: { coordinates: number[][][] } } }).data.geometry.coordinates + expect(data('journey-route')).toEqual([[[11.6, 48.2], [11.7, 48.3], [11.8, 48.4]]]) + expect(data('journey-route-covered')).toEqual([[[11.5, 48.1], [11.6, 48.2]]]) + const covered = vi.mocked(gl.map.addLayer).mock.calls.map(c => c[0] as { id: string; layout: Record }).find(l => l.id === 'journey-route-covered-line') + expect(covered?.layout.visibility).toBe('none') + }) + + it('FE-COMP-JMAPGL-042: hiding what was recorded brings the covered stretches back while the entries show', () => { + withToken() + const shown = { plan: true, recorded: true, entries: true } + const days = [ + { id: 'a', lat: 48.1, lng: 11.5, entry_date: '2026-10-29' }, + { id: 'b', lat: 48.2, lng: 11.6, entry_date: '2026-10-29' }, + ] + const tracks = [{ place_id: -1, trip_id: 0, name: 'Day', color: null, points: [[48.1, 11.5], [48.2, 11.6]] as [number, number][], kind: 'day_recording' as const, date: '2026-10-29' }] + const { rerender } = render() + expect(gl.map.addSource).not.toHaveBeenCalledWith('journey-route', expect.anything()) + + rerender() + expect(gl.map.setLayoutProperty).toHaveBeenCalledWith('journey-route-covered-line', 'visibility', 'visible') + + rerender() + expect(gl.map.setLayoutProperty).toHaveBeenLastCalledWith('journey-route-covered-line', 'visibility', 'none') + }) }) diff --git a/client/src/components/Journey/JourneyMapGL.tsx b/client/src/components/Journey/JourneyMapGL.tsx index fc3839fd7..d703ae5ad 100644 --- a/client/src/components/Journey/JourneyMapGL.tsx +++ b/client/src/components/Journey/JourneyMapGL.tsx @@ -9,6 +9,7 @@ import { SHOT_INDEX_ATTR, ensureJourneyPopupStyle, formatMarkerDate, journeyPopu import { clusterPhotos, photoMarkerHtml, type MapPhoto } from './journeyPhotoLayer' import { MARK_CLUSTER_PX, PLANNED_BADGE_SVG, clusterPoints, glWorldPx, markHitSvg, type JourneyMapMark } from './journeyMapMarks' import { isPlannedTrack, type JourneyMapLayerVisibility } from './journeyMapLayers' +import { coveredConnectionsShown, entryConnections } from './journeyMapConnections' export interface JourneyMapGLHandle { highlightMarker: (id: string | null) => void @@ -114,6 +115,33 @@ const LAYER_IDS: Record = { } const EVERY_LAYER: JourneyMapLayerVisibility = { plan: true, recorded: true, entries: true } const NO_PLANNED: ReadonlySet = new Set() +/** The stretches between entries a recorded day already draws; shown only while that recording is hidden. */ +const COVERED_CONNECTION_LAYER = 'journey-route-covered-line' + +/** One of the faint lines between the entries, as a dashed layer over its own source. */ +function addConnectionLayer(map: mapboxgl.Map, id: string, lines: [number, number][][], dark: boolean, visible: boolean) { + if (lines.length === 0) return + map.addSource(id, { + type: 'geojson', + data: { + type: 'Feature', + properties: {}, + geometry: { type: 'MultiLineString', coordinates: lines.map(line => line.map(([lat, lng]) => [lng, lat])) }, + }, + }) + map.addLayer({ + id: `${id}-line`, + type: 'line', + source: id, + paint: { + 'line-color': dark ? '#71717A' : '#A1A1AA', + 'line-width': 1.5, + 'line-opacity': 0.5, + 'line-dasharray': [2, 3], + }, + layout: { 'line-cap': 'round', 'line-join': 'round', visibility: visible ? 'visible' : 'none' }, + }) +} function markerHtml(dayColor: string, dayLabel: number, highlighted: boolean, planned = false): HTMLDivElement { const fill = dayColor @@ -397,31 +425,12 @@ function JourneyMapGL( try { map.setConfigProperty('basemap', 'language', basemapLanguage(mapLangRef.current)) } catch { /* style/SDK may not support it */ } } - // route trail — dashed line connecting entries in time order - if (items.length > 1) { - const coords = items.map(i => [i.lng, i.lat]) - if (map.getSource('journey-route')) (map.getSource('journey-route') as mapboxgl.GeoJSONSource).setData({ - type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: coords } as GeoJSON.LineString, - }) - else { - map.addSource('journey-route', { - type: 'geojson', - data: { type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: coords } as GeoJSON.LineString }, - }) - map.addLayer({ - id: 'journey-route-line', - type: 'line', - source: 'journey-route', - paint: { - 'line-color': darkRef.current ? '#71717A' : '#A1A1AA', - 'line-width': 1.5, - 'line-opacity': 0.5, - 'line-dasharray': [2, 3], - }, - layout: layout('entries', { 'line-cap': 'round', 'line-join': 'round' }), - }) - } - } + // The faint line between the entries in their order, left off the stretches + // a recorded day already draws as travelled (#2888). Those go on a layer of + // their own that stands in while the reader hides the recording. + const connections = entryConnections(items.map(i => ({ lat: i.lat, lng: i.lng, date: i.time })), stableTracks) + addConnectionLayer(map, 'journey-route', connections.open, !!darkRef.current, visibilityRef.current.entries) + addConnectionLayer(map, 'journey-route-covered', connections.covered, !!darkRef.current, coveredConnectionsShown(visibilityRef.current)) // Tracks, one source for all of them, coloured per feature so a journey // with several routes keeps them apart. Casing underneath, same as the @@ -576,6 +585,10 @@ function JourneyMapGL( markersRef.current.forEach(m => { m.getElement().style.display = layerVisibility.entries ? '' : 'none' }) } } + const coveredShown = coveredConnectionsShown(layerVisibility) + if (coveredConnectionsShown(shownRef.current) !== coveredShown) { + try { map.setLayoutProperty(COVERED_CONNECTION_LAYER, 'visibility', coveredShown ? 'visible' : 'none') } catch { /* not drawn on this map */ } + } shownRef.current = layerVisibility }, [layerVisibility]) diff --git a/client/src/components/Journey/journeyLeafletLayers.ts b/client/src/components/Journey/journeyLeafletLayers.ts index 511b720e2..e6d9754dc 100644 --- a/client/src/components/Journey/journeyLeafletLayers.ts +++ b/client/src/components/Journey/journeyLeafletLayers.ts @@ -1,12 +1,21 @@ import L from 'leaflet'; +import { coveredConnectionsShown } from './journeyMapConnections'; import type { JourneyMapLayerVisibility } from './journeyMapLayers'; import { MARK_CLUSTER_PX, clusterPoints, markHitSvg, type JourneyMapMark, type WorldPx } from './journeyMapMarks'; -/** The layers of the Leaflet journey map a switch takes off and puts back (JRN-04). */ -export type LeafletLayerKind = keyof JourneyMapLayerVisibility; +/** + * The layers of the Leaflet journey map a switch takes off and puts back (JRN-04), + * plus the stretches between entries a recording already shows, which follow two. + */ +export type LeafletLayerKind = keyof JourneyMapLayerVisibility | 'coveredConnections'; export type DrawnLayers = Record; -export const NOTHING_DRAWN = (): DrawnLayers => ({ plan: [], recorded: [], entries: [] }); +export const NOTHING_DRAWN = (): DrawnLayers => ({ plan: [], recorded: [], entries: [], coveredConnections: [] }); + +/** Whether a layer of this kind is on the map with these switches. */ +export function isLeafletLayerShown(kind: LeafletLayerKind, visibility: JourneyMapLayerVisibility): boolean { + return kind === 'coveredConnections' ? coveredConnectionsShown(visibility) : visibility[kind]; +} /** * Puts each layer whose switch changed back on the map or takes it off, @@ -20,9 +29,10 @@ export function switchLeafletLayers( ): JourneyMapLayerVisibility { if (!map) return shown; for (const kind of Object.keys(drawn) as LeafletLayerKind[]) { - if (shown[kind] === next[kind]) continue; + const visible = isLeafletLayerShown(kind, next); + if (isLeafletLayerShown(kind, shown) === visible) continue; for (const layer of drawn[kind]) { - if (next[kind]) layer.addTo(map); + if (visible) layer.addTo(map); else layer.remove(); } } diff --git a/client/src/components/Journey/journeyMapConnections.test.ts b/client/src/components/Journey/journeyMapConnections.test.ts new file mode 100644 index 000000000..cb1f08d68 --- /dev/null +++ b/client/src/components/Journey/journeyMapConnections.test.ts @@ -0,0 +1,116 @@ +import type { JourneyTrack } from '@trek/shared'; +import { describe, expect, it } from 'vitest'; +import { coveredConnectionsShown, entryConnections } from './journeyMapConnections'; + +const stop = (lat: number, date: string) => ({ lat, lng: lat, date }); + +function recorded( + date: string, + kind: JourneyTrack['kind'] = 'day_recording', + points: [number, number][] = [ + [1, 1], + [2, 2], + ] +): JourneyTrack { + return { place_id: -1, trip_id: 0, name: date, color: null, points, kind, date }; +} + +describe('entryConnections (#2888)', () => { + it('JMCONN-001: without a recording the whole line stays open, one polyline through every stop', () => { + const result = entryConnections([stop(1, '2026-10-29'), stop(2, '2026-10-29'), stop(3, '2026-10-30')], []); + expect(result.open).toEqual([ + [ + [1, 1], + [2, 2], + [3, 3], + ], + ]); + expect(result.covered).toEqual([]); + }); + + it('JMCONN-002: a recorded day covers the stretches between its entries, the jump into a day without one stays open', () => { + const result = entryConnections( + [stop(1, '2026-10-29'), stop(2, '2026-10-29'), stop(3, '2026-10-30'), stop(4, '2026-10-30')], + [recorded('2026-10-29', 'dawarich_live')] + ); + expect(result.covered).toEqual([ + [ + [1, 1], + [2, 2], + ], + ]); + expect(result.open).toEqual([ + [ + [2, 2], + [3, 3], + [4, 4], + ], + ]); + }); + + it('JMCONN-003: the night between two recorded days is covered, a day in between without one is not', () => { + const tracks = [recorded('2026-10-29'), recorded('2026-10-30'), recorded('2026-11-01')]; + expect(entryConnections([stop(1, '2026-10-29'), stop(2, '2026-10-30')], tracks).open).toEqual([]); + expect(entryConnections([stop(1, '2026-10-30'), stop(2, '2026-11-01')], tracks).covered).toEqual([]); + }); + + it('JMCONN-004: a trip GPX, a line without a day and a recording without points cover nothing', () => { + const stops = [stop(1, '2026-10-29'), stop(2, '2026-10-29')]; + const gpx: JourneyTrack = { ...recorded('2026-10-29'), kind: 'trip_gpx' }; + const undated: JourneyTrack = { ...recorded('2026-10-29'), date: undefined }; + const empty = recorded('2026-10-29', 'day_recording', [[1, 1]]); + expect(entryConnections(stops, [gpx, undated, empty]).covered).toEqual([]); + }); + + it('JMCONN-005: open and covered stretches alternate in the order of the stops', () => { + const result = entryConnections( + [ + stop(1, '2026-10-28'), + stop(2, '2026-10-29'), + stop(3, '2026-10-29'), + stop(4, '2026-10-31'), + stop(5, '2026-10-31'), + ], + [recorded('2026-10-29'), recorded('2026-10-31')] + ); + expect(result.open).toEqual([ + [ + [1, 1], + [2, 2], + ], + [ + [3, 3], + [4, 4], + ], + ]); + expect(result.covered).toEqual([ + [ + [2, 2], + [3, 3], + ], + [ + [4, 4], + [5, 5], + ], + ]); + }); + + it('JMCONN-006: a timestamp is read by its day, and an unreadable date never counts as recorded', () => { + expect( + entryConnections([stop(1, '2026-10-29T08:00:00Z'), stop(2, '2026-10-29')], [recorded('2026-10-29')]).open + ).toEqual([]); + expect(entryConnections([stop(1, 'someday'), stop(2, '2026-10-29')], [recorded('2026-10-29')]).covered).toEqual([]); + }); + + it('JMCONN-007: fewer than two stops make no line', () => { + expect(entryConnections([stop(1, '2026-10-29')], [])).toEqual({ open: [], covered: [] }); + }); +}); + +describe('coveredConnectionsShown', () => { + it('JMCONN-008: stands in only while the entries show and the recording is hidden', () => { + expect(coveredConnectionsShown({ plan: true, recorded: true, entries: true })).toBe(false); + expect(coveredConnectionsShown({ plan: true, recorded: false, entries: true })).toBe(true); + expect(coveredConnectionsShown({ plan: true, recorded: false, entries: false })).toBe(false); + }); +}); diff --git a/client/src/components/Journey/journeyMapConnections.ts b/client/src/components/Journey/journeyMapConnections.ts new file mode 100644 index 000000000..dd15667b3 --- /dev/null +++ b/client/src/components/Journey/journeyMapConnections.ts @@ -0,0 +1,93 @@ +import type { JourneyTrack } from '@trek/shared'; +import { isPlannedTrack, type JourneyMapLayerVisibility } from './journeyMapLayers'; + +/** A point of the line between entries, `[lat, lng]`. */ +type LatLng = [number, number]; + +/** An entry pin as the connection sees it: where it stands and on which day. */ +export interface ConnectionStop { + lat: number; + lng: number; + /** `YYYY-MM-DD`; a longer ISO string is read by its date. */ + date: string; +} + +/** + * The faint line between the entries, cut where the map already shows how the + * traveller got from one to the next. + * + * `open` is every stretch nothing else on the map accounts for, so the + * straight line is the best guess there is. `covered` is every stretch whose + * days were all recorded: the recorded line is the real way, and a straight + * one across it only gets in its way (#2888). It is still handed back, so a + * reader who hides what was recorded gets the guess back for those stretches. + */ +export interface EntryConnections { + open: LatLng[][]; + covered: LatLng[][]; +} + +const DAY_MS = 86_400_000; + +function dayNumber(date: string): number | null { + const time = Date.parse(`${date.slice(0, 10)}T00:00:00Z`); + return Number.isNaN(time) ? null : Math.round(time / DAY_MS); +} + +/** The days a recorded line with points stands for; a trip's GPX is the plan and stands for none. */ +function recordedDays(tracks: readonly JourneyTrack[]): Set { + const days = new Set(); + for (const track of tracks) { + if (isPlannedTrack(track) || !track.date || track.points.length < 2) continue; + const day = dayNumber(track.date); + if (day !== null) days.add(day); + } + return days; +} + +/** + * Whether the way between two stops is on the map already: every day from the + * first stop's to the second's was recorded, the night between two recorded + * days included. One day in between without a recording leaves it open. + */ +function travelled(from: string, to: string, recorded: ReadonlySet): boolean { + const a = dayNumber(from); + const b = dayNumber(to); + if (a === null || b === null) return false; + const first = Math.min(a, b); + const last = Math.max(a, b); + if (last - first + 1 > recorded.size) return false; + for (let day = first; day <= last; day++) { + if (!recorded.has(day)) return false; + } + return true; +} + +/** + * Splits the line through the stops, taken in the order given (the pins' + * order on the map), into the stretches still open and those a recording + * covers. Neighbouring stretches of the same kind stay one line. + */ +export function entryConnections(stops: readonly ConnectionStop[], tracks: readonly JourneyTrack[]): EntryConnections { + const result: EntryConnections = { open: [], covered: [] }; + const recorded = recordedDays(tracks); + let line: LatLng[] | null = null; + let lineCovered = false; + for (let i = 1; i < stops.length; i++) { + const from = stops[i - 1]; + const to = stops[i]; + const covered = travelled(from.date, to.date, recorded); + if (!line || covered !== lineCovered) { + line = [[from.lat, from.lng]]; + (covered ? result.covered : result.open).push(line); + lineCovered = covered; + } + line.push([to.lat, to.lng]); + } + return result; +} + +/** The covered stretches stand in for the recording only while the pins are shown and the recording is not. */ +export function coveredConnectionsShown(visibility: JourneyMapLayerVisibility): boolean { + return visibility.entries && !visibility.recorded; +} diff --git a/client/src/components/Journey/useJourneyDayImport.test.ts b/client/src/components/Journey/useJourneyDayImport.test.ts index 6cc5e2e96..df637b3e9 100644 --- a/client/src/components/Journey/useJourneyDayImport.test.ts +++ b/client/src/components/Journey/useJourneyDayImport.test.ts @@ -115,6 +115,8 @@ describe('useJourneyDayImport', () => { it('FE-JRN-DAYIMPORT-004: the route goes along only when switched on, kept as previewed', async () => { const { result, onKept, onClose } = setup(); await waitFor(() => expect(result.current.route.offer).toBe('keep')); + expect(result.current.mapTracks.length).toBeGreaterThan(0); + expect(result.current.mapTracks.every((track) => track.date === '2026-05-14')).toBe(true); repo.save.mockResolvedValue({ day: buildRecordedDay(), outcome: 'created' }); act(() => result.current.route.toggle()); diff --git a/client/src/components/Journey/useJourneyDayImport.ts b/client/src/components/Journey/useJourneyDayImport.ts index ed28dbc3b..2a5d37df6 100644 --- a/client/src/components/Journey/useJourneyDayImport.ts +++ b/client/src/components/Journey/useJourneyDayImport.ts @@ -155,8 +155,9 @@ function useDayMap( color: dayColor, points: segment.points, kind: 'day_recording', + date, })), - [route, dayColor, t] + [route, dayColor, date, t] ); return { entries, tracks }; } diff --git a/client/src/components/Journey/useJourneyMapLayers.test.ts b/client/src/components/Journey/useJourneyMapLayers.test.ts index 254a6b8ba..1f6259741 100644 --- a/client/src/components/Journey/useJourneyMapLayers.test.ts +++ b/client/src/components/Journey/useJourneyMapLayers.test.ts @@ -202,6 +202,8 @@ describe('useJourneyMapLayers', () => { expect(result.current.tracks.every((track) => track.kind === 'day_recording' && track.color === '#14b8a6')).toBe( true ); + // The day travels as a date of its own, so the map can tell which stretches it covers (#2888). + expect(result.current.tracks.map((track) => track.date)).toEqual(['2026-05-14', '2026-05-14']); expect(result.current.controls.map((c) => c.layer)).toEqual(['recorded', 'entries', 'photos']); expect(result.current.controls[0]).toEqual({ layer: 'recorded', count: 1, state: 'ready' }); }); diff --git a/client/src/components/Journey/useJourneyMapLayers.ts b/client/src/components/Journey/useJourneyMapLayers.ts index c2dbe51d7..cee7e62ef 100644 --- a/client/src/components/Journey/useJourneyMapLayers.ts +++ b/client/src/components/Journey/useJourneyMapLayers.ts @@ -163,6 +163,7 @@ export function useJourneyMapLayers({ color, points, kind: 'day_recording', + date: day.date, })); }), [sources.recordings, colorOfDay, t, locale] @@ -187,7 +188,7 @@ export function useJourneyMapLayers({ [places, stays, visibility, entryIdsOnMap, stayColor] ); - const recordedDays = useMemo(() => new Set(recorded.map((track) => track.name)).size, [recorded]); + const recordedDays = useMemo(() => new Set(recorded.map((track) => track.date ?? track.name)).size, [recorded]); const controls = useMemo( () => buildControls({ diff --git a/client/src/components/Journey/useJourneyRecordings.test.ts b/client/src/components/Journey/useJourneyRecordings.test.ts index a1c3af93c..73fef1272 100644 --- a/client/src/components/Journey/useJourneyRecordings.test.ts +++ b/client/src/components/Journey/useJourneyRecordings.test.ts @@ -47,6 +47,7 @@ describe('useJourneyRecordings', () => { expect(result.current.byDate.get('2026-05-15')?.id).toBe(4); expect(result.current.tracks).toHaveLength(3); expect(result.current.tracks.every((track) => track.kind === 'day_recording')).toBe(true); + expect(result.current.tracks.map((track) => track.date)).toEqual(['2026-05-14', '2026-05-14', '2026-05-15']); // One line per piece: a gap between two pieces is never drawn over. expect(result.current.tracks.slice(0, 2).map((track) => track.points)).toEqual([ WALK_SEGMENT.points, diff --git a/client/src/components/Journey/useJourneyRecordings.ts b/client/src/components/Journey/useJourneyRecordings.ts index 350192c0f..3dfa72f28 100644 --- a/client/src/components/Journey/useJourneyRecordings.ts +++ b/client/src/components/Journey/useJourneyRecordings.ts @@ -105,6 +105,7 @@ export function useJourneyRecordings( color, points: segment.points, kind: 'day_recording', + date: day.date, })); }), [days, colorOf, t, locale] diff --git a/client/src/hooks/useDawarichJournalTrail.test.ts b/client/src/hooks/useDawarichJournalTrail.test.ts index b76bedc37..1ea89febd 100644 --- a/client/src/hooks/useDawarichJournalTrail.test.ts +++ b/client/src/hooks/useDawarichJournalTrail.test.ts @@ -227,6 +227,7 @@ describe('useDawarichJournalTrail', () => { color: DAWARICH_DAY_COLORS[0], points: [[53.5, 10.0], [53.6, 10.1]], kind: 'dawarich_live', + date: DAY_ONE, }, { place_id: -2, @@ -237,6 +238,7 @@ describe('useDawarichJournalTrail', () => { color: DAWARICH_DAY_COLORS[1], points: [[48.1, 11.5], [48.2, 11.6], [48.3, 11.7]], kind: 'dawarich_live', + date: DAY_TWO, }, ]) // The journey map keys its polylines on place_id. Real places are positive, diff --git a/client/src/hooks/useDawarichJournalTrail.ts b/client/src/hooks/useDawarichJournalTrail.ts index af2c473f1..79beadaa7 100644 --- a/client/src/hooks/useDawarichJournalTrail.ts +++ b/client/src/hooks/useDawarichJournalTrail.ts @@ -72,6 +72,7 @@ export function useDawarichJournalTrail( color: segment.color, points: segment.points, kind: 'dawarich_live', + date: segment.date, })), ) setFetchedAt(track.fetchedAt) diff --git a/client/src/pages/journeyDetail/useJourneyDetailRecordings.test.ts b/client/src/pages/journeyDetail/useJourneyDetailRecordings.test.ts index 9ae960097..74771f9a1 100644 --- a/client/src/pages/journeyDetail/useJourneyDetailRecordings.test.ts +++ b/client/src/pages/journeyDetail/useJourneyDetailRecordings.test.ts @@ -25,8 +25,8 @@ const line = (over: Partial): JourneyTrack => ({ const GPX = [line({})]; const LIVE = [ - line({ place_id: -1, trip_id: 0, name: '2026-05-14', kind: 'dawarich_live' }), - line({ place_id: -2, trip_id: 0, name: '2026-05-15', kind: 'dawarich_live' }), + line({ place_id: -1, trip_id: 0, name: 'Live', kind: 'dawarich_live', date: '2026-05-14' }), + line({ place_id: -2, trip_id: 0, name: 'Live', kind: 'dawarich_live', date: '2026-05-15' }), ]; const ITEMS = [{ entry_date: '2026-05-14', dayColor: '#ff0055' }]; @@ -58,7 +58,7 @@ describe('useJourneyDetailRecordings', () => { const kinds = result.current.mapTracks.map((track) => track.kind ?? 'trip_gpx'); expect(kinds).toEqual(['trip_gpx', 'day_recording', 'dawarich_live']); expect(result.current.mapTracks[1]?.color).toBe('#ff0055'); - expect(result.current.mapTracks[2]?.name).toBe('2026-05-15'); + expect(result.current.mapTracks[2]?.date).toBe('2026-05-15'); }); it('FE-JRN-DETAIL-RECORDINGS-002: with nothing kept and no live trail the GPX tracks pass through as they are', async () => { diff --git a/client/src/pages/journeyDetail/useJourneyDetailRecordings.ts b/client/src/pages/journeyDetail/useJourneyDetailRecordings.ts index c2b19afda..daddedb40 100644 --- a/client/src/pages/journeyDetail/useJourneyDetailRecordings.ts +++ b/client/src/pages/journeyDetail/useJourneyDetailRecordings.ts @@ -42,7 +42,7 @@ export function useJourneyDetailRecordings({ const mapTracks = useMemo(() => { if (recordings.tracks.length === 0 && liveTracks.length === 0) return tripTracks; - const live = liveTracks.filter((track) => !recordings.byDate.has(track.name)); + const live = liveTracks.filter((track) => !track.date || !recordings.byDate.has(track.date)); return [...tripTracks, ...recordings.tracks, ...live]; }, [tripTracks, recordings.tracks, recordings.byDate, liveTracks]); diff --git a/shared/src/journey/journey.schema.spec.ts b/shared/src/journey/journey.schema.spec.ts index cb88c6a03..368a9c104 100644 --- a/shared/src/journey/journey.schema.spec.ts +++ b/shared/src/journey/journey.schema.spec.ts @@ -14,6 +14,7 @@ import { journeyEntryVisibilitySchema, journeyCreateToolSchema, journeyUpdateToolSchema, + journeyTrackSchema, } from './journey.schema'; import { describe, it, expect } from 'vitest'; @@ -143,6 +144,20 @@ describe('the entry enums and the range of a read', () => { }); }); +describe('journeyTrackSchema', () => { + const track = { place_id: -1, trip_id: 0, name: 'Day', color: null, points: [[48.1, 11.5]], kind: 'day_recording' }; + + it('JRNSCH-004: a recorded line may name its calendar day, a GPX track leaves it out', () => { + expect(journeyTrackSchema.parse({ ...track, date: '2026-10-29' }).date).toBe('2026-10-29'); + expect(journeyTrackSchema.parse({ ...track, kind: 'trip_gpx' }).date).toBeUndefined(); + }); + + it('JRNSCH-005: refuses a day that is not a calendar day', () => { + expect(journeyTrackSchema.safeParse({ ...track, date: '29. Oktober' }).success).toBe(false); + expect(journeyTrackSchema.safeParse({ ...track, date: '2026-02-30' }).success).toBe(false); + }); +}); + describe('the journey tool arguments', () => { it('JSCHEMA-TOOL-001: a new journey needs a title and refuses what the tool does not take', () => { expect(journeyCreateToolSchema.parse({ title: 'Japan', trip_ids: [5] })).toEqual({ title: 'Japan', trip_ids: [5] }); diff --git a/shared/src/journey/journey.schema.ts b/shared/src/journey/journey.schema.ts index 72a2f0795..be5a9eaeb 100644 --- a/shared/src/journey/journey.schema.ts +++ b/shared/src/journey/journey.schema.ts @@ -392,6 +392,11 @@ export const journeyTrackSchema = z.object({ * which is all GET `:id/tracks` has ever answered. */ kind: z.enum(['trip_gpx', 'day_recording', 'dawarich_live']).optional(), + /** + * The day a recorded line belongs to, so the map can tell which stretches + * between entries it already shows as travelled. A trip's GPX has none. + */ + date: journeyRecordingDateSchema.optional(), }); export type JourneyTrack = z.infer;