feat(manager): Media page, photo field for products and categories (platform-v2 step 5.2)

- Management → Media (src/pages/media/): every picture and video, search,
  pictures / videos, batch upload (many files or drop on the page) with a
  result per file, details (preview, display name, kind, resolution, length,
  file size, uploaded file, when / who, the sizes made, what uses it, delete
  off while used).
- MediaField: upload on the spot (into the library and set at once), pick
  from the library (MediaPicker, which can also upload), remove; after an
  upload on the spot "Name it after the product / category" renames it in
  Latin letters. Used by the product editor and the category dialog.
- nav: `anyPerm` (any of the keys that manage pictures); the route guard uses
  the same rule. Product rows have test ids.
- @xenia/i18n: latin() (Greek to Latin, the way venues write it) + tests.
- tests: manager media.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 15:57:50 +02:00
co-authored by Claude Opus 5.5
parent 5ad4c4ef55
commit e09056a7a4
20 changed files with 810 additions and 57 deletions
@@ -0,0 +1,90 @@
// The Media page (plan step 5.2, owner 2026-11-17): every picture and video of the venue in one
// library. Batch upload (pick many files or drop them on the page) to use later anywhere that
// takes media (products, categories; the customer display and signage next). The backend names
// and stores the files; here people see a display name, the details and the sizes made.
// Backend: /api/media (routers/media.py).
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Upload } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, EmptyState, Input, Select, Spinner, cx } from '@xenia/ui'
import { listMedia } from './mediaApi'
import { ACCEPT } from './mediaLogic'
import MediaDetails from './MediaDetails'
import { MediaGrid, UploadQueue } from './MediaParts'
import { useUploadQueue } from './mediaHooks'
export default function MediaPage() {
const { t } = useTranslation('manager')
const [kind, setKind] = useState('')
const [q, setQ] = useState('')
const [search, setSearch] = useState('')
const [page, setPage] = useState(null)
const [error, setError] = useState(null)
const [open, setOpen] = useState(null)
const [dragging, setDragging] = useState(false)
const input = useRef(null)
useEffect(() => { const h = setTimeout(() => setSearch(q.trim()), 250); return () => clearTimeout(h) }, [q])
const load = useCallback(async () => {
try { setPage(await listMedia({ kind, q: search })); setError(null) } catch (err) { setError(errorMessage(err, t)) }
}, [kind, search, t])
useEffect(() => { load() }, [load])
// Each finished upload shows at once; the list is read again when the batch is done.
const { queue, add, clear, summary } = useUploadQueue(useCallback(item => {
setPage(p => (p && !p.items.some(x => x.id === item.id) ? { ...p, items: [item, ...p.items], total: p.total + 1 } : p))
}, []))
const wasBusy = useRef(false)
useEffect(() => { if (wasBusy.current && !summary.busy) load(); wasBusy.current = summary.busy }, [summary.busy, load])
const onDrop = e => {
e.preventDefault(); setDragging(false)
if (e.dataTransfer.files?.length) add(e.dataTransfer.files)
}
const kinds = useMemo(() => [
{ value: '', label: t('media.kinds.all') }, { value: 'image', label: t('media.kinds.images') }, { value: 'video', label: t('media.kinds.videos') },
], [t])
const closeDetails = useCallback(() => setOpen(null), [])
return (
<div className={cx('p-4 md:p-6 grid gap-5 max-w-7xl min-h-full content-start', dragging && 'outline-2 outline-dashed outline-accent -outline-offset-8')}
data-testid="media-page"
onDragOver={e => { if ([...(e.dataTransfer?.types || [])].includes('Files')) { e.preventDefault(); setDragging(true) } }}
onDragLeave={e => { if (e.currentTarget === e.target) setDragging(false) }}
onDrop={onDrop}>
<header className="grid gap-1">
<h1 className="text-heading font-semibold text-ink">{t('media.title')}</h1>
<p className="text-body text-ink-2 max-w-3xl">{t('media.intro')}</p>
</header>
<div className="flex flex-wrap gap-2 items-center">
<Button icon={<Upload size={16} />} onClick={() => input.current?.click()} data-testid="media-upload">{t('media.upload')}</Button>
<input ref={input} type="file" multiple accept={ACCEPT} className="sr-only" data-testid="media-file-input"
onChange={e => { if (e.target.files?.length) add(e.target.files); e.target.value = '' }} />
<span className="text-label text-ink-3 hidden md:inline">{t('media.dropHint')}</span>
<div className="flex gap-2 ml-auto w-full sm:w-auto">
<Input value={q} onChange={e => setQ(e.target.value)} placeholder={t('media.search')} aria-label={t('media.search')}
className="flex-1 sm:w-56" data-testid="media-search" />
<Select value={kind} onChange={e => setKind(e.target.value)} options={kinds} aria-label={t('media.kind')} data-testid="media-kind" />
</div>
</div>
<UploadQueue queue={queue} summary={summary} onClear={clear} />
{error ? <EmptyState title={error} />
: !page ? <div className="flex justify-center p-6"><Spinner /></div>
: page.items.length === 0 ? <EmptyState title={t(search || kind ? 'media.noMatch' : 'media.empty')} description={search || kind ? undefined : t('media.emptyHint')} />
: <>
<p className="text-label text-ink-3" data-testid="media-count">{t('media.count', { count: page.total })}</p>
<MediaGrid items={page.items} onOpen={item => setOpen(item.id)} />
{page.total > page.items.length && <p className="text-label text-ink-3">{t('media.more', { shown: page.items.length, total: page.total })}</p>}
</>}
{open && <MediaDetails id={open} onClose={closeDetails}
onChanged={m => setPage(p => p && { ...p, items: p.items.map(x => (x.id === m.id ? { ...x, ...m } : x)) })}
onDeleted={id => setPage(p => p && { ...p, items: p.items.filter(x => x.id !== id), total: p.total - 1 })} />}
</div>
)
}