diff --git a/client/src/components/Tours/planner/tourPlannerParts.test.ts b/client/src/components/Tours/planner/tourPlannerParts.test.ts index a75ac57b4..9b52ec8ab 100644 --- a/client/src/components/Tours/planner/tourPlannerParts.test.ts +++ b/client/src/components/Tours/planner/tourPlannerParts.test.ts @@ -329,6 +329,27 @@ describe('usePlannerHistory', () => { }); expect(undone).toBe(30); }); + + it('moves its mark on every step taken, undone, redone or cleared, and not on an undo with nothing left', () => { + const { result } = renderHook(() => usePlannerHistory()); + expect(result.current.mark).toBe(0); + act(() => result.current.record(1)); + expect(result.current.mark).toBe(1); + act(() => { + result.current.undo(2); + }); + expect(result.current.mark).toBe(2); + act(() => { + result.current.undo(1); + }); + expect(result.current.mark).toBe(2); + act(() => { + result.current.redo(1); + }); + expect(result.current.mark).toBe(3); + act(() => result.current.clear()); + expect(result.current.mark).toBe(4); + }); }); describe('useTourDraftStorage', () => { diff --git a/client/src/components/Tours/planner/usePlannerHistory.ts b/client/src/components/Tours/planner/usePlannerHistory.ts index 7dec177ea..7daa2ae93 100644 --- a/client/src/components/Tours/planner/usePlannerHistory.ts +++ b/client/src/components/Tours/planner/usePlannerHistory.ts @@ -10,11 +10,14 @@ const HISTORY_LIMIT = 30; export function usePlannerHistory() { const [past, setPast] = useState([]); const [future, setFuture] = useState([]); + // Counts every step taken, undone or redone: a caller can tell whether its own step is still the last one. + const [mark, setMark] = useState(0); /** A new step: the state before it goes on the stack, and nothing is left to redo. */ const record = useCallback((before: Entry) => { setPast((current) => [...current, before].slice(-HISTORY_LIMIT)); setFuture([]); + setMark((current) => current + 1); }, []); /** The step to go back to, with the present kept for redo; null when there is none. */ @@ -24,6 +27,7 @@ export function usePlannerHistory() { if (previous === undefined) return null; setPast(past.slice(0, -1)); setFuture([present, ...future].slice(0, HISTORY_LIMIT)); + setMark((current) => current + 1); return previous; }, [future, past] @@ -35,6 +39,7 @@ export function usePlannerHistory() { if (next === undefined) return null; setPast([...past, present].slice(-HISTORY_LIMIT)); setFuture(future.slice(1)); + setMark((current) => current + 1); return next; }, [future, past] @@ -43,7 +48,8 @@ export function usePlannerHistory() { const clear = useCallback(() => { setPast([]); setFuture([]); + setMark((current) => current + 1); }, []); - return { record, undo, redo, clear, canUndo: past.length > 0, canRedo: future.length > 0 }; + return { record, undo, redo, clear, mark, canUndo: past.length > 0, canRedo: future.length > 0 }; } diff --git a/client/src/components/Tours/planner/useTourPlanner.ts b/client/src/components/Tours/planner/useTourPlanner.ts index 6a352bc24..75f10038e 100644 --- a/client/src/components/Tours/planner/useTourPlanner.ts +++ b/client/src/components/Tours/planner/useTourPlanner.ts @@ -933,7 +933,7 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe addWaypoint, appendFixedLeg, setWaypointPosition, removeWaypoint, moveWaypoint, reorderWaypoint, editWaypoints: commitWaypoints, reverse, returnToStart, isLoop: isClosedLoop(waypoints), toggleWaypointFreeLeg, editPaddlePoints, amendWaypoints, paddleEstimate, ...shaping, legVariants, roundHikes, paddleRoute, skiRoute, routeCurrent, stages, - undo, redo, canUndo: history.canUndo, canRedo: history.canRedo, + undo, redo, canUndo: history.canUndo, canRedo: history.canRedo, historyMark: history.mark, status, error, retry, isSaving, route, routeParts, enrichedGeometry, routeAnalysis, distanceMeters, durationSeconds, unreachable, routeProfileFocus, setRouteProfileFocus, diff --git a/client/src/components/Tours/poi/TourPoiSlotPicker.tsx b/client/src/components/Tours/poi/TourPoiSlotPicker.tsx index b1942ff63..7f0037b82 100644 --- a/client/src/components/Tours/poi/TourPoiSlotPicker.tsx +++ b/client/src/components/Tours/poi/TourPoiSlotPicker.tsx @@ -158,18 +158,21 @@ export function TourPoiSlotPicker({ ) )}
- + + + + + {detour && !block && ( diff --git a/client/src/components/Tours/poi/tourPoiSearch.test.tsx b/client/src/components/Tours/poi/tourPoiSearch.test.tsx index f0875974a..418fb5f9c 100644 --- a/client/src/components/Tours/poi/tourPoiSearch.test.tsx +++ b/client/src/components/Tours/poi/tourPoiSearch.test.tsx @@ -14,7 +14,7 @@ import { usePoiInsertion, type PoiInsertionPlanner } from './usePoiInsertion'; import { useTourPoiMarkers } from './useTourPoiMarkers'; import { useTourPoiSearch } from './useTourPoiSearch'; -// FE-TOUR-POI-001 to FE-TOUR-POI-021 +// FE-TOUR-POI-001 to FE-TOUR-POI-022 vi.mock('../planner/useWaypointPlaceNames', () => ({ useWaypointPlaceNames: () => ({}) })); @@ -127,6 +127,7 @@ function insertionPlanner(over: Partial = {}): PoiInsertion isSaving: false, undo: vi.fn(), canUndo: true, + historyMark: 0, ...over, }; } @@ -520,7 +521,13 @@ describe('choosing where a found place goes (PLN-04)', () => { const withPoint = [PA, PB, { ...PC, role: 'via' as const }, added]; useTourPoiStore .getState() - .setInserted({ pointId: 'new', name: 'Spring', before: { distanceM: 2200, minutes: 40 }, pointCount: 4 }); + .setInserted({ + pointId: 'new', + name: 'Spring', + before: { distanceM: 2200, minutes: 40 }, + pointCount: 4, + mark: 0, + }); const planner = alongPlanner({ waypoints: withPoint, distanceMeters: 2900, walkingDurationMinutes: 52 }); const { unmount } = render(); const notice = screen.getByTestId('tour-poi-outcome'); @@ -533,9 +540,33 @@ describe('choosing where a found place goes (PLN-04)', () => { // With nothing to undo, or no right to edit, the notice offers no way back. useTourPoiStore .getState() - .setInserted({ pointId: 'new', name: 'Spring', before: { distanceM: 2200, minutes: 40 }, pointCount: 4 }); + .setInserted({ + pointId: 'new', + name: 'Spring', + before: { distanceM: 2200, minutes: 40 }, + pointCount: 4, + mark: 0, + }); render(); expect(screen.getByTestId('tour-poi-outcome')).toBeInTheDocument(); expect(screen.queryByTestId('tour-poi-undo')).not.toBeInTheDocument(); }); + + it('FE-TOUR-POI-022: a later step that keeps the point count takes the undo away, so it never reverts that step', () => { + let planner = insertionPlanner({ historyMark: 3 }); + const { result, rerender } = renderHook(() => usePoiInsertion(planner)); + act(() => { + result.current.insert(SPRING); + }); + const added = vi.mocked(planner.setSelectedWaypointId).mock.calls[0]?.[0] as string; + const withPoint = [PA, PB, { id: added, lat: 47.015, lng: 11.001, role: 'via' as const }, PC]; + planner = insertionPlanner({ waypoints: withPoint, historyMark: 4 }); + rerender(); + expect(result.current.undo).not.toBeNull(); + // The new point dragged, or the tour renamed: still four points, but no longer the last step. + planner = insertionPlanner({ waypoints: withPoint, historyMark: 5 }); + rerender(); + expect(result.current.outcome).not.toBeNull(); + expect(result.current.undo).toBeNull(); + }); }); diff --git a/client/src/components/Tours/poi/usePoiInsertion.ts b/client/src/components/Tours/poi/usePoiInsertion.ts index b45d5ff15..2a9079738 100644 --- a/client/src/components/Tours/poi/usePoiInsertion.ts +++ b/client/src/components/Tours/poi/usePoiInsertion.ts @@ -35,6 +35,7 @@ export type PoiInsertionPlanner = Pick< | 'isSaving' | 'undo' | 'canUndo' + | 'historyMark' >; function measuresOf(planner: PoiInsertionPlanner): Measures | null { @@ -92,7 +93,8 @@ export function usePoiInsertion(planner: PoiInsertionPlanner) { planner.setSelectedWaypointId(point.id); // An unnamed place (a tap, a bench) is told by its kind, as the list names it. const name = poi.name || t(TOUR_POI_LOOKS[poi.category].labelKey); - setInserted({ pointId: point.id, name, before, pointCount: next.length }); + // Taking it in is one step of the history, so the mark moves on by one. + setInserted({ pointId: point.id, name, before, pointCount: next.length, mark: planner.historyMark + 1 }); chooseSlot(null); select(null); return true; @@ -117,7 +119,10 @@ export function usePoiInsertion(planner: PoiInsertionPlanner) { insert, outcome, dismiss, - undo: outcome && planner.canUndo && planner.canEdit && !planner.isSaving ? undo : null, + undo: + outcome && inserted?.mark === planner.historyMark && planner.canUndo && planner.canEdit && !planner.isSaving + ? undo + : null, }; } diff --git a/client/src/store/tourPoiStore.ts b/client/src/store/tourPoiStore.ts index e896c73d0..86c4c534d 100644 --- a/client/src/store/tourPoiStore.ts +++ b/client/src/store/tourPoiStore.ts @@ -8,6 +8,8 @@ export interface InsertedTourPoi { before: { distanceM: number; minutes: number | null } | null; /** How many points the tour had once the place was in; another edit after it ends the comparison. */ pointCount: number; + /** The planner's history mark right after the step that took the place in; undo is offered only while it holds. */ + mark: number; } /** PLN-04: the slot picked for a place, kept until the place is taken in or the search is cleared. */