mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(places): the picture of a place is chosen in the editor, and the view opens its photos
- The editor's 'Pick a picture' is the one place a place's picture is set, on the desktop dialog and the phone sheet alike: upload one from the device, keep the current one (now shown as chosen), take an image attached to the place, or one of the sources' photos, or remove it. Nothing is sent before the save: the upload and the attached file are put in place once the place is saved, so Cancel leaves the picture as it was and a new place can get one too. Without the details column the same picker sits in the form. - The view (inspector and phone sheet) shows the picture plainly; a click opens the gallery of its own picture and its sources' photos. - The gallery drops an answer that arrives after the place changed, and its credits link to their source pages. The lightbox is a labelled modal dialog that takes and gives back focus, names its arrows and follows the reading direction. - The picker leaves the details column (PlaceImagePicker), the column reads the shared enrichment cache, and PlaceAvatarUpload and usePlaceImagePick, which only the view used, are gone.
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
import { render, screen, fireEvent, waitFor } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { getCached, isLoading, fetchPhoto, onThumbReady } from '../../services/photoService';
|
||||
|
||||
// Mock photoService — PlaceAvatarUpload wraps PlaceAvatar, which pulls thumbnails.
|
||||
vi.mock('../../services/photoService', () => ({
|
||||
getCached: vi.fn(() => null),
|
||||
isLoading: vi.fn(() => false),
|
||||
fetchPhoto: vi.fn(),
|
||||
onThumbReady: vi.fn(() => () => {}),
|
||||
}));
|
||||
|
||||
// IntersectionObserver stub (PlaceAvatar observes visibility to lazy-load photos)
|
||||
class MockIntersectionObserver {
|
||||
observe = vi.fn();
|
||||
disconnect = vi.fn();
|
||||
unobserve = vi.fn();
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
(globalThis as any).IntersectionObserver = MockIntersectionObserver;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(getCached).mockReturnValue(null);
|
||||
vi.mocked(isLoading).mockReturnValue(false);
|
||||
vi.mocked(fetchPhoto).mockReset();
|
||||
vi.mocked(onThumbReady).mockReturnValue(() => {});
|
||||
});
|
||||
|
||||
import PlaceAvatarUpload from './PlaceAvatarUpload';
|
||||
|
||||
const placeNoImage = {
|
||||
id: 1,
|
||||
name: 'Eiffel Tower',
|
||||
image_url: null,
|
||||
google_place_id: null,
|
||||
osm_id: null,
|
||||
lat: 48.8584,
|
||||
lng: 2.2945,
|
||||
};
|
||||
|
||||
const placeWithImage = { ...placeNoImage, image_url: 'https://example.com/eiffel.jpg' };
|
||||
|
||||
describe('PlaceAvatarUpload', () => {
|
||||
it('FE-COMP-PLACEAVUPLOAD-001: renders the underlying avatar image when image_url is set', () => {
|
||||
render(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
const img = screen.getByRole('img') as HTMLImageElement;
|
||||
expect(img.src).toContain('eiffel.jpg');
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-002: clicking the avatar opens the hidden file picker', async () => {
|
||||
const user = userEvent.setup();
|
||||
const { container } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
const input = container.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const clickSpy = vi.spyOn(input, 'click');
|
||||
await user.click(screen.getByRole('button', { name: 'Upload image' }));
|
||||
expect(clickSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-003: choosing a file calls onUpload with the picked file', async () => {
|
||||
const onUpload = vi.fn().mockResolvedValue(undefined);
|
||||
const { container } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={onUpload} onRemove={vi.fn()} />);
|
||||
const input = container.querySelector('input[type="file"]') as HTMLInputElement;
|
||||
const file = new File(['x'], 'photo.jpg', { type: 'image/jpeg' });
|
||||
fireEvent.change(input, { target: { files: [file] } });
|
||||
await waitFor(() => expect(onUpload).toHaveBeenCalledWith(file));
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-004: the remove button only appears when a custom image is set', () => {
|
||||
const { rerender } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.queryByRole('button', { name: 'Remove image' })).toBeNull();
|
||||
rerender(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.getByRole('button', { name: 'Remove image' })).toBeTruthy();
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-005: clicking remove calls onRemove', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onRemove = vi.fn().mockResolvedValue(undefined);
|
||||
render(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={onRemove} />);
|
||||
await user.click(screen.getByRole('button', { name: 'Remove image' }));
|
||||
expect(onRemove).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-PLACEAVUPLOAD-006: the avatar aria-label reflects upload vs change from the places.* keys', () => {
|
||||
const { rerender } = render(<PlaceAvatarUpload place={placeNoImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.getByRole('button', { name: 'Upload image' })).toBeTruthy();
|
||||
rerender(<PlaceAvatarUpload place={placeWithImage} onUpload={vi.fn()} onRemove={vi.fn()} />);
|
||||
expect(screen.getByRole('button', { name: 'Change image' })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -1,204 +0,0 @@
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { Camera, Loader2, Upload, X } from 'lucide-react'
|
||||
import PlaceAvatar from './PlaceAvatar'
|
||||
import { Tooltip } from './Tooltip'
|
||||
import { useToast } from './Toast'
|
||||
import { useTranslation, translateApiError } from '../../i18n'
|
||||
import { fetchImageAsBlob } from '../../api/authUrl'
|
||||
import { usePlaceImagePick } from '../Planner/usePlaceActions'
|
||||
import type { Place, TripFile } from '../../types'
|
||||
|
||||
interface Category {
|
||||
color?: string
|
||||
icon?: string
|
||||
}
|
||||
|
||||
interface PlaceAvatarUploadProps {
|
||||
place: Pick<Place, 'id' | 'name' | 'image_url' | 'google_place_id' | 'osm_id' | 'lat' | 'lng'>
|
||||
category?: Category | null
|
||||
size?: number
|
||||
onUpload: (file: File) => Promise<void>
|
||||
/** Clears the custom image; the auto-fetched default thumbnail then returns (#1136). */
|
||||
onRemove: () => Promise<void> | void
|
||||
/** Pictures already attached to the place, offered as its image beside an upload (#1242). */
|
||||
attachedImages?: TripFile[]
|
||||
onPickAttached?: (fileId: number) => Promise<void>
|
||||
}
|
||||
|
||||
const PICKABLE = /^image\/(jpeg|png|gif|webp)$/
|
||||
|
||||
/** The images among a place's files that can stand as its picture. */
|
||||
export function pickableImages(files: TripFile[]): TripFile[] {
|
||||
return files.filter(f => PICKABLE.test(f.mime_type || ''))
|
||||
}
|
||||
|
||||
/**
|
||||
* A PlaceAvatar the user can click to set a custom thumbnail (#1136): hover reveals
|
||||
* a camera overlay with a tooltip, clicking opens the file picker, and when a custom
|
||||
* upload is present a small corner button removes it (falling back to the default).
|
||||
*/
|
||||
export default function PlaceAvatarUpload({ place, category, size = 52, onUpload, onRemove, attachedImages = [], onPickAttached }: PlaceAvatarUploadProps) {
|
||||
const { t } = useTranslation()
|
||||
const toast = useToast()
|
||||
const fileRef = useRef<HTMLInputElement>(null)
|
||||
const anchorRef = useRef<HTMLDivElement>(null)
|
||||
const { busy, setBusy, pickImage: handleFile } = usePlaceImagePick(onUpload, toast)
|
||||
const [chooserOpen, setChooserOpen] = useState(false)
|
||||
const hasCustom = Boolean(place.image_url)
|
||||
const offersAttached = !!onPickAttached && attachedImages.length > 0
|
||||
// With pictures on the place the click asks where from; without, it goes straight to the device.
|
||||
const openPicker = () => { if (busy) return; if (offersAttached) setChooserOpen(o => !o); else fileRef.current?.click() }
|
||||
|
||||
const pickAttached = async (fileId: number) => {
|
||||
setChooserOpen(false)
|
||||
setBusy(true)
|
||||
try {
|
||||
await onPickAttached!(fileId)
|
||||
} catch (err: unknown) {
|
||||
toast.error(translateApiError(t, err, 'places.imageUploadError'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleRemove = async (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
setBusy(true)
|
||||
try {
|
||||
await onRemove()
|
||||
} catch (err: unknown) {
|
||||
toast.error(translateApiError(t, err, 'places.imageUploadError'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ position: 'relative', width: size, height: size }}>
|
||||
<Tooltip label={hasCustom ? t('places.changeImage') : t('places.uploadImage')} placement="top">
|
||||
<div
|
||||
className="group"
|
||||
style={{ position: 'relative', width: size, height: size, borderRadius: '50%', cursor: busy ? 'default' : 'pointer' }}
|
||||
ref={anchorRef}
|
||||
onClick={openPicker}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-haspopup={offersAttached ? 'dialog' : undefined}
|
||||
aria-expanded={offersAttached ? chooserOpen : undefined}
|
||||
onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); openPicker() } }}
|
||||
aria-label={hasCustom ? t('places.changeImage') : t('places.uploadImage')}
|
||||
>
|
||||
<PlaceAvatar place={place} category={category} size={size} />
|
||||
|
||||
{/* Hover overlay — a camera cue that this thumbnail is editable. */}
|
||||
<div
|
||||
className="opacity-0 group-hover:opacity-100"
|
||||
style={{
|
||||
position: 'absolute', inset: 0, borderRadius: '50%',
|
||||
background: 'rgba(0,0,0,0.45)', display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
transition: 'opacity 0.15s', pointerEvents: 'none',
|
||||
}}
|
||||
>
|
||||
{busy ? <Loader2 size={Math.round(size * 0.34)} className="animate-spin" color="#fff" /> : <Camera size={Math.round(size * 0.34)} color="#fff" />}
|
||||
</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
|
||||
{/* Remove button — only when a custom upload is set. */}
|
||||
{hasCustom && !busy && (
|
||||
<Tooltip label={t('places.removeImage')} placement="top">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleRemove}
|
||||
aria-label={t('places.removeImage')}
|
||||
style={{
|
||||
position: 'absolute', top: -3, insetInlineEnd: -3, width: 18, height: 18, borderRadius: '50%',
|
||||
background: '#ef4444', color: '#fff', border: '2px solid var(--bg-elevated, #fff)',
|
||||
display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', padding: 0, zIndex: 1,
|
||||
}}
|
||||
>
|
||||
<X size={10} strokeWidth={3} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
)}
|
||||
|
||||
<input ref={fileRef} type="file" accept="image/jpeg,image/png,image/gif,image/webp,.heic,.heif" style={{ display: 'none' }} onChange={handleFile} />
|
||||
{chooserOpen && (
|
||||
<ImageChooser
|
||||
anchor={anchorRef.current}
|
||||
images={attachedImages}
|
||||
onUpload={() => { setChooserOpen(false); fileRef.current?.click() }}
|
||||
onPick={id => { void pickAttached(id) }}
|
||||
onClose={() => setChooserOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** Upload from the device, or one of the pictures already on the place (#1242). */
|
||||
function ImageChooser({ anchor, images, onUpload, onPick, onClose }: {
|
||||
anchor: HTMLElement | null
|
||||
images: TripFile[]
|
||||
onUpload: () => void
|
||||
onPick: (fileId: number) => void
|
||||
onClose: () => void
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const [pos, setPos] = useState<{ top: number; left: number } | null>(null)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!anchor) return
|
||||
const r = anchor.getBoundingClientRect()
|
||||
const width = 264
|
||||
setPos({ top: r.bottom + 8, left: Math.min(Math.max(12, r.left), window.innerWidth - width - 12) })
|
||||
}, [anchor])
|
||||
|
||||
useEffect(() => {
|
||||
const onDown = (e: MouseEvent) => {
|
||||
const target = e.target as Node
|
||||
if (!ref.current?.contains(target) && !anchor?.contains(target)) onClose()
|
||||
}
|
||||
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() }
|
||||
document.addEventListener('mousedown', onDown)
|
||||
document.addEventListener('keydown', onKey)
|
||||
return () => { document.removeEventListener('mousedown', onDown); document.removeEventListener('keydown', onKey) }
|
||||
}, [anchor, onClose])
|
||||
|
||||
if (!pos) return null
|
||||
return createPortal(
|
||||
<div ref={ref} role="dialog" aria-label={t('places.chooseImage')}
|
||||
className="trek-popover-enter fixed z-[var(--z-toast)] flex w-[264px] flex-col gap-2 rounded-[14px] border border-edge-secondary bg-surface-card p-2 shadow-popover"
|
||||
style={{ top: pos.top, left: pos.left }}>
|
||||
<button type="button" onClick={onUpload}
|
||||
className="flex items-center gap-2 rounded-[10px] px-2.5 py-2 text-start text-body font-semibold text-content hover:bg-surface-hover">
|
||||
<Upload size={14} strokeWidth={2.2} className="text-content-muted" />
|
||||
{t('places.uploadFromDevice')}
|
||||
</button>
|
||||
<div className="px-2.5 text-caption font-semibold uppercase tracking-[0.04em] text-content-faint">{t('places.fromAttachedFiles')}</div>
|
||||
<div className="grid grid-cols-4 gap-1.5 px-1 pb-1">
|
||||
{images.map(f => <AttachedThumb key={f.id} file={f} onPick={() => onPick(f.id)} />)}
|
||||
</div>
|
||||
</div>,
|
||||
document.body,
|
||||
)
|
||||
}
|
||||
|
||||
function AttachedThumb({ file, onPick }: { file: TripFile; onPick: () => void }) {
|
||||
const [src, setSrc] = useState<string | null>(null)
|
||||
useEffect(() => {
|
||||
let current = true
|
||||
fetchImageAsBlob(file.url).then(u => { if (current) setSrc(u) }).catch(() => {})
|
||||
return () => { current = false }
|
||||
}, [file.url])
|
||||
return (
|
||||
<Tooltip label={file.original_name} placement="top">
|
||||
<button type="button" onClick={onPick} aria-label={file.original_name}
|
||||
className="aspect-square overflow-hidden rounded-[9px] bg-surface-secondary ring-1 ring-edge-faint transition-shadow hover:ring-2 hover:ring-accent">
|
||||
{src && <img src={src} alt="" className="h-full w-full object-cover" />}
|
||||
</button>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user