test(places): the picture chosen in the editor, the gallery and the lightbox

Tests for the picker, the shared image field, staging and saving the picture,
the gallery hook and trigger, the lightbox's dialog, focus and reading
direction, the credit and the enrichment cache; the inspector, phone sheet,
details column, form and wiring tests follow the picture into the editor.
The picker's upload tile and credit line are components of their own, the
heading goes with an empty picker, the gallery's state names its place, and
'No picture' only shows when a picture is being removed.
This commit is contained in:
Maurice
2026-10-23 20:06:09 +02:00
parent 35566690e9
commit bebfeeff97
25 changed files with 1687 additions and 199 deletions
@@ -40,12 +40,20 @@ export function usePlaceGallery(place: Place, language: string): UsePlaceGallery
const placesEnrichEnabled = useAuthStore((s) => s.placesEnrichEnabled);
const { t } = useTranslation();
const toast = useToast();
const [candidates, setCandidates] = useState<PlacePhotoCandidate[] | null>(null);
const [isOpen, setIsOpen] = useState(false);
const [loading, setLoading] = useState(false);
// The place a request was made for: an answer that comes back after the user
// moved on to another place is dropped instead of opening over it.
const requestRef = useRef(0);
// Each piece of state names the place it belongs to: a different place reads
// as nothing loaded, nothing open, so the previous answer never flashes.
const [candidatesFor, setCandidatesFor] = useState<{ placeId: number; list: PlacePhotoCandidate[] } | null>(null);
const [openFor, setOpenFor] = useState<number | null>(null);
const [loadingFor, setLoadingFor] = useState<number | null>(null);
const placeId = place.id;
const candidates = candidatesFor?.placeId === placeId ? candidatesFor.list : null;
const isOpen = openFor === placeId;
const loading = loadingFor === placeId;
// The place on screen now, for an answer that arrives after the user moved on.
const shownRef = useRef(placeId);
useEffect(() => {
shownRef.current = placeId;
}, [placeId]);
const selection = useMemo<PlaceDetailsSelection | null>(() => {
if (place.lat == null || place.lng == null) return null;
@@ -60,15 +68,6 @@ export function usePlaceGallery(place: Place, language: string): UsePlaceGallery
};
}, [place.google_place_id, place.amap_poi_id, place.osm_id, place.lat, place.lng, place.name]);
// A different place owns different pictures: drop the previous answer so the
// next open never flashes them.
useEffect(() => {
requestRef.current += 1;
setCandidates(null);
setIsOpen(false);
setLoading(false);
}, [place.id]);
const photos = useMemo<GalleryPhoto[]>(() => {
const list: GalleryPhoto[] = [];
if (place.image_url) list.push({ id: 'custom', src: place.image_url, caption: null, captionHref: null });
@@ -86,26 +85,26 @@ export function usePlaceGallery(place: Place, language: string): UsePlaceGallery
const canOpen = !!place.image_url || (placesEnrichEnabled && !!selection);
const close = useCallback(() => setIsOpen(false), []);
const close = useCallback(() => setOpenFor(null), []);
const open = useCallback(() => {
// The custom image is already on screen — open now and let any provider
// pictures join it as they arrive.
if (place.image_url) setIsOpen(true);
if (place.image_url) setOpenFor(placeId);
if (!placesEnrichEnabled || !selection) {
if (!place.image_url) toast.info(t('places.details.noPhotos'));
return;
}
if (candidates !== null) {
if (place.image_url || candidates.length) setIsOpen(true);
if (place.image_url || candidates.length) setOpenFor(placeId);
else toast.info(t('places.details.noPhotos'));
return;
}
const apply = (list: PlacePhotoCandidate[]) => {
setCandidates(list);
if (place.image_url || list.length) setIsOpen(true);
setCandidatesFor({ placeId, list });
if (place.image_url || list.length) setOpenFor(placeId);
else toast.info(t('places.details.noPhotos'));
};
@@ -115,22 +114,23 @@ export function usePlaceGallery(place: Place, language: string): UsePlaceGallery
return;
}
const request = ++requestRef.current;
const current = () => request === requestRef.current;
setLoading(true);
// An answer for a place the user has left is kept for that place, but opens nothing and says nothing.
const current = () => shownRef.current === placeId;
setLoadingFor(placeId);
mapsApi
.placeEnrichment({ ...selection, lang: language })
.then((result) => {
storeEnrichment(selection, language, result);
if (current()) apply(result.photos);
else setCandidatesFor({ placeId, list: result.photos });
})
.catch((err: unknown) => {
if (current() && !place.image_url) toast.error(translateApiError(t, err, 'places.details.error'));
})
.finally(() => {
if (current()) setLoading(false);
setLoadingFor((now) => (now === placeId ? null : now));
});
}, [place.image_url, placesEnrichEnabled, selection, candidates, language, t, toast]);
}, [place.image_url, placeId, placesEnrichEnabled, selection, candidates, language, t, toast]);
return { photos, open, close, isOpen, loading, canOpen };
}