feat(roadtrip): lay a whole chain of via points in one write

The single-via route is right for a detour dragged by hand and wrong for
anything that derives its anchors from a line. Pinning a day onto a recorded
track or a signed scenic road means a couple of dozen points, and one request
each re-routes the entire trip once per point, spaced by the routing host's own
rate limit — twenty anchors would spend half a minute drawing routes nobody
asked to see.

replace_legs names the legs to clear rather than taking a flag for the day, and
that is the whole difference: a traveller who bent two legs by hand and then
adopts a scenic road for a third has not asked to lose the two.

The tools follow in the same change, as parity requires. Undoing a detour picks
this up too, where the vias of one leg were deleted one request at a time.
This commit is contained in:
Maurice
2026-09-04 21:21:53 +02:00
committed by Maurice
parent ba7bd1dd91
commit 67163e7224
9 changed files with 203 additions and 5 deletions
+8
View File
@@ -62,6 +62,7 @@ import {
type PluginActionResult,
type PluginInstallRequest,
RoadtripVia,
RoadtripViaBatchRequest,
RoadtripViaCreateRequest,
RoadtripViaReanchorRequest,
RoadtripViaUpdateRequest,
@@ -1227,6 +1228,13 @@ export const roadtripApi = {
apiClient.get(`/trips/${tripId}/roadtrip/vias`).then(r => r.data as { vias: RoadtripVia[] }),
addVia: (tripId: number | string, dayId: number | string, body: RoadtripViaCreateRequest) =>
apiClient.post(`/trips/${tripId}/roadtrip/days/${dayId}/vias`, body).then(r => r.data as { via: RoadtripVia }),
/**
* Lay a chain of vias on one day in one write. Anything that derives its anchors from a
* line produces dozens of them, and one request each would re-route the whole trip once
* per point at better than a second apart.
*/
addVias: (tripId: number | string, dayId: number | string, body: RoadtripViaBatchRequest) =>
apiClient.post(`/trips/${tripId}/roadtrip/days/${dayId}/vias/batch`, body).then(r => r.data as { vias: RoadtripVia[] }),
/**
* Re-pin a day's vias in one write, after its stops changed shape. One request, not one
* per via: the anchors are only correct as a set.
@@ -7,6 +7,14 @@ export interface RoadtripVias {
/** Every via of the trip, keyed by day. */
byDay: Record<number, RoadtripVia[]>
add: (dayId: number, afterOrderIndex: number, lat: number, lng: number) => Promise<void>
/**
* Lay a chain of vias on one day, optionally clearing the legs it fills first.
*
* One request and one reload for the whole chain. `add` per point would trigger a full
* trip re-route between each one, spaced by the routing host's rate limit, so a
* twenty-anchor track would spend half a minute drawing routes nobody asked to see.
*/
addMany: (dayId: number, vias: { after_order_index: number; lat: number; lng: number }[], replaceLegs?: number[]) => Promise<void>
move: (dayId: number, id: number, lat: number, lng: number) => Promise<void>
remove: (dayId: number, id: number) => Promise<void>
/**
@@ -61,6 +69,19 @@ export function useRoadtripVias(tripId: number | string | null, active: boolean)
await reload()
}, [tripId, reload])
const addMany = useCallback(async (
dayId: number,
vias: { after_order_index: number; lat: number; lng: number }[],
replaceLegs?: number[],
) => {
if (!tripId) return
// An empty chain with nothing to clear is not a write. It is a real call though, from
// a track that thinned down to nothing on a very short day.
if (!vias.length && !replaceLegs?.length) return
await roadtripApi.addVias(tripId, dayId, { vias, replace_legs: replaceLegs })
await reload()
}, [tripId, reload])
const move = useCallback(async (dayId: number, id: number, lat: number, lng: number) => {
if (!tripId) return
await roadtripApi.moveVia(tripId, dayId, id, { lat, lng })
@@ -79,5 +100,5 @@ export function useRoadtripVias(tripId: number | string | null, active: boolean)
await reload()
}, [tripId, reload])
return { byDay, add, move, remove, reanchor }
return { byDay, add, addMany, move, remove, reanchor }
}
@@ -968,8 +968,9 @@ export function useTripPlanner() {
const day = roadtripRoutes.days.find(d => d.dayId === open.dayId)
const stop = day?.stops[open.index]
const dayIndex = stop && day ? day.stops.indexOf(stop) : -1
const existing = (roadtripVias.byDay[open.dayId] ?? []).filter(v => v.after_order_index === dayIndex)
for (const via of existing) await roadtripVias.remove(open.dayId, via.id).catch(() => {})
// Clearing the leg in one write. One delete per via meant a full trip re-route
// between each of them, so undoing a detour with three vias drew three routes.
if (dayIndex >= 0) await roadtripVias.addMany(open.dayId, [], [dayIndex]).catch(() => {})
routeAlternatives.close()
return
}