mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
feat(roadtrip): show the place photo on the stop sheet and the stop dialog (#2629)
On the road a stop's picture is often what decides whether it is worth the detour. The phone's stop sheet shows the place's picture full width under its name and the desktop stop dialog shows it in place of the kind tile; both open the place's photos on tap. The picture is the place's own image, else the provider photo for its Google or OSM id through the shared photo cache.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// FE-SHARED-PLACEPHOTO-001 to FE-SHARED-PLACEPHOTO-005
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { fetchPhoto, getCached } from '../../services/photoService';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { usePlacePhoto } from './usePlacePhoto';
|
||||
|
||||
vi.mock('../../services/photoService', () => ({ fetchPhoto: vi.fn(), getCached: vi.fn() }));
|
||||
|
||||
const PROXY = '/api/maps/place-photo/ChIJ123/bytes';
|
||||
|
||||
beforeEach(() => {
|
||||
resetAllStores();
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getCached).mockReturnValue(undefined);
|
||||
vi.mocked(fetchPhoto).mockImplementation((_key, _id, _lat, _lng, _name, callback) => {
|
||||
callback?.({ photoUrl: PROXY, thumbDataUrl: null });
|
||||
});
|
||||
});
|
||||
|
||||
describe('usePlacePhoto (#2629)', () => {
|
||||
it("FE-SHARED-PLACEPHOTO-001: the place's own picture wins and asks the provider for nothing", () => {
|
||||
const { result } = renderHook(() =>
|
||||
usePlacePhoto({ name: 'Dom', image_url: '/uploads/places/dom.jpg', google_place_id: 'ChIJ123' })
|
||||
);
|
||||
expect(result.current).toBe('/uploads/places/dom.jpg');
|
||||
expect(fetchPhoto).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-SHARED-PLACEPHOTO-002: without one, the provider picture is asked by its id through the shared cache', () => {
|
||||
const { result } = renderHook(() =>
|
||||
usePlacePhoto({ name: 'Dom', google_place_id: 'ChIJ123', lat: 50.9, lng: 6.9 })
|
||||
);
|
||||
expect(fetchPhoto).toHaveBeenCalledWith('ChIJ123', 'ChIJ123', 50.9, 6.9, 'Dom', expect.any(Function));
|
||||
expect(result.current).toBe(PROXY);
|
||||
});
|
||||
|
||||
it('FE-SHARED-PLACEPHOTO-003: a place known only by its position gets no picture of its surroundings', () => {
|
||||
const { result } = renderHook(() => usePlacePhoto({ name: 'Somewhere', lat: 50.9, lng: 6.9 }));
|
||||
expect(fetchPhoto).not.toHaveBeenCalled();
|
||||
expect(result.current).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-SHARED-PLACEPHOTO-004: with photos switched off nothing is asked and nothing is shown', () => {
|
||||
seedStore(useAuthStore, { placesPhotosEnabled: false });
|
||||
const { result } = renderHook(() => usePlacePhoto({ name: 'Dom', osm_id: 'way:42' }));
|
||||
expect(fetchPhoto).not.toHaveBeenCalled();
|
||||
expect(result.current).toBeNull();
|
||||
});
|
||||
|
||||
it("FE-SHARED-PLACEPHOTO-005: the next place never shows the previous one's picture while its own loads", () => {
|
||||
vi.mocked(fetchPhoto).mockImplementation((key, _id, _lat, _lng, _name, callback) => {
|
||||
if (key === 'ChIJ123') callback?.({ photoUrl: PROXY, thumbDataUrl: null });
|
||||
});
|
||||
const { result, rerender } = renderHook(({ id }) => usePlacePhoto({ name: 'Stop', google_place_id: id }), {
|
||||
initialProps: { id: 'ChIJ123' },
|
||||
});
|
||||
expect(result.current).toBe(PROXY);
|
||||
|
||||
rerender({ id: 'ChIJ456' });
|
||||
expect(result.current).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { fetchPhoto, getCached } from '../../services/photoService';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { photoCacheKey } from '../Map/placePhoto';
|
||||
|
||||
/** What a place's picture is found by. */
|
||||
interface PhotoPlace {
|
||||
name: string;
|
||||
image_url?: string | null;
|
||||
google_place_id?: string | null;
|
||||
osm_id?: string | null;
|
||||
lat?: number | null;
|
||||
lng?: number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A place's picture at full size, for a view that shows it large (#2629).
|
||||
*
|
||||
* The place's own picture wins, as everywhere else. Without one, the picture its
|
||||
* provider has for its Google or OSM id is asked through the shared photo cache,
|
||||
* so a place the list or the map already shows costs no second request. A place
|
||||
* known only by its position gets none: the proxy would answer with whatever is
|
||||
* near those coordinates, the same street for every stop along it. Null while it
|
||||
* loads, when there is none and while photos are switched off.
|
||||
*/
|
||||
export function usePlacePhoto(place: PhotoPlace | null): string | null {
|
||||
const enabled = useAuthStore((s) => s.placesPhotosEnabled);
|
||||
const own = place?.image_url || null;
|
||||
const providerId = place?.google_place_id || place?.osm_id || null;
|
||||
const key = place && !own && providerId ? photoCacheKey(place) : '';
|
||||
const lat = place?.lat ?? undefined;
|
||||
const lng = place?.lng ?? undefined;
|
||||
const name = place?.name;
|
||||
// Names the place it belongs to, so the previous place's picture never shows for the next.
|
||||
const [loaded, setLoaded] = useState<{ key: string; url: string | null } | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !key || !providerId) return;
|
||||
let cancelled = false;
|
||||
fetchPhoto(key, providerId, lat, lng, name, (entry) => {
|
||||
if (!cancelled) setLoaded({ key, url: entry.photoUrl });
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [enabled, key, providerId, lat, lng, name]);
|
||||
|
||||
if (own) return own;
|
||||
if (!enabled || !key) return null;
|
||||
return loaded?.key === key ? loaded.url : (getCached(key)?.photoUrl ?? null);
|
||||
}
|
||||
Reference in New Issue
Block a user