fix(tours): offer the place's undo only while its insert is the last history step, and explain the add button with a tooltip

This commit is contained in:
Maurice
2026-10-21 11:14:39 +02:00
parent 377619abd1
commit 2377a63a05
7 changed files with 87 additions and 19 deletions
@@ -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<number>());
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', () => {
@@ -10,11 +10,14 @@ const HISTORY_LIMIT = 30;
export function usePlannerHistory<Entry>() {
const [past, setPast] = useState<Entry[]>([]);
const [future, setFuture] = useState<Entry[]>([]);
// 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<Entry>() {
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<Entry>() {
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<Entry>() {
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 };
}
@@ -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,
@@ -158,18 +158,21 @@ export function TourPoiSlotPicker({
)
)}
<div className="flex items-center gap-2">
<button
type="button"
disabled={!!block || locked}
onClick={() => insertion.insert(poi)}
data-testid="tour-poi-add"
title={t('tours.poi.notAPlace')}
className="inline-flex h-8 flex-1 items-center justify-center gap-1.5 rounded-[10px] bg-accent px-3 font-semibold text-accent-text shadow-sm transition-opacity hover:opacity-90 disabled:cursor-default disabled:opacity-40"
style={fs(12, 'body')}
>
<Plus size={14} strokeWidth={2.2} />
{t('tours.poi.add')}
</button>
<Tooltip label={t('tours.poi.notAPlace')} placement="top">
<span className="flex min-w-0 flex-1">
<button
type="button"
disabled={!!block || locked}
onClick={() => insertion.insert(poi)}
data-testid="tour-poi-add"
className="inline-flex h-8 flex-1 items-center justify-center gap-1.5 rounded-[10px] bg-accent px-3 font-semibold text-accent-text shadow-sm transition-opacity hover:opacity-90 disabled:pointer-events-none disabled:opacity-40"
style={fs(12, 'body')}
>
<Plus size={14} strokeWidth={2.2} />
{t('tours.poi.add')}
</button>
</span>
</Tooltip>
{detour && !block && (
<Tooltip label={t('tours.poi.detourHint')} placement="top">
<span data-testid="tour-poi-detour">
@@ -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<PoiInsertionPlanner> = {}): 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(<TourPoiSearchCard planner={planner} />);
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(<TourPoiSearchCard planner={alongPlanner({ waypoints: withPoint, canUndo: false })} />);
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();
});
});
@@ -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,
};
}
+2
View File
@@ -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. */