mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
fix(journey): gallery photos can be deleted on touch screens and ask before they go
The delete on a gallery tile only showed on mouse hover, so a phone or tablet never saw it, and the phone screen had no delete at all. Where it was reachable it deleted at once, although the server removes the photo for good and takes it out of every entry that uses it. The desktop tile now shows its delete on hover, on keyboard focus and always where there is no hover, with a larger target there and an aria label; viewers no longer get the button. The phone gallery gets the same corner button for editors. Both ask through ConfirmDialog first, saying that the photo also leaves the entries it is used in, and both delete through the store action. The entry editor's photo controls follow the same hover rule. Refs #2593
This commit is contained in:
@@ -29,7 +29,9 @@ const SOURCE_ON = 'border-content bg-surface-secondary text-content'
|
||||
const PHOTO_PANEL = 'mt-2 rounded-[12px] border border-edge-faint bg-surface-secondary'
|
||||
/** A control laid over a photo tile, raised on the card colour so it reads on any picture. */
|
||||
const ON_PHOTO = 'bg-surface-card text-content shadow-sm'
|
||||
const PHOTO_REMOVE = `absolute end-1 top-1 grid h-5 w-5 place-items-center rounded-full opacity-0 transition-opacity group-hover:opacity-100 ${ON_PHOTO}`
|
||||
/** A tile control shown on hover, on keyboard focus, and always where there is no hover (#2593). */
|
||||
const ON_TILE_HOVER = 'opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100 [@media(hover:none)]:opacity-100'
|
||||
const PHOTO_REMOVE = `absolute end-1 top-1 grid h-5 w-5 place-items-center rounded-full ${ON_TILE_HOVER} ${ON_PHOTO}`
|
||||
/** What hangs under the location field: the search results, or the note that a search is running. */
|
||||
const DROPDOWN = 'absolute inset-x-0 top-full z-[100] mt-1 rounded-[12px] border border-edge-faint bg-surface-card shadow-dropdown'
|
||||
|
||||
@@ -409,7 +411,7 @@ export function EntryEditor({ entry, journeyId, tripDates, galleryPhotos, trips,
|
||||
{idx > 0 && photos.length > 1 && (
|
||||
<button type="button"
|
||||
onClick={e => { e.stopPropagation(); photoOrder.makeFirst(idx) }}
|
||||
className={`absolute bottom-0.5 start-0.5 rounded px-1.5 py-0.5 font-semibold opacity-0 transition-opacity group-hover:opacity-100 ${ON_PHOTO}`}
|
||||
className={`absolute bottom-0.5 start-0.5 rounded px-1.5 py-0.5 font-semibold ${ON_TILE_HOVER} ${ON_PHOTO}`}
|
||||
style={fs(8)}
|
||||
>
|
||||
{t('journey.editor.makeFirst')}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-JRN-GALLERY-001 to FE-JRN-GALLERY-021
|
||||
// FE-JRN-GALLERY-001 to FE-JRN-GALLERY-024
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { act, useRef, useState } from 'react'
|
||||
@@ -79,8 +79,9 @@ const entries: JourneyEntry[] = [
|
||||
/** Mirrors the page wiring: the gallery reports its connected providers up,
|
||||
and the host renders one button per provider next to Upload. The picker's
|
||||
Add goes through the same hook the page takes from useJourneyDetail. */
|
||||
function GalleryHarness({ gallery, onPhotoClick, onRefresh, onRegisterUpload, onUploadProgress }: {
|
||||
function GalleryHarness({ gallery, canEdit = true, onPhotoClick, onRefresh, onRegisterUpload, onUploadProgress }: {
|
||||
gallery: GalleryPhoto[]
|
||||
canEdit?: boolean
|
||||
onPhotoClick: (photos: GalleryPhoto[], index: number) => void
|
||||
onRefresh: () => void
|
||||
onRegisterUpload?: (fn: () => void) => void
|
||||
@@ -100,6 +101,7 @@ function GalleryHarness({ gallery, onPhotoClick, onRefresh, onRegisterUpload, on
|
||||
journeyId={JOURNEY_ID}
|
||||
userId={1}
|
||||
trips={trips}
|
||||
canEdit={canEdit}
|
||||
onPhotoClick={onPhotoClick}
|
||||
onRefresh={onRefresh}
|
||||
onAddProviderPhotos={addPickedPhotos}
|
||||
@@ -111,16 +113,24 @@ function GalleryHarness({ gallery, onPhotoClick, onRefresh, onRegisterUpload, on
|
||||
)
|
||||
}
|
||||
|
||||
function mountGallery(gallery: GalleryPhoto[], onRegisterUpload?: (fn: () => void) => void) {
|
||||
function mountGallery(gallery: GalleryPhoto[], onRegisterUpload?: (fn: () => void) => void, canEdit = true) {
|
||||
const onPhotoClick = vi.fn()
|
||||
const onRefresh = vi.fn()
|
||||
const onUploadProgress = vi.fn()
|
||||
const utils = render(
|
||||
<GalleryHarness gallery={gallery} onPhotoClick={onPhotoClick} onRefresh={onRefresh} onRegisterUpload={onRegisterUpload} onUploadProgress={onUploadProgress} />,
|
||||
<GalleryHarness gallery={gallery} canEdit={canEdit} onPhotoClick={onPhotoClick} onRefresh={onRefresh} onRegisterUpload={onRegisterUpload} onUploadProgress={onUploadProgress} />,
|
||||
)
|
||||
return { ...utils, onPhotoClick, onRefresh, onUploadProgress }
|
||||
}
|
||||
|
||||
/** The tile's delete asks first (#2593): the corner button, then the dialog's own Delete. */
|
||||
async function deleteThroughConfirm(user: ReturnType<typeof userEvent.setup>) {
|
||||
await user.click(screen.getByRole('button', { name: 'Delete' }))
|
||||
expect(screen.getByText('Delete photo')).toBeInTheDocument()
|
||||
const dialogDelete = screen.getAllByRole('button', { name: 'Delete' }).slice(-1)[0]
|
||||
await user.click(dialogDelete)
|
||||
}
|
||||
|
||||
function useConnectedImmich() {
|
||||
server.use(
|
||||
http.get('/api/addons', () => HttpResponse.json({
|
||||
@@ -205,7 +215,7 @@ describe('GalleryView', () => {
|
||||
expect(screen.getByText('Colosseum')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('FE-JRN-GALLERY-007: removes a deleted photo from the store optimistically', async () => {
|
||||
it('FE-JRN-GALLERY-007: a confirmed delete removes the photo from the gallery and its entries', async () => {
|
||||
let deleted = false
|
||||
server.use(http.delete('/api/journeys/9/gallery/100', () => {
|
||||
deleted = true
|
||||
@@ -220,11 +230,12 @@ describe('GalleryView', () => {
|
||||
},
|
||||
})
|
||||
const user = userEvent.setup()
|
||||
const { container, onRefresh } = mountGallery([photo])
|
||||
const { onRefresh } = mountGallery([photo])
|
||||
|
||||
await user.click(container.querySelectorAll('button')[0])
|
||||
await deleteThroughConfirm(user)
|
||||
|
||||
await waitFor(() => expect(deleted).toBe(true))
|
||||
await waitFor(() => expect(useJourneyStore.getState().current!.gallery).toHaveLength(0))
|
||||
const current = useJourneyStore.getState().current!
|
||||
expect(current.gallery).toHaveLength(0)
|
||||
expect(current.entries[0].photos).toHaveLength(0)
|
||||
@@ -241,9 +252,9 @@ describe('GalleryView', () => {
|
||||
},
|
||||
})
|
||||
const user = userEvent.setup()
|
||||
const { container, onRefresh } = mountGallery([photo])
|
||||
const { onRefresh } = mountGallery([photo])
|
||||
|
||||
await user.click(container.querySelectorAll('button')[0])
|
||||
await deleteThroughConfirm(user)
|
||||
|
||||
await waitFor(() => expect(toastSpy).toHaveBeenCalledWith('Error', 'error', undefined))
|
||||
expect(onRefresh).toHaveBeenCalledTimes(1)
|
||||
@@ -256,13 +267,53 @@ describe('GalleryView', () => {
|
||||
return HttpResponse.json({ ok: true })
|
||||
}))
|
||||
const user = userEvent.setup()
|
||||
const { container } = mountGallery([buildGalleryPhoto()])
|
||||
mountGallery([buildGalleryPhoto()])
|
||||
|
||||
await user.click(container.querySelectorAll('button')[0])
|
||||
await deleteThroughConfirm(user)
|
||||
|
||||
expect(deleted).toBe(false)
|
||||
})
|
||||
|
||||
it('FE-JRN-GALLERY-022: cancelling the confirm leaves the photo where it is', async () => {
|
||||
let deleted = false
|
||||
server.use(http.delete('/api/journeys/9/gallery/100', () => {
|
||||
deleted = true
|
||||
return HttpResponse.json({ ok: true })
|
||||
}))
|
||||
const photo = buildGalleryPhoto()
|
||||
useJourneyStore.setState({
|
||||
current: {
|
||||
id: JOURNEY_ID, user_id: 1, title: 'Italy', status: 'active', created_at: 0, updated_at: 0,
|
||||
entries: [], gallery: [photo], trips, contributors: [], stats: { entries: 0, photos: 1, places: 0 },
|
||||
},
|
||||
})
|
||||
const user = userEvent.setup()
|
||||
const { onPhotoClick } = mountGallery([photo])
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Delete' }))
|
||||
expect(screen.getByText(/removed from the gallery and from every entry/)).toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'Cancel' }))
|
||||
|
||||
expect(screen.queryByText('Delete photo')).not.toBeInTheDocument()
|
||||
expect(deleted).toBe(false)
|
||||
expect(useJourneyStore.getState().current!.gallery).toHaveLength(1)
|
||||
expect(onPhotoClick).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-JRN-GALLERY-023: a viewer gets no delete button on the tiles', () => {
|
||||
mountGallery([buildGalleryPhoto()], undefined, false)
|
||||
|
||||
expect(screen.queryByRole('button', { name: 'Delete' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('FE-JRN-GALLERY-024: the delete button does not wait for a hover on touch screens or keyboard focus', () => {
|
||||
mountGallery([buildGalleryPhoto()])
|
||||
|
||||
const button = screen.getByRole('button', { name: 'Delete' })
|
||||
expect(button.className).toContain('[@media(hover:none)]:opacity-100')
|
||||
expect(button.className).toContain('focus-visible:opacity-100')
|
||||
})
|
||||
|
||||
it('FE-JRN-GALLERY-010: only offers providers that are enabled and connected', async () => {
|
||||
useConnectedImmich()
|
||||
mountGallery([])
|
||||
|
||||
@@ -2,7 +2,6 @@ import { useEffect, useState, useRef } from 'react'
|
||||
import { RefreshCw, Camera, X, Play } from 'lucide-react'
|
||||
import { useJourneyStore } from '../../store/journeyStore'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { journeyApi } from '../../api/client'
|
||||
import { useToast } from '../shared/Toast'
|
||||
import type { JourneyEntry, GalleryPhoto, JourneyTrip } from '../../store/journeyStore'
|
||||
import { photoUrl, posterlessVideo } from '../../pages/journeyDetail/JourneyDetailPage.helpers'
|
||||
@@ -11,13 +10,16 @@ import { useConnectedPhotoProviders } from './useConnectedPhotoProviders'
|
||||
import { useGalleryUpload } from './useGalleryUpload'
|
||||
import { ScrollTrigger } from './JourneyDetailPageScrollTrigger'
|
||||
import EmptyState from '../shared/EmptyState'
|
||||
import ConfirmDialog from '../shared/ConfirmDialog'
|
||||
|
||||
export function GalleryView({ entries, gallery, journeyId, userId, trips, onPhotoClick, onRefresh, onAddProviderPhotos, onRegisterUpload, onRegisterProviders, onUploadProgress }: {
|
||||
export function GalleryView({ entries, gallery, journeyId, userId, trips, canEdit, onPhotoClick, onRefresh, onAddProviderPhotos, onRegisterUpload, onRegisterProviders, onUploadProgress }: {
|
||||
entries: JourneyEntry[]
|
||||
gallery: GalleryPhoto[]
|
||||
journeyId: number
|
||||
userId: number
|
||||
trips: JourneyTrip[]
|
||||
/** Owner or editor: only they get the delete on a tile, the server refuses everyone else. */
|
||||
canEdit: boolean
|
||||
onPhotoClick: (photos: GalleryPhoto[], index: number) => void
|
||||
onRefresh: () => void
|
||||
/** What the picker's Add does: the host's useProviderPhotoAdds, shared with the phone screen. */
|
||||
@@ -63,24 +65,15 @@ export function GalleryView({ entries, gallery, journeyId, userId, trips, onPhot
|
||||
const galleryFileRef = useRef<HTMLInputElement>(null)
|
||||
useEffect(() => { onRegisterUpload?.(() => galleryFileRef.current?.click()) }, [onRegisterUpload])
|
||||
|
||||
// Deleting is final on the server and takes the photo out of every entry that
|
||||
// uses it too, so the tile only asks; the dialog does the deleting (#2593).
|
||||
const [deleteTarget, setDeleteTarget] = useState<GalleryPhoto | null>(null)
|
||||
|
||||
const handleDeletePhoto = async (galleryPhotoId: number) => {
|
||||
const store = useJourneyStore.getState()
|
||||
if (!store.current) return
|
||||
|
||||
// Optimistic update — remove from gallery and all entry photo lists
|
||||
useJourneyStore.setState({
|
||||
current: {
|
||||
...store.current,
|
||||
gallery: (store.current.gallery || []).filter(p => p.id !== galleryPhotoId),
|
||||
entries: store.current.entries.map(e => ({
|
||||
...e,
|
||||
photos: e.photos.filter(p => p.id !== galleryPhotoId),
|
||||
})),
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await journeyApi.deleteGalleryPhoto(journeyId, galleryPhotoId)
|
||||
await store.deleteGalleryPhoto(journeyId, galleryPhotoId)
|
||||
} catch {
|
||||
toast.error(t('common.error'))
|
||||
onRefresh()
|
||||
@@ -134,13 +127,17 @@ export function GalleryView({ entries, gallery, journeyId, userId, trips, onPhot
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{/* Delete button */}
|
||||
<button type="button"
|
||||
onClick={(e) => { e.stopPropagation(); void handleDeletePhoto(photo.id) }}
|
||||
className="absolute top-1.5 end-1.5 w-6 h-6 rounded-full bg-black/60 backdrop-blur text-white flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity z-10"
|
||||
>
|
||||
<X size={12} />
|
||||
</button>
|
||||
{/* Delete: revealed on hover, always there where there is no hover (#2593) */}
|
||||
{canEdit && (
|
||||
<button type="button"
|
||||
onClick={(e) => { e.stopPropagation(); setDeleteTarget(photo) }}
|
||||
onKeyDown={e => e.stopPropagation()}
|
||||
aria-label={t('common.delete')}
|
||||
className="absolute top-1.5 end-1.5 w-6 h-6 rounded-full bg-black/60 backdrop-blur text-white flex items-center justify-center opacity-0 group-hover:opacity-100 focus-visible:opacity-100 [@media(hover:none)]:w-8 [@media(hover:none)]:h-8 [@media(hover:none)]:opacity-100 transition-opacity z-10"
|
||||
>
|
||||
<X size={12} />
|
||||
</button>
|
||||
)}
|
||||
{photo.provider && photo.provider !== 'local' && (
|
||||
<div className="absolute top-1.5 start-1.5">
|
||||
<span className="text-[8px] font-medium px-1.5 py-0.5 rounded-full bg-black/70 backdrop-blur text-white flex items-center gap-1">
|
||||
@@ -150,7 +147,7 @@ export function GalleryView({ entries, gallery, journeyId, userId, trips, onPhot
|
||||
</div>
|
||||
)}
|
||||
{photo.caption && (
|
||||
<div className="absolute bottom-0 inset-x-0 p-2 bg-gradient-to-t from-black/60 to-transparent opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<div className="absolute bottom-0 inset-x-0 p-2 bg-gradient-to-t from-black/60 to-transparent opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100 transition-opacity">
|
||||
<p className="text-[10px] text-white truncate">{photo.caption}</p>
|
||||
</div>
|
||||
)}
|
||||
@@ -162,6 +159,16 @@ export function GalleryView({ entries, gallery, journeyId, userId, trips, onPhot
|
||||
<ScrollTrigger onVisible={() => setVisibleCount(c => c + GALLERY_PAGE)} loading={false} />
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
isOpen={!!deleteTarget}
|
||||
onClose={() => setDeleteTarget(null)}
|
||||
onConfirm={() => { if (deleteTarget) void handleDeletePhoto(deleteTarget.id) }}
|
||||
title={t('journey.gallery.deleteTitle')}
|
||||
message={t('journey.gallery.deleteMessage')}
|
||||
confirmLabel={t('common.delete')}
|
||||
danger
|
||||
/>
|
||||
|
||||
{/* Provider Photo Picker Modal */}
|
||||
{showPicker && (
|
||||
<ProviderPicker
|
||||
|
||||
Reference in New Issue
Block a user