mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
5b4a7e82ff
The trip planner and the journal get a "From Dawarich" list: the stays recorded for those dates, each one a row that can be dismissed or turned into a place or an entry after a review step that shows time, name, note and day. A place that came from a recording carries the Dawarich mark next to its name so its origin stays visible afterwards. The recorded route lies over the trip and journal maps as an overlay, coloured per day and cut along the reader's own timezone rather than UTC. It is fetched for the window on screen and never stored. The Atlas gets its own panel beside the map for the countries and cities the recordings prove, and the wish list is ticked off there, on the day of the visit, undoably. The phone has its own take on all of it, in the mobile shell's colours and with native sheets.
105 lines
3.7 KiB
TypeScript
105 lines
3.7 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import type { DawarichTrack } from '@trek/shared'
|
|
import { dawarichRepo } from '../../repo/dawarichRepo'
|
|
import { useAddonStore } from '../../store/addonStore'
|
|
import { isEffectivelyOffline, onNetworkModeChange } from '../../sync/networkMode'
|
|
|
|
export type DawarichTrailStatus = 'idle' | 'loading' | 'ready' | 'empty' | 'unavailable' | 'offline'
|
|
|
|
export interface DawarichTrailState {
|
|
track: DawarichTrack | null
|
|
status: DawarichTrailStatus
|
|
/** ISO-8601 of the last fetch that actually returned something. */
|
|
fetchedAt: string | null
|
|
reload: () => void
|
|
}
|
|
|
|
/**
|
|
* The recorded route for a trip, fetched while the overlay is on.
|
|
*
|
|
* Called **once** in `MapViewAuto` and mirrored into both renderers as a prop —
|
|
* the same shape `useRoadtripHazards` uses, and for the same reason: two
|
|
* renderers each fetching would double the load on somebody's self-hosted
|
|
* instance and could draw two different answers.
|
|
*
|
|
* Refreshes on a timer while the layer is on, so a trip in progress catches up
|
|
* without anyone reloading the page. Two minutes rather than seconds: this
|
|
* reaches across the network to a server the user runs, and the server caches
|
|
* for a minute anyway, so a tighter loop would buy nothing and cost them.
|
|
*/
|
|
const REFRESH_MS = 120_000
|
|
|
|
export function useDawarichTrail(
|
|
tripId: number | undefined,
|
|
enabled: boolean,
|
|
range?: { from?: string; to?: string },
|
|
): DawarichTrailState {
|
|
const addonEnabled = useAddonStore(s => s.isEnabled)
|
|
const active = enabled && !!tripId && addonEnabled('dawarich')
|
|
|
|
const [track, setTrack] = useState<DawarichTrack | null>(null)
|
|
const [status, setStatus] = useState<DawarichTrailStatus>('idle')
|
|
const [fetchedAt, setFetchedAt] = useState<string | null>(null)
|
|
const [reloadToken, setReloadToken] = useState(0)
|
|
|
|
// Kept in a ref so changing the window does not re-create the effect's
|
|
// identity on every render of the parent — the object literal would.
|
|
const rangeRef = useRef(range)
|
|
rangeRef.current = range
|
|
const rangeKey = `${range?.from ?? ''}|${range?.to ?? ''}`
|
|
|
|
const reload = useCallback(() => setReloadToken(token => token + 1), [])
|
|
|
|
useEffect(() => {
|
|
if (!active || !tripId) {
|
|
setTrack(null)
|
|
setStatus('idle')
|
|
return
|
|
}
|
|
|
|
let controller: AbortController | null = null
|
|
let cancelled = false
|
|
|
|
const load = () => {
|
|
controller?.abort()
|
|
controller = new AbortController()
|
|
const signal = controller.signal
|
|
|
|
if (isEffectivelyOffline()) {
|
|
setStatus('offline')
|
|
return
|
|
}
|
|
// The previous line stays on screen while the next one loads: blanking it
|
|
// makes a refresh look like the recording disappeared.
|
|
setStatus(current => (current === 'ready' ? 'ready' : 'loading'))
|
|
|
|
dawarichRepo
|
|
.tripTrack(tripId, rangeRef.current, signal)
|
|
.then(result => {
|
|
if (cancelled || signal.aborted) return
|
|
setTrack(result)
|
|
setFetchedAt(result.fetchedAt)
|
|
setStatus(result.days.length > 0 ? 'ready' : 'empty')
|
|
})
|
|
.catch(() => {
|
|
if (cancelled || signal.aborted) return
|
|
// Keep whatever was drawn. A line from two minutes ago is more useful
|
|
// than an empty map, and the status is what says it may be stale.
|
|
setStatus(isEffectivelyOffline() ? 'offline' : 'unavailable')
|
|
})
|
|
}
|
|
|
|
load()
|
|
const timer = window.setInterval(load, REFRESH_MS)
|
|
const unsubscribe = onNetworkModeChange(load)
|
|
return () => {
|
|
cancelled = true
|
|
controller?.abort()
|
|
window.clearInterval(timer)
|
|
unsubscribe()
|
|
}
|
|
}, [active, tripId, rangeKey, reloadToken])
|
|
|
|
return { track: active ? track : null, status: active ? status : 'idle', fetchedAt, reload }
|
|
}
|