mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
feat(roadtrip): remember which track a day follows
The vias already made the day follow the road; nothing said which road. After a reload the badge could only say "shaped by hand", and re-fitting after the stops changed meant finding the track again by eye. One row per day, written in the same transaction as the chain it describes: a day claiming to follow a road whose vias never landed would be worse than a day claiming nothing. Three states rather than two, which is why the field is an object and not an id — absent leaves whatever the day followed alone, null says it follows nothing, an object records the road it now takes. A bare nullable id could not tell "do not touch this" apart from "forget it", and a hand-dragged via on the same day must not wipe the label. The foreign key is the point: delete the imported track and the label cascades with it, so no day is left claiming to follow a line nobody can see. The vias stay, because they are the drive. Both write paths check that the place is a track of this trip before it becomes a label — permission alone would let another trip's place id, or an ordinary place, become a name that can never be drawn. Also clears three reliability findings the scanner raised on this branch: two default sorts given an explicit comparator, and a trailing-slash regex the scanner is right to call backtracking, walked instead.
This commit is contained in:
@@ -50,6 +50,16 @@ export interface FollowTrack {
|
||||
clear: () => Promise<void>
|
||||
/** How many vias the open day carries — what makes clearing worth offering. */
|
||||
viaCount: number
|
||||
/**
|
||||
* The track the open day follows, if it follows one.
|
||||
*
|
||||
* Read back from the day rather than remembered from the run that applied it: the point
|
||||
* of storing it at all is that it survives a reload, and a field that only ever held
|
||||
* what this session did would look right until somebody refreshed.
|
||||
*/
|
||||
current: { name: string; strayKm: number | null } | null
|
||||
/** The name of the track each day follows, for the rail's badge. */
|
||||
namesByDay: Record<number, string>
|
||||
/**
|
||||
* Whether the trip holds a track at all.
|
||||
*
|
||||
@@ -185,7 +195,9 @@ export function useFollowTrack(
|
||||
if (controller.signal.aborted) { setBusy(false); return }
|
||||
|
||||
try {
|
||||
await vias.addMany(dayId, plan.vias, plan.legs)
|
||||
// The chain and the name it came from, in one write: a day claiming to follow a
|
||||
// road whose vias never landed would be worse than a day claiming nothing.
|
||||
await vias.addMany(dayId, plan.vias, plan.legs, { place_id: trackId, stray_km: plan.strayKm })
|
||||
setOutcome({ vias: plan.vias.length, strayKm: plan.strayKm, capped: plan.capped })
|
||||
} catch {
|
||||
setError('save')
|
||||
@@ -195,6 +207,29 @@ export function useFollowTrack(
|
||||
}, [tracks, dayId, stops, busy, tripId, vias])
|
||||
|
||||
const viaCount = dayId === null ? 0 : (vias.byDay[dayId]?.length ?? 0)
|
||||
|
||||
/**
|
||||
* The name behind each day's stored track id.
|
||||
*
|
||||
* A lookup over `places` rather than a parse: naming a line costs a row, and the rail
|
||||
* asks for this on every render.
|
||||
*/
|
||||
const namesByDay = useMemo(() => {
|
||||
const names: Record<number, string> = {}
|
||||
const byId = new Map(places.map(p => [p.id, p]))
|
||||
for (const [day, track] of Object.entries(vias.trackByDay)) {
|
||||
const place = byId.get(track.place_id)
|
||||
if (place?.name) names[Number(day)] = place.name
|
||||
}
|
||||
return names
|
||||
}, [places, vias.trackByDay])
|
||||
|
||||
const current = useMemo(() => {
|
||||
if (dayId === null) return null
|
||||
const track = vias.trackByDay[dayId]
|
||||
const name = namesByDay[dayId]
|
||||
return track && name ? { name, strayKm: track.stray_km } : null
|
||||
}, [dayId, vias.trackByDay, namesByDay])
|
||||
// A cheap look at the column, not a parse: this runs on every planner render, and the
|
||||
// question is only whether anything at all was ever imported.
|
||||
const available = useMemo(() => places.some(p => (p.route_geometry?.length ?? 0) > 2), [places])
|
||||
@@ -206,7 +241,8 @@ export function useFollowTrack(
|
||||
try {
|
||||
// Every leg of the day, so nothing is left behind on a leg the track never reached.
|
||||
const legs = Array.from({ length: Math.max(0, stops.length - 1) }, (_, i) => i)
|
||||
await vias.addMany(dayId, [], legs)
|
||||
// null rather than nothing: dropping the points is also dropping the claim.
|
||||
await vias.addMany(dayId, [], legs, null)
|
||||
setOutcome({ vias: 0, strayKm: 0, capped: false })
|
||||
} catch {
|
||||
setError('save')
|
||||
@@ -215,5 +251,5 @@ export function useFollowTrack(
|
||||
}
|
||||
}, [dayId, busy, stops.length, vias])
|
||||
|
||||
return { dayId, open, close, tracks, busy, round, error, outcome, apply, clear, viaCount, available }
|
||||
return { dayId, open, close, tracks, busy, round, error, outcome, apply, clear, viaCount, available, current, namesByDay }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user