mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
fix(map): keep the overview frame when the trip is edited
This commit is contained in:
@@ -40,6 +40,14 @@ export function useTripRouteOverview(
|
||||
const distanceUnit = useSettingsStore(s => s.settings.distance_unit)
|
||||
const [result, setResult] = useState<TripRouteOverview>(EMPTY)
|
||||
const abortRef = useRef<AbortController | null>(null)
|
||||
// The map refits its camera whenever a NEW focusPoints array arrives, so the
|
||||
// array's identity is a camera command. A content edit — a place dropped onto
|
||||
// a day — re-plans the route, and if that published a fresh frame the map
|
||||
// would yank itself out from under the edit every time. A new frame is only
|
||||
// published when the overview is (re)activated or another trip loads; edits
|
||||
// while it is on reuse the reference and the camera stays where the user put it.
|
||||
const frameRef = useRef<{ tripId: number | null; points: [number, number][] } | null>(null)
|
||||
const wasEnabled = useRef(false)
|
||||
|
||||
const plan = useMemo(
|
||||
() => (enabled
|
||||
@@ -59,7 +67,12 @@ export function useTripRouteOverview(
|
||||
|
||||
useEffect(() => {
|
||||
abortRef.current?.abort()
|
||||
if (!plan.length) { setResult(EMPTY); return }
|
||||
if (!plan.length) {
|
||||
setResult(EMPTY)
|
||||
// Off (or nothing to draw): the next activation publishes a fresh frame.
|
||||
wasEnabled.current = false
|
||||
return
|
||||
}
|
||||
|
||||
const controller = new AbortController()
|
||||
abortRef.current = controller
|
||||
@@ -67,7 +80,13 @@ export function useTripRouteOverview(
|
||||
// Straight lines first so the shape of the trip is on screen immediately, then the
|
||||
// real roads replace them — the same two-step the single-day route draws with.
|
||||
const first = summariseTripRoute(assembleTripRoute(plan, emptyAnswers(plan)))
|
||||
setResult({ ...first, loading: true })
|
||||
// Reframe only on activation or a trip switch, never on a content edit (see
|
||||
// frameRef above).
|
||||
const reframe = frameRef.current === null || frameRef.current.tripId !== tripId || !wasEnabled.current
|
||||
if (reframe) frameRef.current = { tripId, points: first.focusPoints }
|
||||
wasEnabled.current = true
|
||||
const frame = frameRef.current!.points
|
||||
setResult({ ...first, focusPoints: frame, loading: true })
|
||||
|
||||
// Published leg by leg: the legs go to the router one at a time with a pause between
|
||||
// them, so a cold trip takes a second per leg, and roads that fill in as they answer
|
||||
@@ -75,13 +94,15 @@ export function useTripRouteOverview(
|
||||
//
|
||||
// The frame is the exception. A fresh `focusPoints` array is what tells the map to
|
||||
// fit the camera, and a fit every second would take the map back from wherever the
|
||||
// reader has panned to. The frame set on the straight lines therefore holds until
|
||||
// the round is over, and only the finished result brings a new one, exactly the two
|
||||
// fits the overview made when every answer landed at once.
|
||||
// reader has panned to. The frame set on the straight lines therefore holds while
|
||||
// the round answers; a reframe round ends on the routed roads (the two fits the
|
||||
// overview has always made on activation), an edit round ends on the frame it
|
||||
// started with and the camera never moves.
|
||||
const publish = (routed: TripRouteAnswers, loading: boolean): void => {
|
||||
if (controller.signal.aborted) return
|
||||
const next = summariseTripRoute(assembleTripRoute(plan, routed))
|
||||
setResult({ ...next, focusPoints: loading ? first.focusPoints : next.focusPoints, loading })
|
||||
if (!loading && reframe) frameRef.current = { tripId, points: next.focusPoints }
|
||||
setResult({ ...next, focusPoints: loading ? frame : frameRef.current!.points, loading })
|
||||
}
|
||||
routeTripLegs(plan, { tripId, signal: controller.signal, onAnswer: routed => publish(routed, true) })
|
||||
.then(routed => publish(routed, false))
|
||||
|
||||
Reference in New Issue
Block a user