From 9529b55bd7b650161f650c445d069a2dae4a0f7f Mon Sep 17 00:00:00 2001 From: Maurice Date: Thu, 8 Oct 2026 21:20:32 +0000 Subject: [PATCH] refactor(planner): share the booking files filter between desktop and phone filesFor moves out of the desktop booking parts into utils, and the phone transport sheet now reads a booking's files through it as the desktop transport detail and booking cards do. --- .../DayPlanSidebarTransportDetailModal.tsx | 3 ++- .../components/Planner/ReservationsPanel.tsx | 3 ++- .../Planner/bookings/BookingDetailHost.tsx | 2 +- .../Planner/bookings/bookingParts.tsx | 4 ---- .../screens/trip/sheets/MTransportSheet.tsx | 5 ++--- client/src/utils/reservationFiles.test.ts | 22 +++++++++++++++++++ client/src/utils/reservationFiles.ts | 8 +++++++ 7 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 client/src/utils/reservationFiles.test.ts create mode 100644 client/src/utils/reservationFiles.ts diff --git a/client/src/components/Planner/DayPlanSidebarTransportDetailModal.tsx b/client/src/components/Planner/DayPlanSidebarTransportDetailModal.tsx index 115149181..ddd614c2b 100644 --- a/client/src/components/Planner/DayPlanSidebarTransportDetailModal.tsx +++ b/client/src/components/Planner/DayPlanSidebarTransportDetailModal.tsx @@ -10,7 +10,8 @@ import { useTripStore } from '../../store/tripStore' import { formatTime, splitReservationDateTime } from '../../utils/formatters' import { parseReservationMetadata } from '../../utils/flightLegs' import { BookingDialogHeader } from './bookings/BookingDialogShell' -import { BOX, Eyebrow, Field, filesFor, fs, toneColor, toneOf } from './bookings/bookingParts' +import { BOX, Eyebrow, Field, fs, toneColor, toneOf } from './bookings/bookingParts' +import { filesFor } from '../../utils/reservationFiles' import { TransitLegs, transitSummary } from './bookings/transitParts' import type { Reservation } from '../../types' diff --git a/client/src/components/Planner/ReservationsPanel.tsx b/client/src/components/Planner/ReservationsPanel.tsx index 42d8d95ba..f3c1f677a 100644 --- a/client/src/components/Planner/ReservationsPanel.tsx +++ b/client/src/components/Planner/ReservationsPanel.tsx @@ -13,7 +13,8 @@ import { type BookingsKind, } from './bookings/bookingsModel' import { bookingFacts, formatDay, type BookingFacts } from './bookings/bookingFacts' -import { filesFor, SectionHead, fs } from './bookings/bookingParts' +import { SectionHead, fs } from './bookings/bookingParts' +import { filesFor } from '../../utils/reservationFiles' import { useBookingsView } from './bookings/useBookingsView' import BookingsHeader from './bookings/BookingsHeader' import BookingCard from './bookings/BookingCard' diff --git a/client/src/components/Planner/bookings/BookingDetailHost.tsx b/client/src/components/Planner/bookings/BookingDetailHost.tsx index 6871c70c8..26b9441e3 100644 --- a/client/src/components/Planner/bookings/BookingDetailHost.tsx +++ b/client/src/components/Planner/bookings/BookingDetailHost.tsx @@ -8,7 +8,7 @@ import { usePluginViewContributions } from '../../Plugins/PluginContributions' import { TRANSPORT_TYPES } from '../../../utils/dayMerge' import { buildAssignmentLookup, costsFor } from './bookingsModel' import { bookingFacts } from './bookingFacts' -import { filesFor } from './bookingParts' +import { filesFor } from '../../../utils/reservationFiles' import { canShowOnMap } from './showOnMap' import { useBookingActions } from './useBookingActions' import { useReservationDetailPlugins } from './useReservationDetailPlugins' diff --git a/client/src/components/Planner/bookings/bookingParts.tsx b/client/src/components/Planner/bookings/bookingParts.tsx index 2f42f838b..c4b49352a 100644 --- a/client/src/components/Planner/bookings/bookingParts.tsx +++ b/client/src/components/Planner/bookings/bookingParts.tsx @@ -276,10 +276,6 @@ export function useOpenFile() { return (f: TripFile) => { openFile(f.url, f.original_name).catch(() => toast.error(t('files.openError'))) } } -export function filesFor(r: Reservation, files: TripFile[]): TripFile[] { - return files.filter(f => !f.deleted_at && (f.reservation_id === r.id || (f.linked_reservation_ids || []).includes(r.id))) -} - /** The attached files, one framed row each, opening the file. */ export function FileRows({ files }: { files: TripFile[] }) { const open = useOpenFile() diff --git a/client/src/mobile/screens/trip/sheets/MTransportSheet.tsx b/client/src/mobile/screens/trip/sheets/MTransportSheet.tsx index 64141dbda..22c31294e 100644 --- a/client/src/mobile/screens/trip/sheets/MTransportSheet.tsx +++ b/client/src/mobile/screens/trip/sheets/MTransportSheet.tsx @@ -9,6 +9,7 @@ import { splitReservationDateTime } from '../../../../utils/formatters' import { getFlightLegs, getTrainLegs, usesStationRoute } from '../../../../utils/flightLegs' import { openFile } from '../../../../utils/fileDownload' import { runsOnDay } from '../../../../utils/reservationRoutes' +import { filesFor } from '../../../../utils/reservationFiles' import type { Reservation } from '../../../../types' import { Eyebrow, INNER_CLS, StatBox, TileHeader, displayTime } from './MTripSheetUi' @@ -102,9 +103,7 @@ export default function MTransportSheet({ planner, shell }: MTripSheetsProps) { const routeLegs = res.type === 'flight' ? getFlightLegs(res) : usesStationRoute(res.type) ? getTrainLegs(res) : [] const legCodes = routeLegs.length > 1 ? routeLegs.filter(l => l.confirmation_number) : [] - const resFiles = (planner.files || []).filter(f => - !f.deleted_at && (f.reservation_id === res.id || (f.linked_reservation_ids || []).includes(res.id)), - ) + const resFiles = filesFor(res, planner.files || []) const confirmed = res.status === 'confirmed' const codeBlurred = blurCodes && !codeRevealed diff --git a/client/src/utils/reservationFiles.test.ts b/client/src/utils/reservationFiles.test.ts new file mode 100644 index 000000000..3eb3626f8 --- /dev/null +++ b/client/src/utils/reservationFiles.test.ts @@ -0,0 +1,22 @@ +// FE-UTIL-RESFILES-001 to -002: the files a booking shows, on the desktop booking +// cards and details and on the phone's booking and transport tabs and sheet. +import { describe, expect, it } from 'vitest'; + +import { buildTripFile } from '../../tests/helpers/factories'; +import { filesFor } from './reservationFiles'; + +describe('filesFor', () => { + it('FE-UTIL-RESFILES-001: a booking keeps its own files and the ones linked to it', () => { + const own = buildTripFile({ id: 1, reservation_id: 5 }); + const linked = buildTripFile({ id: 2, reservation_id: null, linked_reservation_ids: [9, 5] }); + const other = buildTripFile({ id: 3, reservation_id: 6, linked_reservation_ids: [7] }); + const loose = buildTripFile({ id: 4, reservation_id: null }); + expect(filesFor({ id: 5 }, [own, linked, other, loose]).map((f) => f.id)).toEqual([1, 2]); + }); + + it('FE-UTIL-RESFILES-002: a trashed file is left out', () => { + const trashed = buildTripFile({ id: 1, reservation_id: 5, deleted_at: '2026-01-01T00:00:00Z' }); + const trashedLink = buildTripFile({ id: 2, linked_reservation_ids: [5], deleted_at: '2026-01-01T00:00:00Z' }); + expect(filesFor({ id: 5 }, [trashed, trashedLink])).toEqual([]); + }); +}); diff --git a/client/src/utils/reservationFiles.ts b/client/src/utils/reservationFiles.ts new file mode 100644 index 000000000..00fc7a20d --- /dev/null +++ b/client/src/utils/reservationFiles.ts @@ -0,0 +1,8 @@ +import type { Reservation, TripFile } from '../types'; + +/** A booking's files: the ones uploaded to it and the ones linked to it, trashed files left out. */ +export function filesFor(r: Pick, files: TripFile[]): TripFile[] { + return files.filter( + (f) => !f.deleted_at && (f.reservation_id === r.id || (f.linked_reservation_ids || []).includes(r.id)) + ); +}