From 4439367f4f8ffa5ee72415dd1838e2c04d0414b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gil=20P=C3=A9rez?= Date: Mon, 20 Apr 2026 22:49:02 +0200 Subject: [PATCH] feat(potential-trips): filter trip assets by trip city The trip-assets endpoint was returning every orphan asset in the date range, so screenshots, photos taken at the airport coming back, or anything else timestamped during the trip days mixed in. The right panel ended up showing photos from multiple cities for one trip selection. Pass the trip's dominant city through the context to the asset fetcher and add an optional 'city' filter on /api/albums/potential-trip-assets: WHERE e.city = :city OR e.city IS NULL NULL is included so we don't lose photos taken during the trip whose EXIF lacks a city tag (no GPS, screenshots edited later, older devices, etc.). When no city is provided (single-day Days view, or a trip whose city couldn't be resolved) the previous date-only behaviour is preserved. --- .../PotentialAlbumsAssets.tsx | 6 ++-- src/contexts/PhotoSelectionContext.tsx | 2 ++ src/handlers/api/album.handler.ts | 10 ++++-- src/pages/albums/potential-albums.tsx | 1 + src/pages/api/albums/potential-trip-assets.ts | 34 ++++++++++++++++--- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/components/albums/potential-albums/PotentialAlbumsAssets.tsx b/src/components/albums/potential-albums/PotentialAlbumsAssets.tsx index 82e93c4..c261847 100644 --- a/src/components/albums/potential-albums/PotentialAlbumsAssets.tsx +++ b/src/components/albums/potential-albums/PotentialAlbumsAssets.tsx @@ -17,7 +17,7 @@ export default function PotentialAlbumsAssets() { updateContext, config } = usePhotoSelectionContext(); - const { startDate, endDate } = config; + const { startDate, endDate, tripCity } = config; const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); @@ -31,7 +31,7 @@ export default function PotentialAlbumsAssets() { }); // If endDate is set we're in trip-range mode, else single-day mode. const fetcher = endDate - ? listPotentialTripAssets(startDate, endDate) + ? listPotentialTripAssets(startDate, endDate, tripCity) : listPotentialAlbumsAssets({ startDate }); return fetcher .then((fetchedAssets) => updateContext({ assets: fetchedAssets })) @@ -70,7 +70,7 @@ export default function PotentialAlbumsAssets() { useEffect(() => { if (startDate) fetchAssets(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [startDate, endDate]); + }, [startDate, endDate, tripCity]); if (loading) return ( diff --git a/src/contexts/PhotoSelectionContext.tsx b/src/contexts/PhotoSelectionContext.tsx index fcfba23..e9f6acf 100644 --- a/src/contexts/PhotoSelectionContext.tsx +++ b/src/contexts/PhotoSelectionContext.tsx @@ -7,6 +7,8 @@ export interface IPhotoSelectionConfig { // Common config potentially used by multiple features startDate?: string; endDate?: string; + /** Trip's dominant city, used to filter the asset list to that location. */ + tripCity?: string | null; /** Pre-filled name suggestion for a new album (e.g. "20180401 - Lisboa"). */ suggestedAlbumName?: string; diff --git a/src/handlers/api/album.handler.ts b/src/handlers/api/album.handler.ts index 5d59b06..32d26ec 100644 --- a/src/handlers/api/album.handler.ts +++ b/src/handlers/api/album.handler.ts @@ -50,8 +50,14 @@ export const listPotentialTrips = async (filters?: IPotentialTripsFilters): Prom return API.get(LIST_POTENTIAL_TRIPS_PATH, filters); } -export const listPotentialTripAssets = async (startDate: string, endDate: string): Promise => { - return API.get(LIST_POTENTIAL_TRIP_ASSETS_PATH, { startDate, endDate }).then((assets) => assets.map(cleanUpAsset)); +export const listPotentialTripAssets = async ( + startDate: string, + endDate: string, + city?: string | null, +): Promise => { + const params: Record = { startDate, endDate }; + if (city) params.city = city; + return API.get(LIST_POTENTIAL_TRIP_ASSETS_PATH, params).then((assets) => assets.map(cleanUpAsset)); } export const listAlbums = async (filters?: { sortBy?: string, sortOrder?: string }) => { diff --git a/src/pages/albums/potential-albums.tsx b/src/pages/albums/potential-albums.tsx index 4b69a72..f03c48f 100644 --- a/src/pages/albums/potential-albums.tsx +++ b/src/pages/albums/potential-albums.tsx @@ -159,6 +159,7 @@ export default function PotentialAlbums() { ...contextState.config, startDate: trip.startDate, endDate: trip.endDate, + tripCity: trip.cityName, suggestedAlbumName: trip.suggestedName, }, }) diff --git a/src/pages/api/albums/potential-trip-assets.ts b/src/pages/api/albums/potential-trip-assets.ts index 8c8fae3..ec6bd9e 100644 --- a/src/pages/api/albums/potential-trip-assets.ts +++ b/src/pages/api/albums/potential-trip-assets.ts @@ -5,8 +5,22 @@ import { isFlipped } from "@/helpers/asset.helper"; import { sql } from "drizzle-orm"; import type { NextApiRequest, NextApiResponse } from "next"; -const SELECT_ORPHAN_RANGE = (startDate: string, endDate: string, ownerId: string) => - sql.raw(` +// Optional `city` narrows the result to assets whose EXIF city matches the +// trip's dominant city. Assets with NULL city are included alongside the +// match: they may be photos taken during the trip but without GPS metadata +// (screenshots, edited copies, older devices, etc.) and excluding them would +// lose real trip content. If `city` is not provided the date range alone +// determines membership. +const SELECT_ORPHAN_RANGE = ( + startDate: string, + endDate: string, + ownerId: string, + city: string | null +) => { + const cityClause = city + ? `AND (e."city" = '${city.replace(/'/g, "''")}' OR e."city" IS NULL)` + : ""; + return sql.raw(` SELECT a."id", a."ownerId", @@ -21,7 +35,9 @@ const SELECT_ORPHAN_RANGE = (startDate: string, endDate: string, ownerId: string e."exifImageWidth", e."exifImageHeight", e."dateTimeOriginal", - e."orientation" + e."orientation", + e."city", + e."country" FROM asset a LEFT JOIN @@ -35,16 +51,22 @@ const SELECT_ORPHAN_RANGE = (startDate: string, endDate: string, ownerId: string AND a."ownerId" = '${ownerId}' AND e."dateTimeOriginal"::date BETWEEN '${startDate}' AND '${endDate}' AND a."visibility" = 'timeline' + ${cityClause} ORDER BY e."dateTimeOriginal" ASC `); +}; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { const currentUser = await getCurrentUser(req); if (!currentUser) return res.status(401).json({ message: "Unauthorized" }); - const { startDate, endDate } = req.query as { startDate?: string; endDate?: string }; + const { startDate, endDate, city } = req.query as { + startDate?: string; + endDate?: string; + city?: string; + }; if (!startDate || !endDate) { return res.status(400).json({ error: "startDate and endDate are required (YYYY-MM-DD)" }); } @@ -56,7 +78,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return res.status(400).json({ error: "Invalid date format, expected YYYY-MM-DD" }); } - const { rows } = await db.execute(SELECT_ORPHAN_RANGE(startDate, endDate, currentUser.id)); + const { rows } = await db.execute( + SELECT_ORPHAN_RANGE(startDate, endDate, currentUser.id, city ?? null) + ); const cleaned = rows.map((row: any) => ({ ...row,