mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
3f0eb210d8
A day that runs past midnight used to keep every one of its stops on its own card, so day one ended at 03:12 with three places nobody reaches on day one and day two began with a drive that had already happened. The rail now lays the stops out by the date they are actually reached: when the clock goes backwards between two arrivals the ones after it move to the following day, into a block that says where they came from, and the night drive and its kilometres go with them. It is an arrangement, not a write — the day planner beside it is untouched, nothing reaches the database, and a shorter stay the evening before puts everything back on its own. The map follows the same arrangement, because a boundary drawn in the rail and not on the map is two answers to one question. Day one now ends where its last reachable stop is and the line, the legs and the totals are cut to match. Two switches in the driving settings, off by default. "Connect the days" routes the gap between one day's last stop and the next day's first, which is real driving the rail has never drawn, and counts it. "A colour per day" then puts the days back into a line that has become continuous, and tints each day header to match. Collapsing a day from its header takes its route and its places off the map as well as out of the rail, and the categories in the corridor panel fold the same way. The refuel band is one full-width red band with the pump as its button, the results are chips, and the driving dialog is two columns of panels with the figures on one right edge. Also fixes a via that would not adjust the route once dragged. Five separate causes, all measured rather than guessed: leg threading used the card index instead of the owning day's, the anchor lookup returned chain indices, it filtered by the card rather than by the anchor's owner, seams were routed without their vias, and a seam once routed was never re-fetched.
105 lines
3.9 KiB
TypeScript
105 lines
3.9 KiB
TypeScript
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
|
import { Marker, Tooltip, useMap } from 'react-leaflet'
|
|
import L from 'leaflet'
|
|
import { useTranslation } from '../../i18n/TranslationContext'
|
|
import { useStableVias } from './viaMarkerState'
|
|
import type { RoadtripVia } from '@trek/shared'
|
|
|
|
/**
|
|
* The handles that shape a drive.
|
|
*
|
|
* A via is deliberately small and unnumbered: it is not a stop, and anything that looks
|
|
* like one would read as a place on the itinerary. Dragging it redraws the route through
|
|
* the new spot, which is the whole interaction — the same one a paper map gets when you
|
|
* put a finger on the road you actually want to take.
|
|
*/
|
|
const VIA_ICON = L.divIcon({
|
|
className: '',
|
|
html: `<span style="
|
|
display:block;width:12px;height:12px;border-radius:9999px;
|
|
background:#0a84ff;border:2.5px solid #ffffff;
|
|
box-shadow:0 1px 4px rgba(0,0,0,.45);cursor:grab;
|
|
"></span>`,
|
|
iconSize: [12, 12],
|
|
iconAnchor: [6, 6],
|
|
})
|
|
|
|
interface RoadtripViaMarkersProps {
|
|
viasByDay: Record<number, RoadtripVia[]>
|
|
onMoveVia?: (dayId: number, id: number, lat: number, lng: number) => void
|
|
onRemoveVia?: (dayId: number, id: number) => void
|
|
}
|
|
|
|
/** Leaflet's half; the GL renderers draw their own inside `MapViewGL`. */
|
|
/** Below this the handles are not drawn. Shared with the GL renderer's own copy. */
|
|
const VIA_MIN_ZOOM = 9
|
|
|
|
export default function RoadtripViaMarkers({
|
|
viasByDay, onMoveVia, onRemoveVia,
|
|
}: RoadtripViaMarkersProps): React.ReactElement | null {
|
|
const { t } = useTranslation()
|
|
const vias = useStableVias(viasByDay)
|
|
// Read through a ref so the handlers below can stay the same objects across renders.
|
|
// Both callbacks are rebuilt on every render of the planner, and react-leaflet compares
|
|
// its props by identity: a fresh `eventHandlers` unbinds and rebinds every listener,
|
|
// and a fresh `position` array calls `setLatLng` on every marker. Doing either while a
|
|
// drag is in flight is how a freshly placed via lost its first drag.
|
|
const handlersRef = useRef({ onMoveVia, onRemoveVia })
|
|
handlersRef.current = { onMoveVia, onRemoveVia }
|
|
const draggable = !!onMoveVia
|
|
/**
|
|
* Zoomed out, the handles go away.
|
|
*
|
|
* A via is a handle for a few hundred metres of road, and at a continental zoom a whole
|
|
* day's worth collapses into a cluster of dots over one town — not something anybody can
|
|
* aim at, and a drag there moves the route by kilometres per pixel. Below this the drive
|
|
* is read, not shaped.
|
|
*/
|
|
const map = useMap()
|
|
const [zoomedIn, setZoomedIn] = useState(() => map.getZoom() >= VIA_MIN_ZOOM)
|
|
useEffect(() => {
|
|
const update = () => setZoomedIn(map.getZoom() >= VIA_MIN_ZOOM)
|
|
update()
|
|
map.on('zoomend', update)
|
|
return () => { map.off('zoomend', update) }
|
|
}, [map])
|
|
|
|
const markers = useMemo(() => vias.map(via => ({
|
|
via,
|
|
position: [via.lat, via.lng] as [number, number],
|
|
handlers: {
|
|
dragend: (e: { target: { getLatLng: () => { lat: number; lng: number } } }) => {
|
|
const at = e.target.getLatLng()
|
|
handlersRef.current.onMoveVia?.(via.day_id, via.id, at.lat, at.lng)
|
|
},
|
|
// Right-click rather than a delete handle: a 12px dot has no room for one,
|
|
// and the same gesture removes things elsewhere on the map.
|
|
contextmenu: (e: { originalEvent: MouseEvent }) => {
|
|
e.originalEvent.preventDefault()
|
|
handlersRef.current.onRemoveVia?.(via.day_id, via.id)
|
|
},
|
|
},
|
|
})), [vias])
|
|
|
|
if (!markers.length || !zoomedIn) return null
|
|
|
|
return (
|
|
<>
|
|
{markers.map(({ via, position, handlers }) => (
|
|
<Marker
|
|
key={`via-${via.id}`}
|
|
position={position}
|
|
icon={VIA_ICON}
|
|
draggable={draggable}
|
|
zIndexOffset={400}
|
|
eventHandlers={handlers}
|
|
>
|
|
<Tooltip direction="top" offset={[0, -8]} opacity={1} className="map-tooltip">
|
|
{t('roadtrip.via.hint')}
|
|
</Tooltip>
|
|
</Marker>
|
|
))}
|
|
</>
|
|
)
|
|
}
|