From 8692b8790ced1f023942115ad842cde45eeb1b9e Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 1 Nov 2026 10:57:08 +0100 Subject: [PATCH] fix(planner): judge a move by the order a day is drawn in across time zones The checks behind a drag and the up/down buttons read each booking's raw clock, so on a day whose flights were put in the order they happen every move of a timed stop asked to change its time (desktop) or was refused (mobile). They now read the same key the day is ordered by. --- .../src/components/Planner/DayPlanSidebar.tsx | 16 +++++----------- .../screens/trip/plan/planTimelineModel.ts | 14 +++++--------- client/src/utils/dayMerge.test.ts | 9 ++++++++- client/src/utils/dayMerge.ts | 18 ++++++++++++++++-- .../unit/mobile/trip/planTimelineModel.test.ts | 15 +++++++++++++++ 5 files changed, 49 insertions(+), 23 deletions(-) diff --git a/client/src/components/Planner/DayPlanSidebar.tsx b/client/src/components/Planner/DayPlanSidebar.tsx index cb2af0d17..cbf58b702 100644 --- a/client/src/components/Planner/DayPlanSidebar.tsx +++ b/client/src/components/Planner/DayPlanSidebar.tsx @@ -49,7 +49,7 @@ import { getAccommodationAnchors, getDayBookendHotels, shouldDrawMorningLeg, sho import { TRANSPORT_TYPES, parseTimeToMinutes, getSpanPhase, hidesOnMiddleDay, getDisplayTimeForDay, getTransportRouteEndpoints, getTransportForDay as _getTransportForDay, getMergedItems as _getMergedItems, isCarrierTransport, hasCarrierEndpointOnDay, - getAssignmentReservations, timedSlot, storedRideSlot, seatTimedDrop, stopDayIds, + getAssignmentReservations, timedSlot, storedRideSlot, seatTimedDrop, stopDayIds, chronoKeyOf, type MergedItem, } from '../../utils/dayMerge' import { withinDriveRange } from '@trek/shared/roadtrip' @@ -935,12 +935,9 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) { const fromIdx = m.findIndex(i => i.type === fromType && i.data.id === fromId) const toIdx = m.findIndex(matchTo) if (fromIdx !== -1 && toIdx !== -1) { + const keyOf = chronoKeyOf(m, dayId, getDisplayTimeForDay) const timedInOrder = moveRow(m, fromIdx, matchTo, insertAfter).order - .map(i => { - if (i.type === 'transport') return parseTimeToMinutes(getDisplayTimeForDay(i.data, dayId)) - if (i.type === 'place') return parseTimeToMinutes(i.data?.place?.place_time) - return null - }) + .map(i => (i.type === 'note' ? null : keyOf(i))) .filter(t => t !== null) const isChronological = timedInOrder.every((t, i) => i === 0 || t >= timedInOrder[i - 1]) @@ -1829,12 +1826,9 @@ const DayPlanSidebar = React.memo(function DayPlanSidebar(props: DayPlanSidebarP // Check chronological order of all timed items in the new order const placeTime = place.place_time if (parseTimeToMinutes(placeTime) !== null) { + const keyOf = chronoKeyOf(m, day.id, getDisplayTimeForDay) const timedInNewOrder = newOrder - .map(i => { - if (i.type === 'transport') return parseTimeToMinutes(getDisplayTimeForDay(i.data, day.id)) - if (i.type === 'place') return parseTimeToMinutes(i.data?.place?.place_time) - return null - }) + .map(i => (i.type === 'note' ? null : keyOf(i))) .filter(t => t !== null) const isChronological = timedInNewOrder.every((t, i) => i === 0 || t >= timedInNewOrder[i - 1]) if (!isChronological) { diff --git a/client/src/mobile/screens/trip/plan/planTimelineModel.ts b/client/src/mobile/screens/trip/plan/planTimelineModel.ts index 0e362a8e2..01b750f85 100644 --- a/client/src/mobile/screens/trip/plan/planTimelineModel.ts +++ b/client/src/mobile/screens/trip/plan/planTimelineModel.ts @@ -1,6 +1,6 @@ import { - TRANSPORT_TYPES, getAssignmentReservations, getDisplayTimeForDay, getSpanPhase, getTransportRouteEndpoints, hidesOnMiddleDay, - parseTimeToMinutes, + TRANSPORT_TYPES, chronoKeyOf, getAssignmentReservations, getDisplayTimeForDay, getSpanPhase, getTransportRouteEndpoints, + hidesOnMiddleDay, parseTimeToMinutes, } from '../../../../utils/dayMerge' import { getDayBookendHotels, isDayInAccommodationRange } from '../../../../utils/dayOrder' import type { MergedItem } from '../../../../utils/dayMerge' @@ -310,12 +310,8 @@ export function breaksChronology( dayId: number, getDisplayTime: (r: Reservation, dayId: number) => string | null, ): boolean { - const times = order - .map(it => { - if (it.type === 'place') return parseTimeToMinutes((it.data as Assignment).place?.place_time) - if (it.type === 'note') return parseTimeToMinutes((it.data as DayNote).time) - return parseTimeToMinutes(getDisplayTime(it.data as Reservation, dayId)) - }) - .filter((m): m is number => m != null) + // The key the day is drawn by, so flights put in the order they happen across zones still read as in order (#2929). + const keyOf = chronoKeyOf(order, dayId, getDisplayTime) + const times = order.map(keyOf).filter((m): m is number => m != null) return times.some((m, i) => i > 0 && m < times[i - 1]) } diff --git a/client/src/utils/dayMerge.test.ts b/client/src/utils/dayMerge.test.ts index 9511a8b22..b62be0286 100644 --- a/client/src/utils/dayMerge.test.ts +++ b/client/src/utils/dayMerge.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { parseTimeToMinutes, getSpanPhase, hidesOnMiddleDay, getTransportRouteEndpoints, getDisplayTimeForDay, getTransportForDay, getAssignmentReservations, pinnedAway, stopDayIds, getPlaceBookings, getMergedItems, nightRowsOf, rideSeatKey, seatTimedDrop, stayStartingOn, storedRideSlot, timedSlot, type MergedItem } from './dayMerge' +import { parseTimeToMinutes, getSpanPhase, hidesOnMiddleDay, getTransportRouteEndpoints, getDisplayTimeForDay, getTransportForDay, getAssignmentReservations, pinnedAway, stopDayIds, getPlaceBookings, getMergedItems, chronoKeyOf, nightRowsOf, rideSeatKey, seatTimedDrop, stayStartingOn, storedRideSlot, timedSlot, type MergedItem } from './dayMerge' import { planMergedOrder, withTransportPositions } from './mergedOrder' describe('parseTimeToMinutes', () => { @@ -739,4 +739,11 @@ describe('getMergedItems: bookings in different time zones (#2929)', () => { const order = getMergedItems({ dayAssignments: [], dayNotes: [], dayTransports: [toLA, toSeattle], dayId: DAY }) expect(ids(seatTimedDrop([order[1], order[0]], 0, DAY))).toEqual([1, 2]) }) + + it('gives the zoned flights the key they are drawn by, so the drawn order reads as in order', () => { + const items = getMergedItems({ dayAssignments: [], dayNotes: [], dayTransports: [toSeattle, toLA], dayId: DAY }) + const keyOf = chronoKeyOf(items, DAY, getDisplayTimeForDay) + // Drawn Auckland first: it takes the 17:00 slot, the Seattle flight the 20:20 one. + expect(items.map(keyOf)).toEqual([17 * 60, 20 * 60 + 20]) + }) }) diff --git a/client/src/utils/dayMerge.ts b/client/src/utils/dayMerge.ts index 5f10c3d0c..34f33deee 100644 --- a/client/src/utils/dayMerge.ts +++ b/client/src/utils/dayMerge.ts @@ -353,13 +353,27 @@ function applyChronoOrder( dayId: number, getDisplayTime: (r: any, dayId: number) => string | null ): MergedItem[] { + return chronoOrder(items, chronoKeyOf(items, dayId, getDisplayTime)) +} + +/** + * The minutes the day orders an item by: its own clock, or for a booking in a known + * zone the clock slot it takes among the day's zoned bookings (#2929). The checks + * that ask whether a move keeps the day in order read the same key, or a day whose + * flights were put in the order they happen would read as out of order on every move. + */ +export function chronoKeyOf( + items: readonly MergedItem[], + dayId: number, + getDisplayTime: (r: any, dayId: number) => string | null +): (it: MergedItem) => number | null { const clockOf = (it: MergedItem): number | null => { if (it.type === 'place') return parseTimeToMinutes(it.data?.place?.place_time) if (it.type === 'note') return parseTimeToMinutes(it.data?.time) return parseTimeToMinutes(getDisplayTime(it.data, dayId)) } - const zoned = zonedClockSlots(items, dayId, getDisplayTime, clockOf) - return chronoOrder(items, it => (zoned.has(it) ? zoned.get(it) : clockOf(it))) + const zoned = zonedClockSlots([...items], dayId, getDisplayTime, clockOf) + return it => (zoned.has(it) ? (zoned.get(it) as number) : clockOf(it)) } /** diff --git a/client/tests/unit/mobile/trip/planTimelineModel.test.ts b/client/tests/unit/mobile/trip/planTimelineModel.test.ts index 2c81e2eec..de6a830e7 100644 --- a/client/tests/unit/mobile/trip/planTimelineModel.test.ts +++ b/client/tests/unit/mobile/trip/planTimelineModel.test.ts @@ -884,4 +884,19 @@ describe('planTimelineModel — chronology helpers', () => { expect(breaksChronology([nine, bus], 2, getDisplayTime)).toBe(true) expect(getDisplayTime).toHaveBeenCalledWith(bus.data, 2) }) + + it('FE-MOB-PTLM-061: flights drawn in the order they happen across time zones do not read as out of order (#2929)', () => { + const flight = (id: number, time: string, from: string, to: string) => + buildReservation({ + id, type: 'flight', day_id: 2, end_day_id: 2, reservation_time: `2026-05-02T${time}`, + metadata: JSON.stringify({ departure_timezone: from, arrival_timezone: to }), + }) + const toLA = flight(61, '20:20', 'Pacific/Auckland', 'America/Los_Angeles') + const toSeattle = flight(62, '17:00', 'America/Los_Angeles', 'America/Los_Angeles') + const drawn = getMergedItems({ dayAssignments: [], dayNotes: [], dayTransports: [toSeattle, toLA], dayId: 2 }) + expect(drawn.map(i => i.data.id)).toEqual([61, 62]) + const breakfast = placeItem(assignment(11, 0, place(101, 'Breakfast', 48.1, 16.1, '09:00'))) + expect(breaksChronology([breakfast, ...drawn], 2, getDisplayTimeForDay)).toBe(false) + expect(breaksChronology([...drawn, breakfast], 2, getDisplayTimeForDay)).toBe(true) + }) })