Files
TREK/client/src/hooks/useDawarichJournalTrail.ts
T
Maurice 65bb2d95eb feat(journey): keep the route recorded on a journey day from Dawarich, previewed first, with its gaps
A day header in the journey timeline now carries the Dawarich mark for an
editor whose Dawarich answers (dimmed with the reason while offline). It opens
a dialog that names the journey and the day and reads that one local day from
the caller's own connection: a sketch of the route, distance, first and last
time, pieces, gaps without recording, modes and whether tracks or thinned
points answered. The day is cut in the zone Dawarich records in where its
answer shows one, else in this device's, and a picker cuts it in any other.
Nothing is kept before "Keep route". On a phone the same sheet opens from the
journey's More options for the day of the card shown.

What is kept is the preview that was seen: the save reads the day again and
refuses with recording_changed when its hash moved, and the dialog shows the
new version. The same day taken over twice keeps one version and announces
nothing; a different version is replaced only on "Replace", never touching an
entry or text. A failed read throws before any write, so the kept version
stays. Gaps stay gaps: points are split where silence ends elsewhere, and no
day is joined to the one before. The dialog says how many other people will
see the route; editors may keep their own recording.

Kept days show in the day header (distance, whose recording, since when,
still running) and on the desktop and phone journey maps in the colour of
their day's pins, winning over the live trail of their date, and stay drawn
without Dawarich. A trip's GPX track is now drawn dashed as the plan, recorded
routes solid. Shared links do not show recorded days. Remove route asks first
and works without a connection.

Server: migration 046160 adds journey_recorded_days (one row per journey,
local day and source, segments as JSON, zone, window, fetch time, hash,
importer kept past their account). The journey domain owns the rows through
JourneyRecordedDaysService; the new journey-recordings domain takes a day
over through DawarichTracksService.fetchDay, which reads past the cache, asks
a window wide enough for every zone and refuses with DomainErrors. REST under
/api/journeys/:id/recordings (list, preview, PUT and DELETE /:date) with
@trek/shared contracts, and MCP list_journey_recorded_days,
preview_journey_recorded_day, save_journey_recorded_day and
delete_journey_recorded_day on the same service and access. A new user event
journey:recording:updated keeps every open map current. JourneyTrack gains an
optional kind. All 27 locales carry the strings; the Journey and Dawarich wiki
pages and the MCP tool list say what it does.
2026-10-21 15:05:21 +02:00

97 lines
3.5 KiB
TypeScript

import { useEffect, useMemo, useState } from 'react'
import type { JourneyTrack } from '@trek/shared'
import { dawarichRepo } from '../repo/dawarichRepo'
import { useAddonStore } from '../store/addonStore'
import { trailSegments } from '../components/Map/dawarichTrail'
import { isEffectivelyOffline } from '../sync/networkMode'
/**
* The recorded route for a journal, shaped as the journey map's own tracks.
*
* The journey map already draws `JourneyTrack[]` — several separate polylines,
* each with its own colour and a white casing, described in that file as
* reading "as a recorded route rather than as the dashed line that merely
* connects entries in time order". That is exactly what this is, so it rides
* the existing layer instead of adding a third one.
*
* It is deliberately NOT the map's `trail` prop: that draws a single polyline,
* and a day's recording is several disconnected pieces — joining them would
* paint straight lines across gaps the traveller never travelled.
*
* The synthetic `place_id` is negative so it can never collide with a real
* place's, which is what the map keys its lines on.
*/
export function useDawarichJournalTrail(
/** Dates the journal covers, from its trips. */
dates: Set<string>,
enabled: boolean,
): { tracks: JourneyTrack[]; fetchedAt: string | null; status: 'idle' | 'loading' | 'ready' | 'unavailable' | 'offline' } {
const addonEnabled = useAddonStore(s => s.isEnabled)
const active = enabled && addonEnabled('dawarich') && dates.size > 0
const [tracks, setTracks] = useState<JourneyTrack[]>(EMPTY)
const [fetchedAt, setFetchedAt] = useState<string | null>(null)
const [status, setStatus] = useState<'idle' | 'loading' | 'ready' | 'unavailable' | 'offline'>('idle')
// The window is the journal's own span. A Set has no order, so it is sorted
// here rather than trusting insertion order.
const window = useMemo(() => {
if (dates.size === 0) return null
// ISO dates: lexicographic order is chronological, and staying locale-independent
// keeps the window identical on every runtime.
const sorted = [...dates].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
return { from: `${sorted[0]}T00:00:00Z`, to: `${sorted[sorted.length - 1]}T23:59:59Z` }
}, [dates])
useEffect(() => {
if (!active || !window) {
setTracks(EMPTY)
setStatus('idle')
return
}
if (isEffectivelyOffline()) {
setTracks(EMPTY)
setStatus('offline')
return
}
const controller = new AbortController()
let cancelled = false
setStatus('loading')
dawarichRepo
.windowTrack(window.from, window.to, controller.signal)
.then(track => {
if (cancelled) return
const segments = trailSegments(track)
setTracks(
segments.map((segment, index): JourneyTrack => ({
place_id: -(index + 1),
trip_id: 0,
name: segment.date,
color: segment.color,
points: segment.points,
kind: 'dawarich_live',
})),
)
setFetchedAt(track.fetchedAt)
setStatus('ready')
})
.catch(() => {
if (cancelled) return
setTracks(EMPTY)
setStatus(isEffectivelyOffline() ? 'offline' : 'unavailable')
})
return () => {
cancelled = true
controller.abort()
}
}, [active, window])
return { tracks: active ? tracks : EMPTY, fetchedAt, status }
}
/** Stable empty identity: a fresh literal would re-render the map on every pass. */
const EMPTY: JourneyTrack[] = []