Files
TREK/client/src/mobile/screens/trip/places/placesBrowserModel.ts
T
Maurice 4d6820a4d4 @
fix(planner): route a layover flight per leg, and count linked places as planned

Two v4.0.0 reports, both a surface answering a question with less than it had.

The map built its own waypoint order. It read the BOOKING position and never
expanded metadata.legs, while a multi-leg booking stores its position per leg —
so a Frankfurt to Vancouver flight via Reykjavik was dropped from the list and
the airport before it was joined to the airport after it in one driving run
across the Atlantic. It now takes the order from getMergedItems, the same one
the day plan renders, which brings the span filter and the leg expansion with
it. getMergedItems in turn had never read day_plan_position, the one slot the
map did read, which is how the two could disagree at all.

getTransportRouteEndpoints handed back the booking outermost airports for every
leg. A stop between two legs was therefore routed from the arrival airport back
to the departure one — a drive from Vancouver to Frankfurt with a cafe in
Reykjavik on the way. A leg now spans its own pair, when the endpoints line up
one per stop with the legs; anything ambiguous keeps the role lookup.

Being planned meant one thing: a day_assignments row. A hotel is linked through
its stay and a venue through its booking, and neither is ever dragged onto a
day, so both sat under Unplanned while the day header printed their names. The
three places that asked the question — desktop sidebar, map markers, phone
browser — now ask one helper that also counts a stay and a day-anchored
booking. A booking that names no day still counts as unplanned, because nothing
says when it happens, and the day plan renders nothing for it either.
@
2026-08-27 19:21:06 +02:00

78 lines
3.0 KiB
TypeScript

import type { AssignmentsMap, Day, Place } from '../../../../types'
import {
plannedPlaceIds as sharedPlannedPlaceIds,
type PlannedAccommodation, type PlannedReservation,
} from '../../../../utils/plannedPlaces'
/**
* Pure filter model of the mobile places browser. Mirrors the desktop
* sidebar's semantics exactly (usePlacesSidebar + the map's mapPlaces memo):
* the pool filter and category set come from the trip store, so the list here
* and the map markers always agree (#1541).
*/
export function plannedPlaceIds(
assignments: AssignmentsMap,
accommodations: PlannedAccommodation[] = [],
reservations: PlannedReservation[] = [],
): Set<number> {
return sharedPlannedPlaceIds({ assignments, accommodations, reservations })
}
/** place id → number of the first (lowest-numbered) day it is planned on. */
export function firstPlannedDayNumbers(assignments: AssignmentsMap, days: Day[]): Map<number, number> {
const numberByDayId = new Map<number, number>()
days.forEach((day, i) => numberByDayId.set(day.id, day.day_number ?? i + 1))
const result = new Map<number, number>()
for (const [dayId, dayAssignments] of Object.entries(assignments)) {
const dayNumber = numberByDayId.get(Number(dayId))
if (dayNumber == null) continue
for (const a of dayAssignments) {
const placeId = a.place?.id
if (placeId == null) continue
const prev = result.get(placeId)
if (prev == null || dayNumber < prev) result.set(placeId, dayNumber)
}
}
return result
}
export function matchesCategoryFilter(place: Place, categoryFilters: Set<string>): boolean {
if (categoryFilters.size === 0) return true
if (place.category_id == null) return categoryFilters.has('uncategorized')
return categoryFilters.has(String(place.category_id))
}
export function matchesSearch(place: Place, search: string): boolean {
if (!search) return true
const q = search.toLowerCase()
return place.name.toLowerCase().includes(q) || (place.address || '').toLowerCase().includes(q)
}
interface PoolFilterArgs {
filter: string
categoryFilters: Set<string>
search: string
plannedIds: Set<number>
}
export function filterPool(places: Place[], { filter, categoryFilters, search, plannedIds }: PoolFilterArgs): Place[] {
return places.filter(p => {
if (filter === 'unplanned' && plannedIds.has(p.id)) return false
if (filter === 'planned' && !plannedIds.has(p.id)) return false
if (filter === 'tracks' && !p.route_geometry) return false
return matchesCategoryFilter(p, categoryFilters) && matchesSearch(p, search)
})
}
/** Chip counts run on the category+search base set, like the desktop tabs. */
export function poolCounts(places: Place[], categoryFilters: Set<string>, search: string, plannedIds: Set<number>) {
const base = places.filter(p => matchesCategoryFilter(p, categoryFilters) && matchesSearch(p, search))
return {
all: base.length,
unplanned: base.filter(p => !plannedIds.has(p.id)).length,
planned: base.filter(p => plannedIds.has(p.id)).length,
tracks: base.filter(p => p.route_geometry).length,
}
}