mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
188953158a
A stay, a name, an importance or a protection set in a stop's dialog no longer plans a city walk's route again: the walk stays ready to save, its time overview follows at once, and one undo takes the change back without routing either. A stop moved or added plans the route again as before. A walk saves no breaks, so a cafe never counts as a stay and as a break at once, and a break left from another type neither counts nor holds its save up. The planner and the tour detail show a walk's walking time, its stays and their total apart and in the same words: 45 minutes of walking, a museum of 90 minutes and a cafe of 30 minutes make 2 h 45 min. A stay not entered yet makes the stays and the total at least what is known, stays nobody entered read not known yet, and stays entered as 0 read 0 min. A total entered by hand keeps its own label, and the computed total shows beside it. The head of the tour detail, the day row on the desktop and the phone, the tour summary and the library of other trips show that same total, with a ≥ where a stay is missing. Their time on the move is called walking, and the summary says a walk is planned with its stays rather than breaks. The tour list and the library carry the minutes a walk's stops are entered with and how many have no stay yet, counted from the walk's points, so the way back of a loop is no stop and its start's stay counts once. A loop run the other way keeps its start as the stop there, with its key, name and stay. The planner's waypoint list keeps the rows of a leg in a file of their own, and the tour detail its name heading in a part of its own, so both stay within their size limits.
885 lines
42 KiB
TypeScript
885 lines
42 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
import { hikingPreferencesOf, isClosedLoop, paddleSpeedKmhSchema, tourWebsiteSchema, type BikeProfile, type HikingPreferences, type PlannableTourType, type TourCreateResponse, type TourFixedLeg, type TourListItem, type TourMaxHikingDifficulty } from '@trek/shared'
|
|
import { paddleRouteRepo } from '../../../repo/paddleRouteRepo'
|
|
import { tourRepo } from '../../../repo/tourRepo'
|
|
import { randomId } from '../../../utils/randomId'
|
|
import { analyzeRouteGeometry, type RouteProfileFocus } from '../../../utils/routeGeometry'
|
|
import { closeLoop, reverseRoute, toggleFreeLeg, withPointAppended } from './tourRouteActions'
|
|
import { walkTimes, walkTimesOf } from './stationModel'
|
|
import { hasCompleteElevation, useTourRoute } from './useTourRoute'
|
|
import { normalizePlannerWaypoints, plannerWaypointsOf, ridablePoints, routeInputsKey, sameWaypointSnapshot, savedWaypointsOf, unlinkedPoint, withSavedKeys, type TourPlannerWaypoint } from './tourPlannerWaypoints'
|
|
import { openedTourFields, tourPlannerFacts } from './tourPlannerFacts'
|
|
import { usePlannerHistory } from './usePlannerHistory'
|
|
import { useTourDraftStorage } from './useTourDraftStorage'
|
|
import { useTourRevisionGuard } from './useTourRevisionGuard'
|
|
import { useRouteShaping } from './useRouteShaping'
|
|
import { useTourMapView } from './useTourMapView'
|
|
import type { DraftPayload } from './tourPlannerDraft'
|
|
import { paddleEstimateOf } from './paddlePlanOf'
|
|
import { adoptChangedPoints } from './paddleRouting'
|
|
import { NO_PADDLE_FIELDS, paddleFieldsOf, usePaddleFields, type PaddleFieldValues } from './usePaddleFields'
|
|
import { useTourPermissions, type TourPermissionProps } from '../useTourPermissions'
|
|
|
|
/** What a tour planned from an import starts with. */
|
|
export interface DerivedTourDraft {
|
|
name: string
|
|
description?: string | null
|
|
website?: string | null
|
|
difficulty: TourMaxHikingDifficulty
|
|
points: Array<{ lat: number; lng: number }>
|
|
/** The import the points were taken from. */
|
|
sourcePlaceId: number
|
|
/**
|
|
* Plan the import over in place, keeping its id, days and notes (the server
|
|
* keeps the recorded track to go back to), rather than as a new tour beside it.
|
|
*/
|
|
inPlace: boolean
|
|
/** P2C-03: the import's revision, which a save planned over in place names. */
|
|
revision?: number
|
|
/** A paddle import stays a paddle tour, with its boat and water, and a bike import a bike tour with its profile. */
|
|
tourType?: PlannableTourType
|
|
paddle?: PaddleFieldValues
|
|
bikeProfile?: BikeProfile
|
|
}
|
|
|
|
export type TourPlannerStatus =
|
|
| 'empty'
|
|
| 'dirty'
|
|
| 'routing'
|
|
| 'routing-failed'
|
|
| 'enriching-elevation'
|
|
| 'elevation-failed'
|
|
| 'ready'
|
|
| 'saving'
|
|
| 'saved'
|
|
|
|
/** One step of the history: the route, and what the route depends on. */
|
|
interface HistoryEntry {
|
|
waypoints: TourPlannerWaypoint[]; difficulty: TourMaxHikingDifficulty; preferences: HikingPreferences | null; bikeProfile: BikeProfile
|
|
tourType: PlannableTourType
|
|
}
|
|
|
|
/** CW-02: two steps plan the same route: their points alike but for a station's own fields, and the same choices. */
|
|
function sameRoute(a: HistoryEntry, b: HistoryEntry): boolean {
|
|
const { waypoints: pointsA, ...choicesA } = a
|
|
const { waypoints: pointsB, ...choicesB } = b
|
|
return routeInputsKey(pointsA) === routeInputsKey(pointsB) && JSON.stringify(choicesA) === JSON.stringify(choicesB)
|
|
}
|
|
|
|
/** Where a new tour was planned from; an edit leaves it as it was saved. */
|
|
function derivedFromField(placeId: number | null, derivedFrom: number | null): { derived_from_place_id?: number } {
|
|
return placeId === null && derivedFrom !== null ? { derived_from_place_id: derivedFrom } : {}
|
|
}
|
|
|
|
function trimmedOrNull(value: string): string | null {
|
|
return value.trim() || null
|
|
}
|
|
|
|
function parsedWebsiteOrNull(value: string) {
|
|
return value.trim() ? tourWebsiteSchema.parse(value) : null
|
|
}
|
|
|
|
export interface UseTourPlannerOptions extends TourPermissionProps {
|
|
tripId: number | string
|
|
active?: boolean
|
|
onSaved?: (result: TourCreateResponse) => void | Promise<void>
|
|
}
|
|
|
|
export type TourPlannerMode =
|
|
| { type: 'neutral' }
|
|
| { type: 'new-draft' }
|
|
| { type: 'edit-saved'; placeId: number }
|
|
| { type: 'view-gpx'; placeId: number; tour: TourListItem }
|
|
|
|
export function selectTourPlannerMode(state: {
|
|
readOnlyGpxTour: { tour: TourListItem } | null
|
|
editingPlaceId: number | null
|
|
hasUnsavedChanges: boolean
|
|
}): TourPlannerMode {
|
|
if (state.readOnlyGpxTour) {
|
|
return { type: 'view-gpx', placeId: state.readOnlyGpxTour.tour.place_id, tour: state.readOnlyGpxTour.tour }
|
|
}
|
|
if (state.editingPlaceId !== null) return { type: 'edit-saved', placeId: state.editingPlaceId }
|
|
if (state.hasUnsavedChanges) return { type: 'new-draft' }
|
|
return { type: 'neutral' }
|
|
}
|
|
|
|
export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPermission, canAssign: assignPermission }: UseTourPlannerOptions) {
|
|
const { canEdit, canAssign } = useTourPermissions({ tripId, canEdit: editPermission, canAssign: assignPermission })
|
|
const editPermissionRef = useRef(canEdit)
|
|
editPermissionRef.current = canEdit
|
|
const draftStorage = useTourDraftStorage(tripId)
|
|
const { restored, keep: keepDraft, forget: forgetDraft } = draftStorage
|
|
|
|
const [name, setNameState] = useState(restored?.name ?? '')
|
|
const [description, setDescriptionState] = useState(restored?.description ?? '')
|
|
const [website, setWebsiteState] = useState(restored?.website ?? '')
|
|
const [plannedDurationMinutes, setPlannedDurationMinutesState] = useState<number | null>(restored?.plannedDurationMinutes ?? null)
|
|
const [breakAdditionalMinutes, setBreakAdditionalMinutesState] = useState<number | null>(restored?.breakAdditionalMinutes ?? null)
|
|
const [waypoints, setWaypoints] = useState<TourPlannerWaypoint[]>(restored?.waypoints ?? [])
|
|
const [maxHikingDifficulty, setMaxHikingDifficultyState] = useState<TourMaxHikingDifficulty>(restored?.maxHikingDifficulty ?? 2)
|
|
const [hikingPreferences, setHikingPreferencesState] = useState<HikingPreferences | null>(restored?.hikingPreferences ?? null)
|
|
const [bikeProfile, setBikeProfileState] = useState<BikeProfile>(restored?.bikeProfile ?? 'touring')
|
|
const [tourType, setTourTypeState] = useState<PlannableTourType>(restored?.tourType ?? 'hike')
|
|
const [selectedWaypointId, setSelectedWaypointId] = useState<string | null>(restored?.waypoints[0]?.id ?? null)
|
|
const history = usePlannerHistory<HistoryEntry>()
|
|
const { record: recordStep, clear: clearHistory } = history
|
|
const revision = useTourRevisionGuard(restored?.baseRevision ?? null)
|
|
const { adopt: adoptRevision, refuse: refuseRevision, baseRef: revisionBaseRef } = revision
|
|
const [status, setStatus] = useState<TourPlannerStatus>(restored?.waypoints.length ? 'dirty' : 'empty')
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(Boolean(restored))
|
|
const [isSaving, setIsSaving] = useState(false)
|
|
const [draftRestored, setDraftRestored] = useState(Boolean(restored))
|
|
const [editingPlaceId, setEditingPlaceId] = useState<number | null>(restored?.editingPlaceId ?? null)
|
|
const [derivedFromPlaceId, setDerivedFromPlaceId] = useState<number | null>(restored?.derivedFromPlaceId ?? null)
|
|
const [openingTourId, setOpeningTourId] = useState<number | null>(null)
|
|
const [saveOutcome, setSaveOutcome] = useState<TourListItem | null>(null)
|
|
const [retryToken, setRetryToken] = useState(0)
|
|
const mapView = useTourMapView()
|
|
const [readOnlyGpxTour, setReadOnlyGpxTour] = useState<{ tour: TourListItem; routeGeometry: string | null } | null>(null)
|
|
const [newTourConfirmationOpen, setNewTourConfirmationOpen] = useState(false)
|
|
const [elevationProfileExpanded, setElevationProfileExpanded] = useState(true)
|
|
const [routeProfileFocus, setRouteProfileFocus] = useState<RouteProfileFocus | null>(null)
|
|
const [newDraftGeneration, setNewDraftGeneration] = useState(0)
|
|
const [mapFocusKey, setMapFocusKey] = useState(0)
|
|
const draftRevisionRef = useRef(0)
|
|
const saveRequestIdRef = useRef(0)
|
|
const saveInFlightRef = useRef(false)
|
|
const deletedTourPlaceIdsRef = useRef(new Set<number>())
|
|
const mountedRef = useRef(false)
|
|
const contextGenerationRef = useRef(0)
|
|
const detailRequestIdRef = useRef(0)
|
|
const detailAbortRef = useRef<AbortController | null>(null)
|
|
const detailPlaceIdRef = useRef<number | null>(null)
|
|
const activeRef = useRef(active)
|
|
const readOnlyGpxTourRef = useRef(readOnlyGpxTour)
|
|
const tripIdRef = useRef(tripId)
|
|
const editingPlaceIdRef = useRef(editingPlaceId)
|
|
activeRef.current = active
|
|
readOnlyGpxTourRef.current = readOnlyGpxTour
|
|
tripIdRef.current = tripId
|
|
editingPlaceIdRef.current = editingPlaceId
|
|
const waypointsRef = useRef(waypoints)
|
|
const difficultyRef = useRef(maxHikingDifficulty)
|
|
const preferencesRef = useRef(hikingPreferences)
|
|
const bikeProfileRef = useRef(bikeProfile)
|
|
const tourTypeRef = useRef(tourType)
|
|
waypointsRef.current = waypoints
|
|
difficultyRef.current = maxHikingDifficulty
|
|
const snapshot = useCallback((): HistoryEntry => ({
|
|
waypoints: waypointsRef.current, difficulty: difficultyRef.current, preferences: preferencesRef.current,
|
|
bikeProfile: bikeProfileRef.current, tourType: tourTypeRef.current,
|
|
}), [])
|
|
|
|
useEffect(() => {
|
|
mountedRef.current = true
|
|
return () => {
|
|
mountedRef.current = false
|
|
saveRequestIdRef.current += 1
|
|
saveInFlightRef.current = false
|
|
detailRequestIdRef.current += 1
|
|
detailPlaceIdRef.current = null
|
|
detailAbortRef.current?.abort()
|
|
detailAbortRef.current = null
|
|
}
|
|
}, [])
|
|
|
|
const invalidateDetailRequest = useCallback(() => {
|
|
detailRequestIdRef.current += 1
|
|
detailPlaceIdRef.current = null
|
|
detailAbortRef.current?.abort()
|
|
detailAbortRef.current = null
|
|
if (mountedRef.current) setOpeningTourId(null)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
if (!active) invalidateDetailRequest()
|
|
}, [active, invalidateDetailRequest])
|
|
|
|
useEffect(() => {
|
|
invalidateDetailRequest()
|
|
}, [tripId, invalidateDetailRequest])
|
|
|
|
|
|
/** A field other than the route changed: the draft is unsaved again, and a finished save is no longer current. */
|
|
const markEdited = useCallback(() => {
|
|
if (!editPermissionRef.current) return false
|
|
invalidateDetailRequest()
|
|
draftRevisionRef.current += 1
|
|
setHasUnsavedChanges(true)
|
|
setSaveOutcome(null)
|
|
setStatus(current => current === 'saved' || current === 'saving' ? (waypointsRef.current.length < 2 ? 'dirty' : 'ready') : current)
|
|
return true
|
|
}, [invalidateDetailRequest])
|
|
|
|
const paddle = usePaddleFields(restored?.paddle ?? NO_PADDLE_FIELDS, markEdited)
|
|
const loadPaddle = paddle.load
|
|
const paddleBoat = paddle.boatType
|
|
// The router's own default where the tour names none, so a plan and its cache key agree.
|
|
const paddleLakeStyle = paddle.lakeStyle ?? 'direct'
|
|
|
|
// A point the router set (a paddle tour's) is laid in without a step of the history.
|
|
const patchWaypoints = useCallback((next: TourPlannerWaypoint[]) => {
|
|
waypointsRef.current = next
|
|
setWaypoints(next)
|
|
}, [])
|
|
const forgetFocus = useCallback(() => setRouteProfileFocus(null), [])
|
|
const routing = useTourRoute({
|
|
tripId, waypoints, tourType, maxHikingDifficulty, hikingPreferences, bikeProfile, paddleBoat, paddleLakeStyle, retryToken,
|
|
error, setStatus, setError, onCleared: forgetFocus, patchWaypoints,
|
|
})
|
|
const { route, routeParts, paddleRoute, enrichedGeometry, routeCurrent, distanceMeters, durationSeconds,
|
|
unreachable, interruptRouting, clearRoute } = routing
|
|
|
|
const commitWaypoints = useCallback((nextValue: TourPlannerWaypoint[] | ((current: TourPlannerWaypoint[]) => TourPlannerWaypoint[])) => {
|
|
if (!editPermissionRef.current) return
|
|
const current = waypointsRef.current
|
|
const changed = typeof nextValue === 'function' ? nextValue(current) : nextValue
|
|
const paddle = tourTypeRef.current === 'kayak'
|
|
// A point the router set that the paddler moves or edits is theirs from then on.
|
|
const next = normalizePlannerWaypoints(paddle ? adoptChangedPoints(current, changed) : changed, paddle)
|
|
if (sameWaypointSnapshot(current, next)) return
|
|
recordStep(snapshot())
|
|
waypointsRef.current = next
|
|
setWaypoints(next)
|
|
// CW-02: a station's own fields (its stay, its name) plan no route: the routing goes on, and Save stays as it was.
|
|
if (routeInputsKey(current) === routeInputsKey(next)) {
|
|
markEdited()
|
|
return
|
|
}
|
|
invalidateDetailRequest()
|
|
interruptRouting()
|
|
draftRevisionRef.current += 1
|
|
setHasUnsavedChanges(true)
|
|
setSaveOutcome(null)
|
|
setStatus('dirty')
|
|
setError(null)
|
|
}, [recordStep, invalidateDetailRequest, interruptRouting, markEdited, snapshot])
|
|
|
|
// Detail laid in after the step that asked for it (a portage's way on land), with no step of its own.
|
|
const amendWaypoints = useCallback((change: (current: TourPlannerWaypoint[]) => TourPlannerWaypoint[]) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current) return
|
|
const changed = change(waypointsRef.current)
|
|
if (changed === waypointsRef.current) return
|
|
interruptRouting()
|
|
draftRevisionRef.current += 1
|
|
waypointsRef.current = normalizePlannerWaypoints(changed, tourTypeRef.current === 'kayak')
|
|
setWaypoints(waypointsRef.current)
|
|
setHasUnsavedChanges(true)
|
|
setStatus('dirty')
|
|
}, [interruptRouting])
|
|
|
|
/**
|
|
* The routing choices, as one step of the history: a hike's limit and its
|
|
* preferences (MF-08), a bike tour's profile (BIK-01). The route is planned again.
|
|
*/
|
|
const setRoutingChoice = useCallback((change: {
|
|
difficulty?: TourMaxHikingDifficulty; preferences?: HikingPreferences | null; bikeProfile?: BikeProfile
|
|
}) => {
|
|
if (!editPermissionRef.current) return
|
|
const difficulty = change.difficulty ?? difficultyRef.current
|
|
const preferences = change.preferences === undefined ? preferencesRef.current : hikingPreferencesOf(change.preferences)
|
|
const profile = change.bikeProfile ?? bikeProfileRef.current
|
|
if (difficulty === difficultyRef.current && profile === bikeProfileRef.current
|
|
&& JSON.stringify(preferences) === JSON.stringify(preferencesRef.current)) return
|
|
invalidateDetailRequest()
|
|
interruptRouting()
|
|
draftRevisionRef.current += 1
|
|
recordStep(snapshot())
|
|
difficultyRef.current = difficulty
|
|
preferencesRef.current = preferences
|
|
bikeProfileRef.current = profile
|
|
setMaxHikingDifficultyState(difficulty)
|
|
setHikingPreferencesState(preferences)
|
|
setBikeProfileState(profile)
|
|
setHasUnsavedChanges(true)
|
|
setSaveOutcome(null)
|
|
setStatus('dirty')
|
|
setError(null)
|
|
}, [recordStep, invalidateDetailRequest, interruptRouting, snapshot])
|
|
const setMaxHikingDifficulty = useCallback((difficulty: TourMaxHikingDifficulty) => setRoutingChoice({ difficulty }), [setRoutingChoice])
|
|
const setHikingPreferences = useCallback((preferences: HikingPreferences | null) => setRoutingChoice({ preferences }), [setRoutingChoice])
|
|
const setBikeProfile = useCallback((profile: BikeProfile) => setRoutingChoice({ bikeProfile: profile }), [setRoutingChoice])
|
|
|
|
const addWaypoint = useCallback((lat: number, lng: number) => {
|
|
if (!editPermissionRef.current) return
|
|
const id = randomId()
|
|
commitWaypoints(current => withPointAppended<TourPlannerWaypoint>(current, { id, lat, lng, role: 'end' }))
|
|
setSelectedWaypointId(id)
|
|
}, [commitWaypoints])
|
|
|
|
/**
|
|
* A fixed leg onto the end of the tour, as one step of the history: a
|
|
* waypoint at the line's start that carries it, and one at its end.
|
|
*/
|
|
const appendFixedLeg = useCallback((leg: TourFixedLeg) => {
|
|
if (!editPermissionRef.current) return
|
|
const first = leg.geometry[0]
|
|
const last = leg.geometry[leg.geometry.length - 1]
|
|
if (!first || !last) return
|
|
const startId = randomId()
|
|
const endId = randomId()
|
|
commitWaypoints(current => [
|
|
...current,
|
|
{ id: startId, lat: Number(first[0]), lng: Number(first[1]), role: 'via', fixedLeg: leg },
|
|
{ id: endId, lat: Number(last[0]), lng: Number(last[1]), role: 'end' },
|
|
])
|
|
setSelectedWaypointId(endId)
|
|
}, [commitWaypoints])
|
|
|
|
const setWaypointPosition = useCallback((id: string, lat: number, lng: number) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current || !Number.isFinite(lat) || !Number.isFinite(lng)) return false
|
|
const current = waypointsRef.current.find(point => point.id === id)
|
|
if (!current) return false
|
|
if (current.lat === lat && current.lng === lng) return true
|
|
const points = waypointsRef.current
|
|
// In a loop the start and the way back are one spot: moving either moves both.
|
|
const loopEnds = isClosedLoop(points) && (id === points[0].id || id === points[points.length - 1].id)
|
|
? new Set([points[0].id, points[points.length - 1].id])
|
|
: new Set([id])
|
|
// A point moved stands for no place any more (PR1-10): it is where the hand put it.
|
|
commitWaypoints(list => list.map(point => loopEnds.has(point.id) ? unlinkedPoint({ ...point, lat, lng }) : point))
|
|
return true
|
|
}, [commitWaypoints])
|
|
|
|
/** PR1-01: the tour taken back from its last point to its start, as one step. */
|
|
const returnToStart = useCallback(() => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current) return
|
|
commitWaypoints(current => closeLoop(current, randomId()))
|
|
}, [commitWaypoints])
|
|
|
|
/** PR1-02: the tour the other way round, as one step. */
|
|
const reverse = useCallback(() => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current) return
|
|
commitWaypoints(current => reverseRoute(current))
|
|
}, [commitWaypoints])
|
|
|
|
/** PAD-03/PAD-07: a paddler's change to the control points (a portage, a lock), as one step. */
|
|
const editPaddlePoints = useCallback((change: (current: TourPlannerWaypoint[]) => TourPlannerWaypoint[]) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current || tourTypeRef.current !== 'kayak') return
|
|
commitWaypoints(change)
|
|
}, [commitWaypoints])
|
|
|
|
/** P2A-04: the leg after this waypoint drawn by hand, or routed again (a paddle tour's on the water). */
|
|
const toggleWaypointFreeLeg = useCallback((id: string) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current) return
|
|
commitWaypoints(current => toggleFreeLeg(current, id))
|
|
}, [commitWaypoints])
|
|
|
|
const removeWaypoint = useCallback((id: string) => {
|
|
if (!editPermissionRef.current) return
|
|
commitWaypoints(current => current.filter(point => point.id !== id))
|
|
setSelectedWaypointId(current => current === id ? null : current)
|
|
}, [commitWaypoints])
|
|
|
|
const moveWaypoint = useCallback((id: string, direction: -1 | 1) => {
|
|
commitWaypoints(current => {
|
|
const index = current.findIndex(point => point.id === id)
|
|
const target = index + direction
|
|
if (index < 0 || target < 0 || target >= current.length) return current
|
|
const next = [...current]
|
|
;[next[index], next[target]] = [next[target], next[index]]
|
|
return next
|
|
})
|
|
}, [commitWaypoints])
|
|
|
|
const reorderWaypoint = useCallback(
|
|
(id: string, targetId: string, placement: 'before' | 'after') => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current || id === targetId) return;
|
|
commitWaypoints((current) => {
|
|
const fromIndex = current.findIndex((point) => point.id === id);
|
|
const targetIndex = current.findIndex((point) => point.id === targetId);
|
|
if (fromIndex < 0 || targetIndex < 0) return current;
|
|
const next = [...current];
|
|
const [moved] = next.splice(fromIndex, 1);
|
|
const targetAfterRemoval = next.findIndex((point) => point.id === targetId);
|
|
const insertIndex = targetAfterRemoval + (placement === 'after' ? 1 : 0);
|
|
next.splice(insertIndex, 0, moved);
|
|
return next;
|
|
});
|
|
},
|
|
[commitWaypoints]
|
|
);
|
|
|
|
/** A snapshot of the history put back: the route and what it depends on. */
|
|
const restoreStep = useCallback((entry: HistoryEntry) => {
|
|
// CW-02: a step that changed a station's own fields only comes back without planning the route again.
|
|
const routeKept = sameRoute(entry, snapshot())
|
|
invalidateDetailRequest()
|
|
if (!routeKept) interruptRouting()
|
|
draftRevisionRef.current += 1
|
|
waypointsRef.current = entry.waypoints
|
|
difficultyRef.current = entry.difficulty
|
|
preferencesRef.current = entry.preferences
|
|
bikeProfileRef.current = entry.bikeProfile
|
|
tourTypeRef.current = entry.tourType
|
|
setWaypoints(entry.waypoints)
|
|
setMaxHikingDifficultyState(entry.difficulty)
|
|
setHikingPreferencesState(entry.preferences)
|
|
setBikeProfileState(entry.bikeProfile)
|
|
setTourTypeState(entry.tourType)
|
|
setSelectedWaypointId(id => entry.waypoints.some(point => point.id === id) ? id : entry.waypoints[0]?.id ?? null)
|
|
setHasUnsavedChanges(true)
|
|
if (routeKept) return
|
|
setStatus('dirty')
|
|
setError(null)
|
|
}, [invalidateDetailRequest, interruptRouting, snapshot])
|
|
|
|
const { undo: undoStep, redo: redoStep } = history
|
|
const undo = useCallback(() => {
|
|
if (!editPermissionRef.current) return
|
|
const previous = undoStep(snapshot())
|
|
if (previous) restoreStep(previous)
|
|
}, [restoreStep, snapshot, undoStep])
|
|
|
|
const redo = useCallback(() => {
|
|
if (!editPermissionRef.current) return
|
|
const next = redoStep(snapshot())
|
|
if (next) restoreStep(next)
|
|
}, [redoStep, restoreStep, snapshot])
|
|
|
|
|
|
const setName = useCallback((value: string) => {
|
|
if (value !== name && markEdited()) setNameState(value)
|
|
}, [markEdited, name])
|
|
|
|
const setDescription = useCallback((value: string) => {
|
|
if (value !== description && markEdited()) setDescriptionState(value)
|
|
}, [description, markEdited])
|
|
|
|
const setWebsite = useCallback((value: string) => {
|
|
if (value !== website && markEdited()) setWebsiteState(value)
|
|
}, [markEdited, website])
|
|
|
|
const setPlannedDurationMinutes = useCallback((value: number | null) => {
|
|
if (value !== plannedDurationMinutes && markEdited()) setPlannedDurationMinutesState(value)
|
|
}, [markEdited, plannedDurationMinutes])
|
|
|
|
const setBreakAdditionalMinutes = useCallback((value: number | null) => {
|
|
if (value !== breakAdditionalMinutes && markEdited()) setBreakAdditionalMinutesState(value)
|
|
}, [breakAdditionalMinutes, markEdited])
|
|
|
|
|
|
/**
|
|
* PAD-01, BIK-01: hike, bike or paddle tour, as one step of the history with
|
|
* the route, since a paddle tour draws every leg by hand and a bike tour
|
|
* rides no via ferrata. Nothing is put on a day by the choice itself.
|
|
*/
|
|
const setTourType = useCallback((value: PlannableTourType) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current || value === tourTypeRef.current) return
|
|
invalidateDetailRequest()
|
|
interruptRouting()
|
|
draftRevisionRef.current += 1
|
|
recordStep(snapshot())
|
|
tourTypeRef.current = value
|
|
setTourTypeState(value)
|
|
const next = normalizePlannerWaypoints(value === 'bike' ? ridablePoints(waypointsRef.current) : waypointsRef.current, value === 'kayak')
|
|
waypointsRef.current = next
|
|
setWaypoints(next)
|
|
setHasUnsavedChanges(true)
|
|
setSaveOutcome(null)
|
|
setStatus('dirty')
|
|
setError(null)
|
|
}, [recordStep, invalidateDetailRequest, interruptRouting, snapshot])
|
|
|
|
const routeAnalysis = useMemo(
|
|
() => analyzeRouteGeometry(JSON.stringify(enrichedGeometry ?? route)),
|
|
[enrichedGeometry, route],
|
|
)
|
|
const isPaddle = tourType === 'kayak'
|
|
const paddleSpeedInvalid = paddle.speedKmh !== null && !paddleSpeedKmhSchema.safeParse(paddle.speedKmh).success
|
|
const { effectiveSpeedKmh, portageSpeedKmh, boatType, waterContext, speedKmh, lakeStyle } = paddle
|
|
// The routed length of each leg while the route is the current points' (PAD-11); straight lines before that.
|
|
const routedLegLengths = paddleRoute && routeCurrent ? paddleRoute.legLengths : undefined
|
|
const paddleEstimate = useMemo(() => isPaddle ? paddleEstimateOf(waypoints, { effectiveSpeedKmh, portageSpeedKmh }, paddleSpeedInvalid, routedLegLengths) : null,
|
|
[effectiveSpeedKmh, isPaddle, paddleSpeedInvalid, portageSpeedKmh, routedLegLengths, waypoints])
|
|
// CW-02: a city walk's stays, what of them is known.
|
|
const stays = useMemo(() => (tourType === 'walk' ? walkTimes(waypoints, null) : null), [tourType, waypoints])
|
|
const facts = useMemo(() => tourPlannerFacts({
|
|
tourType, maxHikingDifficulty, hikingPreferences, bikeProfile, website, plannedDurationMinutes, breakAdditionalMinutes,
|
|
paddle: { boatType, waterContext, speedKmh, portageSpeedKmh, lakeStyle },
|
|
paddleSpeedInvalid, paddleEstimate, routeAnalysis, durationSeconds, stationStayMinutes: stays?.stayMinutes ?? null,
|
|
}), [bikeProfile, boatType, breakAdditionalMinutes, durationSeconds, hikingPreferences, lakeStyle, maxHikingDifficulty, paddleEstimate, paddleSpeedInvalid,
|
|
plannedDurationMinutes, portageSpeedKmh, routeAnalysis, speedKmh, stays, tourType, waterContext, website])
|
|
const { websiteInvalid, plannedDurationInvalid, breakAdditionalInvalid, paddleIncomplete, walkingDurationMinutes,
|
|
plannedTotalMinutes, plannedTotalInvalid, savedDurationSeconds, savedBreakAdditionalMinutes, typeFields } = facts
|
|
// CW-02: and with the walking time, the walk's times as the overview says them.
|
|
const stationStays = useMemo(() => stays && walkTimesOf({ ...stays, walkingMinutes: walkingDurationMinutes }), [stays, walkingDurationMinutes])
|
|
|
|
const returnToNeutral = useCallback(() => {
|
|
if (saveInFlightRef.current) return
|
|
invalidateDetailRequest()
|
|
interruptRouting()
|
|
contextGenerationRef.current += 1
|
|
draftRevisionRef.current += 1
|
|
setRouteProfileFocus(null)
|
|
forgetDraft()
|
|
setNameState('')
|
|
setDescriptionState('')
|
|
setWebsiteState('')
|
|
setPlannedDurationMinutesState(null)
|
|
setBreakAdditionalMinutesState(null)
|
|
setWaypoints([])
|
|
setMaxHikingDifficultyState(2)
|
|
setTourTypeState('hike')
|
|
loadPaddle(NO_PADDLE_FIELDS)
|
|
waypointsRef.current = []
|
|
difficultyRef.current = 2
|
|
preferencesRef.current = null
|
|
setHikingPreferencesState(null)
|
|
bikeProfileRef.current = 'touring'
|
|
setBikeProfileState('touring')
|
|
tourTypeRef.current = 'hike'
|
|
setSelectedWaypointId(null)
|
|
clearHistory()
|
|
setStatus('empty')
|
|
clearRoute()
|
|
setError(null)
|
|
setHasUnsavedChanges(false)
|
|
setDraftRestored(false)
|
|
setEditingPlaceId(null)
|
|
editingPlaceIdRef.current = null
|
|
setDerivedFromPlaceId(null)
|
|
adoptRevision(null)
|
|
setOpeningTourId(null)
|
|
setSaveOutcome(null)
|
|
setReadOnlyGpxTour(null)
|
|
setNewTourConfirmationOpen(false)
|
|
setElevationProfileExpanded(true)
|
|
setNewDraftGeneration(generation => generation + 1)
|
|
setMapFocusKey(key => key + 1)
|
|
}, [adoptRevision, clearHistory, clearRoute, invalidateDetailRequest, interruptRouting, loadPaddle, forgetDraft])
|
|
|
|
const forgetDeletedTour = useCallback((placeId: number) => {
|
|
deletedTourPlaceIdsRef.current.add(placeId)
|
|
const editorReferencesTour = editingPlaceIdRef.current === placeId
|
|
|| readOnlyGpxTourRef.current?.tour.place_id === placeId
|
|
|| detailPlaceIdRef.current === placeId
|
|
if (!editorReferencesTour) return
|
|
if (saveInFlightRef.current) {
|
|
saveRequestIdRef.current += 1
|
|
saveInFlightRef.current = false
|
|
setIsSaving(false)
|
|
}
|
|
returnToNeutral()
|
|
}, [returnToNeutral])
|
|
|
|
const startNewTour = useCallback((confirmed = false) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current) return
|
|
const currentDraftIsEmpty = editingPlaceId === null && waypoints.length === 0 && name.trim().length === 0
|
|
if (!confirmed && hasUnsavedChanges && !currentDraftIsEmpty) {
|
|
setNewTourConfirmationOpen(true)
|
|
return
|
|
}
|
|
returnToNeutral()
|
|
setHasUnsavedChanges(true)
|
|
}, [editingPlaceId, hasUnsavedChanges, name, returnToNeutral, waypoints.length]
|
|
)
|
|
|
|
/**
|
|
* A draft through the control points taken from an import: a new tour that
|
|
* remembers the import, or the import itself planned over in place. Nothing
|
|
* is stored until the draft is saved like any other.
|
|
*/
|
|
const startDerivedTour = useCallback(
|
|
(draft: DerivedTourDraft) => {
|
|
if (!editPermissionRef.current || saveInFlightRef.current || draft.points.length < 2) return false
|
|
returnToNeutral()
|
|
const type = draft.tourType ?? 'hike'
|
|
const points = normalizePlannerWaypoints(
|
|
draft.points.map(({ lat, lng }) => ({ id: randomId(), lat, lng, role: 'via' as const })),
|
|
type === 'kayak',
|
|
)
|
|
tourTypeRef.current = type
|
|
setTourTypeState(type)
|
|
if (draft.paddle) loadPaddle(draft.paddle)
|
|
if (draft.bikeProfile) {
|
|
bikeProfileRef.current = draft.bikeProfile
|
|
setBikeProfileState(draft.bikeProfile)
|
|
}
|
|
setNameState(draft.name)
|
|
setDescriptionState(draft.description ?? '')
|
|
setWebsiteState(draft.website ?? '')
|
|
setWaypoints(points)
|
|
waypointsRef.current = points
|
|
setMaxHikingDifficultyState(draft.difficulty)
|
|
difficultyRef.current = draft.difficulty
|
|
setSelectedWaypointId(points[0].id)
|
|
if (draft.inPlace) {
|
|
setEditingPlaceId(draft.sourcePlaceId)
|
|
editingPlaceIdRef.current = draft.sourcePlaceId
|
|
adoptRevision(draft.revision)
|
|
} else {
|
|
setDerivedFromPlaceId(draft.sourcePlaceId)
|
|
}
|
|
setStatus('dirty')
|
|
setHasUnsavedChanges(true)
|
|
return true
|
|
},
|
|
[adoptRevision, loadPaddle, returnToNeutral])
|
|
|
|
const cancelNewTour = useCallback(() => setNewTourConfirmationOpen(false), [])
|
|
const toggleElevationProfile = useCallback(() => {
|
|
if (elevationProfileExpanded) setRouteProfileFocus(null)
|
|
setElevationProfileExpanded(expanded => !expanded)
|
|
}, [elevationProfileExpanded])
|
|
const discard = useCallback(() => returnToNeutral(), [returnToNeutral])
|
|
|
|
const viewGpxTour = useCallback((tour: TourListItem, routeGeometry: string | null) => {
|
|
if (saveInFlightRef.current) return
|
|
invalidateDetailRequest()
|
|
contextGenerationRef.current += 1
|
|
returnToNeutral()
|
|
setReadOnlyGpxTour({ tour, routeGeometry })
|
|
}, [invalidateDetailRequest, returnToNeutral])
|
|
|
|
const closeGpxTour = useCallback(() => {
|
|
invalidateDetailRequest()
|
|
contextGenerationRef.current += 1
|
|
setRouteProfileFocus(null)
|
|
setReadOnlyGpxTour(null)
|
|
}, [invalidateDetailRequest])
|
|
|
|
const openTour = useCallback(async (tour: Pick<TourListItem, 'place_id'> & Partial<TourListItem>) => {
|
|
if (!mountedRef.current || !activeRef.current || saveInFlightRef.current || deletedTourPlaceIdsRef.current.has(tour.place_id)) return false
|
|
detailAbortRef.current?.abort()
|
|
const controller = new AbortController()
|
|
detailAbortRef.current = controller
|
|
const requestId = ++detailRequestIdRef.current
|
|
const submittedTripId = String(tripId)
|
|
const submittedPlaceId = tour.place_id
|
|
const submittedContextGeneration = contextGenerationRef.current
|
|
const submittedRevision = draftRevisionRef.current
|
|
const submittedEditingPlaceId = editingPlaceIdRef.current
|
|
const submittedGpxPlaceId = readOnlyGpxTourRef.current?.tour.place_id ?? null
|
|
detailPlaceIdRef.current = submittedPlaceId
|
|
const requestIsCurrent = () => mountedRef.current
|
|
&& activeRef.current
|
|
&& requestId === detailRequestIdRef.current
|
|
&& detailPlaceIdRef.current === submittedPlaceId
|
|
&& String(tripIdRef.current) === submittedTripId
|
|
&& contextGenerationRef.current === submittedContextGeneration
|
|
&& draftRevisionRef.current === submittedRevision
|
|
&& editingPlaceIdRef.current === submittedEditingPlaceId
|
|
&& (readOnlyGpxTourRef.current?.tour.place_id ?? null) === submittedGpxPlaceId
|
|
setRouteProfileFocus(null)
|
|
setOpeningTourId(tour.place_id)
|
|
setError(null)
|
|
try {
|
|
const result = await tourRepo.detail(tripId, tour.place_id, controller.signal)
|
|
if (!requestIsCurrent() || saveInFlightRef.current) return false
|
|
if (result.waypoints.length < 2) {
|
|
setError('open')
|
|
return false
|
|
}
|
|
|
|
interruptRouting()
|
|
const opened = openedTourFields(result.tour)
|
|
const nextWaypoints = normalizePlannerWaypoints(plannerWaypointsOf(result.waypoints, randomId), opened.tourType === 'kayak')
|
|
forgetDraft()
|
|
contextGenerationRef.current += 1
|
|
draftRevisionRef.current += 1
|
|
setNameState(opened.name)
|
|
setDescriptionState(opened.description)
|
|
setWebsiteState(opened.website)
|
|
setPlannedDurationMinutesState(opened.plannedDurationMinutes)
|
|
setBreakAdditionalMinutesState(opened.breakAdditionalMinutes)
|
|
setWaypoints(nextWaypoints)
|
|
setMaxHikingDifficultyState(opened.difficulty)
|
|
setTourTypeState(opened.tourType)
|
|
loadPaddle(paddleFieldsOf(result.tour))
|
|
waypointsRef.current = nextWaypoints
|
|
difficultyRef.current = opened.difficulty
|
|
preferencesRef.current = opened.preferences
|
|
setHikingPreferencesState(opened.preferences)
|
|
bikeProfileRef.current = opened.bikeProfile
|
|
setBikeProfileState(opened.bikeProfile)
|
|
tourTypeRef.current = opened.tourType
|
|
setSelectedWaypointId(nextWaypoints[0]?.id ?? null)
|
|
clearHistory()
|
|
setStatus('dirty')
|
|
clearRoute()
|
|
setHasUnsavedChanges(false)
|
|
setDraftRestored(false)
|
|
setEditingPlaceId(result.tour.place_id)
|
|
editingPlaceIdRef.current = result.tour.place_id
|
|
adoptRevision(result.tour.revision)
|
|
setMapFocusKey(key => key + 1)
|
|
setSaveOutcome(null)
|
|
setReadOnlyGpxTour(null)
|
|
return true
|
|
} catch {
|
|
if (requestIsCurrent() && !saveInFlightRef.current) setError('open')
|
|
return false
|
|
} finally {
|
|
if (mountedRef.current && requestId === detailRequestIdRef.current) {
|
|
detailPlaceIdRef.current = null
|
|
if (detailAbortRef.current === controller) detailAbortRef.current = null
|
|
setOpeningTourId(null)
|
|
}
|
|
}
|
|
}, [adoptRevision, clearHistory, clearRoute, interruptRouting, loadPaddle, forgetDraft, tripId])
|
|
|
|
const retry = useCallback(() => {
|
|
if (waypointsRef.current.length < 2) return
|
|
interruptRouting()
|
|
setError(null)
|
|
setStatus('dirty')
|
|
setRetryToken(value => value + 1)
|
|
}, [interruptRouting])
|
|
|
|
// P2A-03/P2A-05/PAD-11: shaping points, a leg planned afresh, drawn legs handed back to the router.
|
|
const shaping = useRouteShaping({
|
|
commit: commitWaypoints, waypointsRef, retry,
|
|
mayEdit: useCallback(() => editPermissionRef.current && !saveInFlightRef.current, []),
|
|
isPaddle: useCallback(() => tourTypeRef.current === 'kayak', []),
|
|
forgetLeg: useCallback((from: TourPlannerWaypoint, to: TourPlannerWaypoint) =>
|
|
paddleRouteRepo.forget(tripId, from, to, { boat: paddleBoat ?? 'kayak', lakeStyle: paddleLakeStyle }), [paddleBoat, paddleLakeStyle, tripId]),
|
|
})
|
|
|
|
const readOnlyGpxAnalysis = useMemo(
|
|
() => analyzeRouteGeometry(readOnlyGpxTour?.routeGeometry),
|
|
[readOnlyGpxTour?.routeGeometry],
|
|
)
|
|
const mode = useMemo(() => selectTourPlannerMode({ readOnlyGpxTour, editingPlaceId, hasUnsavedChanges }), [editingPlaceId, hasUnsavedChanges, readOnlyGpxTour])
|
|
const focusOwner = mode.type === 'view-gpx' || mode.type === 'edit-saved' ? `${mode.type}:${mode.placeId}` : mode.type
|
|
useEffect(() => { setRouteProfileFocus(null) }, [focusOwner, routeAnalysis, readOnlyGpxAnalysis, readOnlyGpxTour])
|
|
|
|
|
|
useEffect(() => {
|
|
if (!hasUnsavedChanges) return
|
|
const draft: DraftPayload = { version: 10, name, description, website, plannedDurationMinutes, breakAdditionalMinutes, waypoints, maxHikingDifficulty, editingPlaceId,
|
|
derivedFromPlaceId, tourType, baseRevision: revision.base, hikingPreferences, bikeProfile,
|
|
paddle: { boatType: paddle.boatType, waterContext: paddle.waterContext, speedKmh: paddle.speedKmh, portageSpeedKmh: paddle.portageSpeedKmh, lakeStyle: paddle.lakeStyle },
|
|
}
|
|
keepDraft(draft)
|
|
}, [breakAdditionalMinutes,
|
|
derivedFromPlaceId, description, editingPlaceId, hasUnsavedChanges, maxHikingDifficulty, name, plannedDurationMinutes, keepDraft, waypoints, website,
|
|
tourType, paddle.boatType, paddle.waterContext, paddle.speedKmh, paddle.portageSpeedKmh, paddle.lakeStyle, revision.base, hikingPreferences,
|
|
bikeProfile])
|
|
|
|
useEffect(() => {
|
|
if (!hasUnsavedChanges) return
|
|
const warn = (event: BeforeUnloadEvent) => { event.preventDefault(); event.returnValue = '' }
|
|
window.addEventListener('beforeunload', warn)
|
|
return () => window.removeEventListener('beforeunload', warn)
|
|
}, [hasUnsavedChanges])
|
|
|
|
const canSave = canEdit && !isSaving && hasUnsavedChanges && name.trim().length > 0 && status === 'ready'
|
|
&& routeCurrent && hasCompleteElevation(enrichedGeometry)
|
|
&& description.length <= 2000 && !websiteInvalid && !plannedDurationInvalid && !breakAdditionalInvalid && !plannedTotalInvalid
|
|
&& !paddleIncomplete && !(isPaddle && paddleSpeedInvalid)
|
|
|
|
/**
|
|
* Saves the draft: over the tour it edits, naming the revision it was loaded
|
|
* at (P2C-03), or with `asNew` as a tour of its own under `name`, the way out
|
|
* of a refused save that keeps both versions.
|
|
*/
|
|
const save = useCallback(async (options: { asNew?: boolean; name?: string } = {}) => {
|
|
if (!mountedRef.current || saveInFlightRef.current || !editPermissionRef.current || !canSave || !hasCompleteElevation(enrichedGeometry)) return null
|
|
if (editingPlaceId !== null && deletedTourPlaceIdsRef.current.has(editingPlaceId)) return null
|
|
invalidateDetailRequest()
|
|
saveInFlightRef.current = true
|
|
const requestId = ++saveRequestIdRef.current
|
|
const submittedRevision = draftRevisionRef.current
|
|
const submittedContextGeneration = contextGenerationRef.current
|
|
const submittedTripId = String(tripId)
|
|
const submittedPlaceId = editingPlaceId
|
|
const targetPlaceId = options.asNew ? null : submittedPlaceId
|
|
const expectedRevision = targetPlaceId === null ? null : revisionBaseRef.current
|
|
const payload = {
|
|
name: (options.name ?? name).trim(),
|
|
description: trimmedOrNull(description),
|
|
website: parsedWebsiteOrNull(website),
|
|
...typeFields,
|
|
route_geometry: enrichedGeometry.map(point => [point[0], point[1], point[2]]),
|
|
waypoints: savedWaypointsOf(waypoints, tourType),
|
|
duration_seconds: savedDurationSeconds,
|
|
planned_duration_minutes: plannedDurationMinutes,
|
|
break_additional_minutes: savedBreakAdditionalMinutes,
|
|
...derivedFromField(targetPlaceId, derivedFromPlaceId),
|
|
...(expectedRevision !== null ? { expected_revision: expectedRevision } : {}),
|
|
} as const
|
|
setStatus('saving')
|
|
setIsSaving(true)
|
|
setError(null)
|
|
const stillCurrent = () => mountedRef.current
|
|
&& requestId === saveRequestIdRef.current
|
|
&& String(tripIdRef.current) === submittedTripId
|
|
&& editingPlaceIdRef.current === submittedPlaceId
|
|
&& contextGenerationRef.current === submittedContextGeneration
|
|
&& draftRevisionRef.current === submittedRevision
|
|
try {
|
|
const result = targetPlaceId === null
|
|
? await tourRepo.create(tripId, payload)
|
|
: await tourRepo.update(tripId, targetPlaceId, payload)
|
|
if (stillCurrent()) {
|
|
// CW-03: the keys the server gave new points stay with them, so the next save keeps their visits.
|
|
const keyed = withSavedKeys(waypointsRef.current, result.waypoints)
|
|
if (keyed !== waypointsRef.current) {
|
|
waypointsRef.current = keyed
|
|
setWaypoints(keyed)
|
|
}
|
|
forgetDraft()
|
|
adoptRevision(result.tour.revision)
|
|
if (options.name) setNameState(options.name.trim())
|
|
setHasUnsavedChanges(false)
|
|
setStatus('saved')
|
|
clearHistory()
|
|
setEditingPlaceId(result.tour.place_id)
|
|
editingPlaceIdRef.current = result.tour.place_id
|
|
setSaveOutcome(result.tour)
|
|
}
|
|
if (mountedRef.current && String(tripIdRef.current) === submittedTripId && !deletedTourPlaceIdsRef.current.has(result.tour.place_id)) {
|
|
try { await onSaved?.(result) } catch { return result }
|
|
}
|
|
return result
|
|
} catch (failure: unknown) {
|
|
if (stillCurrent()) {
|
|
setStatus('ready')
|
|
// A save refused because someone else saved the tour meanwhile keeps the draft and asks what to do.
|
|
setError(refuseRevision(failure) ? 'conflict' : 'save')
|
|
}
|
|
return null
|
|
} finally {
|
|
if (mountedRef.current && requestId === saveRequestIdRef.current) {
|
|
saveInFlightRef.current = false
|
|
setIsSaving(false)
|
|
}
|
|
}
|
|
}, [adoptRevision, refuseRevision, revisionBaseRef, clearHistory, savedBreakAdditionalMinutes, canSave,
|
|
derivedFromPlaceId,
|
|
description, savedDurationSeconds, editingPlaceId, enrichedGeometry, invalidateDetailRequest, name, onSaved, plannedDurationMinutes, forgetDraft, tourType, tripId, typeFields, waypoints, website])
|
|
|
|
return {
|
|
tripId, canEdit, canAssign,
|
|
name, setName, maxHikingDifficulty, setMaxHikingDifficulty, hikingPreferences, setHikingPreferences,
|
|
bikeProfile, setBikeProfile, isBike: tourType === 'bike',
|
|
tourType, setTourType, isPaddle, paddle, paddleSpeedInvalid, paddleIncomplete,
|
|
description, setDescription, website, setWebsite, websiteInvalid,
|
|
plannedDurationMinutes, setPlannedDurationMinutes, plannedDurationInvalid,
|
|
breakAdditionalMinutes, setBreakAdditionalMinutes, breakAdditionalInvalid,
|
|
walkingDurationMinutes, plannedTotalMinutes, plannedTotalInvalid, stationStays,
|
|
waypoints, selectedWaypointId, setSelectedWaypointId,
|
|
addWaypoint, appendFixedLeg, setWaypointPosition, removeWaypoint, moveWaypoint, reorderWaypoint, editWaypoints: commitWaypoints,
|
|
reverse, returnToStart, isLoop: isClosedLoop(waypoints), toggleWaypointFreeLeg, editPaddlePoints, amendWaypoints, paddleEstimate,
|
|
...shaping, paddleRoute, routeCurrent,
|
|
undo, redo, canUndo: history.canUndo, canRedo: history.canRedo,
|
|
status, error, retry, isSaving,
|
|
route, routeParts, enrichedGeometry, routeAnalysis, distanceMeters, durationSeconds, unreachable,
|
|
routeProfileFocus, setRouteProfileFocus,
|
|
...mapView,
|
|
mode, readOnlyGpxTour, readOnlyGpxAnalysis, viewGpxTour, closeGpxTour,
|
|
startNewTour,
|
|
startDerivedTour,
|
|
derivedFromPlaceId,
|
|
newTourConfirmationOpen, cancelNewTour, forgetDeletedTour,
|
|
elevationProfileExpanded, toggleElevationProfile, newDraftGeneration, draftRestored, mapFocusKey,
|
|
editingPlaceId, openingTourId, openTour,
|
|
saveOutcome, hasUnsavedChanges, canSave, save, discard, returnToNeutral, revision,
|
|
}
|
|
}
|
|
|
|
export type TourPlannerController = ReturnType<typeof useTourPlanner>
|