diff --git a/client/src/components/Planner/DayPlanSidebar.tsx b/client/src/components/Planner/DayPlanSidebar.tsx index 8ad4526ad..9fe3b7e03 100644 --- a/client/src/components/Planner/DayPlanSidebar.tsx +++ b/client/src/components/Planner/DayPlanSidebar.tsx @@ -844,7 +844,7 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) { // Places get sequential integer positions (0, 1, 2, ...) // Non-place items between place N-1 and place N get fractional positions - const { assignmentIds, noteUpdates, transportUpdates, legPosUpdates } = planMergedOrder(newOrder) + const { assignmentIds, noteUpdates, transportUpdates, legPosUpdates } = planMergedOrder(newOrder, prevAssignmentIds) try { // Update transport positions in store FIRST so the useEffect triggered by diff --git a/client/src/pages/tripPlanner/useDayPlanEdits.ts b/client/src/pages/tripPlanner/useDayPlanEdits.ts index 64b4efc53..bb3265eae 100644 --- a/client/src/pages/tripPlanner/useDayPlanEdits.ts +++ b/client/src/pages/tripPlanner/useDayPlanEdits.ts @@ -6,6 +6,7 @@ import { useTripStore, type TripStoreState } from '../../store/tripStore' import { isEffectivelyOffline } from '../../sync/networkMode' import type { Accommodation, Place } from '../../types' import { timedSlot } from '../../utils/dayMerge' +import { withHiddenRows } from '../../utils/mergedOrder' import { restoreVisit, visitOf } from './placeUndo' import type { PlannerBase, PlannerHistory } from './plannerTypes' import type { PlaceEdits } from './usePlaceEdits' @@ -142,9 +143,7 @@ export function useDayPlanEdits(options: DayPlanEditsOptions) { // pinned behind instead of being shifted arithmetically. Without this the // day's detours stayed on their old numbers and the drive quietly took a // different road, persisted and visible to every collaborator. - const visible = new Set(orderedIds) - let nextVisible = 0 - const completeOrder = prevIds.map(id => visible.has(id) ? orderedIds[nextVisible++] : id) + const completeOrder = withHiddenRows(prevIds, orderedIds) const stopIdsBefore = roadtripStopsOf(dayId).map(a => a.id) const stopIdsAfter = completeOrder.filter(id => stopIdsBefore.includes(id)) const plan = reanchorByStopOrder(roadtripVias.byDay[dayId] ?? [], stopIdsBefore, stopIdsAfter) diff --git a/client/src/utils/mergedOrder.test.ts b/client/src/utils/mergedOrder.test.ts index f66433582..a5d5050af 100644 --- a/client/src/utils/mergedOrder.test.ts +++ b/client/src/utils/mergedOrder.test.ts @@ -2,11 +2,13 @@ // phone's plan timeline both derive from a day's new timeline order. import { describe, expect, it } from 'vitest'; +import { getMergedItems } from './dayMerge'; import { keepDayPositions, metadataWithLegPositions, moveRow, planMergedOrder, + withHiddenRows, withTransportPositions, } from './mergedOrder'; @@ -106,3 +108,38 @@ describe('keepDayPositions', () => { expect(keepDayPositions({ id: 1 }, undefined)).toEqual({ id: 1 }); }); }); + +describe('planMergedOrder over a day with hidden rows (#3100)', () => { + it('FE-UTIL-MERGEDORDER-010: hidden rows keep their slots and the shown rows fill the rest in their new order', () => { + expect(withHiddenRows([10, 1, 2, 11], [2, 1])).toEqual([10, 2, 1, 11]); + expect(withHiddenRows([1, 2], [2, 1])).toEqual([2, 1]); + }); + + it('FE-UTIL-MERGEDORDER-011: bookings and notes are positioned in the full day the places are stored in', () => { + const plan = planMergedOrder([transport(8), place(1), note(7), transport(9), place(2)], [10, 1, 2]); + expect(plan.assignmentIds).toEqual([1, 2]); + expect(plan.transportUpdates).toEqual([ + { id: 8, day_plan_position: 0.5 }, + { id: 9, day_plan_position: 1 + 2 / 3 }, + ]); + expect(plan.noteUpdates).toEqual([{ id: 7, sort_order: 1 + 1 / 3 }]); + }); + + it('FE-UTIL-MERGEDORDER-012: an untimed stop dropped between two timed bookings stays there behind a hidden hotel row', () => { + const dayId = 5; + const tour = { id: 8, day_id: dayId, reservation_time: '2026-11-25T12:00' }; + const flight = { id: 9, day_id: dayId, reservation_time: '2026-11-25T19:15' }; + const newOrder = [transport(8), place(1), transport(9), place(2)]; + const plan = planMergedOrder(newOrder, [10, 2, 1]); + const stored = withHiddenRows([10, 2, 1], plan.assignmentIds); + const dayAssignments = stored + .filter((id) => id !== 10) + .map((id) => ({ id, order_index: stored.indexOf(id), place: { place_time: null } })); + const positioned = [tour, flight].map((r) => { + const update = plan.transportUpdates.find((u) => u.id === r.id); + return { ...r, day_positions: { [dayId]: update?.day_plan_position } }; + }); + const drawn = getMergedItems({ dayAssignments, dayNotes: [], dayTransports: positioned, dayId }); + expect(drawn.map((it) => `${it.type}:${it.data.id}`)).toEqual(['transport:8', 'place:1', 'transport:9', 'place:2']); + }); +}); diff --git a/client/src/utils/mergedOrder.ts b/client/src/utils/mergedOrder.ts index 99e388ee8..da32cb596 100644 --- a/client/src/utils/mergedOrder.ts +++ b/client/src/utils/mergedOrder.ts @@ -42,24 +42,49 @@ export function moveRow( return { order: moved, index }; } -export function planMergedOrder(newOrder: readonly MergedOrderItem[]): MergedOrderPlan { +/** + * The day's full stored order once the rows on screen take the order given: a row the + * list does not show keeps its slot, and the shown rows fill the remaining slots in + * their new order. + */ +export function withHiddenRows(storedIds: readonly number[], visibleIds: readonly number[]): number[] { + const visible = new Set(visibleIds); + let next = 0; + return storedIds.map((id) => (visible.has(id) ? visibleIds[next++] : id)); +} + +/** + * `storedIds` is the day's full stored order when the timeline leaves rows out (the + * desktop list hides the stop a lodging booking puts on the day). The places are stored + * across that full day, so a booking or note has to be positioned in the same count, or + * every hidden row ahead of a place pushed that place one slot behind the booking it was + * dropped above (#3100). Without it the timeline is taken to show the whole day. + */ +export function planMergedOrder(newOrder: readonly MergedOrderItem[], storedIds?: readonly number[]): MergedOrderPlan { const plan: MergedOrderPlan = { assignmentIds: [], noteUpdates: [], transportUpdates: [], legPosUpdates: {} }; - let placeCount = 0; + for (const item of newOrder) if (item.type === 'place') plan.assignmentIds.push(item.data.id); + const full = storedIds ? withHiddenRows(storedIds, plan.assignmentIds) : plan.assignmentIds; + const slotOf = (id: number): number => { + const at = full.indexOf(id); + return at === -1 ? plan.assignmentIds.indexOf(id) : at; + }; + let lastPlaceId: number | null = null; let i = 0; while (i < newOrder.length) { if (newOrder[i].type === 'place') { - plan.assignmentIds.push(newOrder[i].data.id); - placeCount++; + lastPlaceId = newOrder[i].data.id; i++; continue; } - // Consecutive non-place items share the gap after the place before them. + // Consecutive non-place items share the gap after the place before them, or the + // gap ahead of the first place when they open the day. const group: MergedOrderItem[] = []; while (i < newOrder.length && newOrder[i].type !== 'place') { group.push(newOrder[i]); i++; } - const base = placeCount > 0 ? placeCount - 1 : -1; + const nextPlace = newOrder.slice(i).find((it) => it.type === 'place'); + const base = lastPlaceId !== null ? slotOf(lastPlaceId) : nextPlace ? slotOf(nextPlace.data.id) - 1 : -1; group.forEach((g, idx) => { const pos = base + (idx + 1) / (group.length + 1); if (g.type === 'note') plan.noteUpdates.push({ id: g.data.id, sort_order: pos });