fix(tours): preserve map viewport during route updates

This commit is contained in:
elmocito
2026-10-06 00:44:43 +02:00
parent af7dbce105
commit 04c7491e2a
8 changed files with 146 additions and 6 deletions
@@ -81,6 +81,36 @@ describe('useTourPlanner', () => {
expect(result.current.waypoints).toHaveLength(3)
})
it('changes the map focus intent only for explicit Tour context changes', async () => {
detailTour.mockResolvedValue({
tour: savedTour,
waypoints: [
{ lat: 48, lng: 11, role: 'start', sequence: 0 },
{ lat: 48.01, lng: 11.02, role: 'end', sequence: 1 },
],
})
const { result } = renderHook(() => useTourPlanner({ tripId: 8 }))
const initialFocusKey = result.current.mapFocusKey
act(() => {
result.current.addWaypoint(48, 11)
result.current.addWaypoint(48.01, 11.02)
})
await act(async () => { await vi.advanceTimersByTimeAsync(450) })
expect(result.current.mapFocusKey).toBe(initialFocusKey)
act(() => result.current.retry())
expect(result.current.mapFocusKey).toBe(initialFocusKey)
await act(async () => { expect(await result.current.openTour(savedTour)).toBe(true) })
expect(result.current.mapFocusKey).toBe(initialFocusKey + 1)
act(() => result.current.addWaypoint(48.02, 11.03))
expect(result.current.mapFocusKey).toBe(initialFocusKey + 1)
act(() => result.current.viewGpxTour({ ...savedTour, has_waypoints: false }, '[[48,11],[48.01,11.02]]'))
expect(result.current.mapFocusKey).toBe(initialFocusKey + 2)
})
it('TOUR-PLANNER-002: restores a trip-scoped local draft without changing stable ids', () => {
localStorage.setItem('tour-draft-8', JSON.stringify({
version: 1,
@@ -136,6 +136,7 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
const [elevationProfileExpanded, setElevationProfileExpanded] = useState(true)
const [routeProfileFocus, setRouteProfileFocus] = useState<RouteProfileFocus | null>(null)
const [newDraftGeneration, setNewDraftGeneration] = useState(0)
const [mapFocusKey, setMapFocusKey] = useState(0)
const abortRef = useRef<AbortController | null>(null)
const generationRef = useRef(0)
const draftRevisionRef = useRef(0)
@@ -334,6 +335,7 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
setNewTourConfirmationOpen(false)
setElevationProfileExpanded(true)
setNewDraftGeneration(generation => generation + 1)
setMapFocusKey(key => key + 1)
}, [invalidateDetailRequest, interruptRouting, storageKey])
const startNewTour = useCallback((confirmed = false) => {
@@ -429,6 +431,7 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
setDraftRestored(false)
setEditingPlaceId(result.tour.place_id)
editingPlaceIdRef.current = result.tour.place_id
setMapFocusKey(key => key + 1)
setSaveOutcome(null)
setReadOnlyGpxTour(null)
return true
@@ -613,7 +616,7 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
mapBaseLayer, setMapBaseLayer,
mode, readOnlyGpxTour, readOnlyGpxAnalysis, viewGpxTour, closeGpxTour,
startNewTour, newTourConfirmationOpen, cancelNewTour,
elevationProfileExpanded, toggleElevationProfile, newDraftGeneration, draftRestored,
elevationProfileExpanded, toggleElevationProfile, newDraftGeneration, draftRestored, mapFocusKey,
editingPlaceId, openingTourId, openTour,
saveOutcome, hasUnsavedChanges, canSave, save, discard, returnToNeutral,
}