Files
TREK/client/src/components/Collections/usePlaceMedia.ts
T
Maurice d008331e5f feat(places): notes, visits, tours, history, tags and location in the place sheet, saved lists on the trip map
Both the desktop sheet and the phone sheet of All places show the place's
location as a badge, its notes per list, its visits from trips (hidden without
any), linked tours and bookings, its change history and the reader's own tags
with a tag filter. A trip map can show the places of linked lists and add one
to the trip. All places reloads after a list is added or removed.
2026-11-29 21:16:51 +01:00

86 lines
3.2 KiB
TypeScript

import type { PlaceMediaFile, PlaceMediaResponse, PlacesLibraryEntry, PlacesLibraryKind } from '@trek/shared';
import { useEffect, useMemo, useState } from 'react';
import { placeVisitsRepo } from '../../repo/placeVisitsRepo';
/** One picture of a place, as the strip and the lightbox show it. */
export interface PlacePhoto {
id: string;
src: string;
thumbSrc: string;
/** Where it comes from: the trip, the list or the journey and its day. */
caption: string;
provider?: string;
mediaType?: string | null;
}
const journeyPhoto = (photoId: number, size: 'thumbnail' | 'original') => `/api/photos/${photoId}/${size}`;
/** The pictures of a place from all its origins, the place's own first, then the journeys', each once. */
export function photosOf(media: PlaceMediaResponse | null): PlacePhoto[] {
if (!media) return [];
const seen = new Set<string>();
const out: PlacePhoto[] = [];
for (const img of media.images) {
if (seen.has(img.image_url)) continue;
seen.add(img.image_url);
out.push({
id: `img:${img.image_url}`,
src: img.image_url,
thumbSrc: img.image_url,
caption: img.kind === 'trip' ? img.trip_title : img.collection_name,
});
}
for (const p of media.journey_photos) {
out.push({
id: `jp:${p.journey_photo_id}`,
src: journeyPhoto(p.photo_id, 'original'),
thumbSrc: journeyPhoto(p.photo_id, 'thumbnail'),
caption: p.caption?.trim() || p.journey_title,
provider: p.provider,
mediaType: p.media_type,
});
}
return out;
}
/** The open entry's origin the media are read through: its first trip place or saved place. */
function originKey(entry: PlacesLibraryEntry | null): string | null {
const o = entry?.origins[0];
if (!o) return null;
return o.kind === 'trip' ? `trip:${o.place_id}` : `list:${o.collection_place_id}`;
}
/**
* The pictures and trip files of a place of All places (PLC-19, PLC-20), for the
* desktop and the phone sheet: the place's own images from its trips and lists,
* the photos of journey entries made from it, and the files a trip attached to
* it, each with where it comes from and only where the reader may open it.
*/
export function usePlaceMedia(entry: PlacesLibraryEntry | null) {
const [media, setMedia] = useState<{ key: string; res: PlaceMediaResponse } | null>(null);
const [open, setOpen] = useState<number | null>(null);
const key = originKey(entry);
useEffect(() => {
if (!key) return;
const [kind, id] = key.split(':');
const ctrl = new AbortController();
placeVisitsRepo
.media(kind as PlacesLibraryKind, Number(id), ctrl.signal)
.then((res) => {
if (!ctrl.signal.aborted) setMedia({ key, res });
})
// Pictures are extra: without them the sheet simply shows none.
.catch(() => {
if (!ctrl.signal.aborted) setMedia({ key, res: { images: [], journey_photos: [], files: [] } });
});
return () => ctrl.abort();
}, [key]);
const current = media && media.key === key ? media.res : null;
const photos = useMemo(() => photosOf(current), [current]);
const files: PlaceMediaFile[] = current?.files ?? [];
return { photos, files, open, setOpen, close: () => setOpen(null) };
}