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.
This commit is contained in:
David Gil Pérez authored and David Gil - Klaus committed 2026-04-20 22:50:27 +02:00
1 parent 01499e7c4e
commit 4439367f4f
5 files changed
+43 -10

No files matched your search

@@ -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<string | null>(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 (
+2
View File
@@ -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;
+8 -2
View File
@@ -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<IAsset[]> => {
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<IAsset[]> => {
const params: Record<string, string> = { 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 }) => {
+1
View File
@@ -159,6 +159,7 @@ export default function PotentialAlbums() {
...contextState.config,
startDate: trip.startDate,
endDate: trip.endDate,
tripCity: trip.cityName,
suggestedAlbumName: trip.suggestedName,
},
})
+29 -5
View File
@@ -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,