diff --git a/client/src/components/Planner/TransitSearchPanel.test.tsx b/client/src/components/Planner/TransitSearchPanel.test.tsx index c0fa35c30..1cf79228d 100644 --- a/client/src/components/Planner/TransitSearchPanel.test.tsx +++ b/client/src/components/Planner/TransitSearchPanel.test.tsx @@ -289,6 +289,52 @@ describe('TransitSearchPanel', () => { expect(screen.queryByText('No coords')).not.toBeInTheDocument() }) + it('FE-PLANNER-TRANSIT-010b: the stay the day starts or ends in survives a day full of stops (#2538)', async () => { + const user = userEvent.setup() + const prev = buildDay({ id: 9, trip_id: 1, day_number: 1, date: '2025-05-31' }) + const today = buildDay({ id: 10, trip_id: 1, day_number: 2, date: '2025-06-01', title: 'Berlin Day' }) + const next = buildDay({ id: 11, trip_id: 1, day_number: 3, date: '2025-06-02' }) + const stay = (id: number, name: string, start: number, end: number, lat: number) => + ({ id, trip_id: 1, place_id: 100 + id, start_day_id: start, end_day_id: end, place_name: name, place_lat: lat, place_lng: 13.38 }) as Accommodation + const accommodations = [ + stay(2, 'Later Hostel', 11, 11, 52.3), + stay(1, 'Hotel Adlon', 9, 11, 52.5163), + ] + // Nine located stops: the old list kept the first eight places and cut every stay. + const places = Array.from({ length: 9 }, (_, i) => buildPlace({ id: i + 1, name: `Stop ${i + 1}`, lat: 52.4 + i / 100, lng: 13.4 })) + render() + + const [fromInput, toInput] = screen.getAllByPlaceholderText('Search stop or station…') + await user.click(fromInput) + const picks = (await screen.findAllByRole('button', { name: /Hotel Adlon|Stop \d|Hostel/ })).map(b => b.textContent) + expect(picks[0]).toBe('Hotel Adlon') + expect(picks.slice(1)).toEqual(['Stop 1', 'Stop 2', 'Stop 3', 'Stop 4', 'Stop 5', 'Stop 6', 'Stop 7', 'Stop 8']) + + await user.click(screen.getByRole('button', { name: 'Hotel Adlon' })) + expect(fromInput).toHaveValue('Hotel Adlon') + await user.click(toInput) + expect(await screen.findByRole('button', { name: 'Hotel Adlon' })).toBeInTheDocument() + }) + + it('FE-PLANNER-TRANSIT-010c: a transfer day offers the stay it leaves and the one it arrives at, then the other stays', async () => { + const user = userEvent.setup() + const d1 = buildDay({ id: 9, trip_id: 1, day_number: 1, date: '2025-05-31' }) + const d2 = buildDay({ id: 10, trip_id: 1, day_number: 2, date: '2025-06-01', title: 'Berlin Day' }) + const d3 = buildDay({ id: 11, trip_id: 1, day_number: 3, date: '2025-06-02' }) + const d4 = buildDay({ id: 12, trip_id: 1, day_number: 4, date: '2025-06-03' }) + const stay = (id: number, name: string, start: number, end: number, lat: number) => + ({ id, trip_id: 1, place_id: 100 + id, start_day_id: start, end_day_id: end, place_name: name, place_lat: lat, place_lng: 13.38 }) as Accommodation + const accommodations = [ + stay(3, 'Final Inn', 12, 12, 52.2), + stay(2, 'Arrival Hotel', 10, 11, 52.3), + stay(1, 'Departure Hotel', 9, 10, 52.5), + ] + render() + await user.click(screen.getAllByPlaceholderText('Search stop or station…')[0]) + const picks = (await screen.findAllByRole('button', { name: /Hotel|Inn|Fernsehturm/ })).map(b => b.textContent) + expect(picks).toEqual(['Departure Hotel', 'Arrival Hotel', 'Fernsehturm', 'Final Inn']) + }) + it('FE-PLANNER-TRANSIT-011: hovering a quick pick and a geocode result toggles the row background', async () => { const user = userEvent.setup() render() diff --git a/client/src/components/Planner/TransitSearchPanel.tsx b/client/src/components/Planner/TransitSearchPanel.tsx index 15c2ba216..173142091 100644 --- a/client/src/components/Planner/TransitSearchPanel.tsx +++ b/client/src/components/Planner/TransitSearchPanel.tsx @@ -7,6 +7,7 @@ import { transitApi } from '../../api/client' import { useSettingsStore } from '../../store/settingsStore' import { useToast } from '../shared/Toast' import { useTranslation } from '../../i18n' +import { getDayBookendHotels } from '../../utils/dayOrder' import type { Day, Place, Accommodation } from '../../types' import type { TransitProvider } from '@trek/shared' @@ -101,6 +102,41 @@ function fmtDuration(seconds: number, t: (k: string, p?: Record 0 ? `${h} h ${m} min` : `${h} h` } +// ── quick picks ────────────────────────────────────────────────────────────── + +const MAX_QUICK_PICKS = 8 + +function stayPick(a: Accommodation): PickedPlace | null { + return a.place_lat != null && a.place_lng != null && a.place_name ? { name: a.place_name, lat: a.place_lat, lng: a.place_lng } : null +} + +/** + * What the from/to fields offer before anything is typed. The stay the day + * starts in and the one it ends in come first and are never cut, since most + * connections of a day begin or end there (#2538); then the day's own located + * places and the trip's other stays, up to MAX_QUICK_PICKS. + */ +function buildQuickPicks(day: Day, days: Day[], places: Place[], accommodations: Accommodation[]): PickedPlace[] { + const { morning, evening } = getDayBookendHotels(day, days, accommodations) + const dayStays = [morning, evening].filter((a): a is Accommodation => a != null) + + const seen = new Set() + const unique = (p: PickedPlace | null): p is PickedPlace => { + if (!p) return false + const k = `${p.name}:${p.lat}` + if (seen.has(k)) return false + seen.add(k) + return true + } + + const stays = dayStays.map(stayPick).filter(unique) + const rest = [ + ...places.map(p => (p.lat != null && p.lng != null ? { name: p.name, lat: p.lat, lng: p.lng } : null)), + ...accommodations.filter(a => !dayStays.includes(a)).map(stayPick), + ].filter(unique) + return [...stays, ...rest.slice(0, MAX_QUICK_PICKS)] +} + // ── from/to stop picker ────────────────────────────────────────────────────── function StopPicker({ label, value, onPick, quickPicks, near, placeholder }: { @@ -366,21 +402,7 @@ export default function TransitSearchPanel({ day, days, places, accommodations = const [addingIdx, setAddingIdx] = useState(null) const [provider, setProvider] = useState(null) - // Quick picks: the day's located places, plus the trip's located accommodations. - const quickPicks = useMemo(() => { - const picks: PickedPlace[] = [] - for (const p of places) { - if (p.lat != null && p.lng != null) picks.push({ name: p.name, lat: p.lat, lng: p.lng }) - } - for (const a of accommodations) { - const lat = (a as { place_lat?: number | null }).place_lat - const lng = (a as { place_lng?: number | null }).place_lng - const name = (a as { place_name?: string | null }).place_name - if (lat != null && lng != null && name) picks.push({ name, lat, lng }) - } - const seen = new Set() - return picks.filter(p => { const k = `${p.name}:${p.lat}`; if (seen.has(k)) return false; seen.add(k); return true }).slice(0, 8) - }, [places, accommodations]) + const quickPicks = useMemo(() => buildQuickPicks(day, days, places, accommodations), [day, days, places, accommodations]) const near = quickPicks.length > 0 ? `${quickPicks[0].lat},${quickPicks[0].lng}` : null