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:
@@ -0,0 +1,52 @@
|
||||
// Pick a picture from the media library (plan step 5.2), or upload one here: an upload made from
|
||||
// the picker is picked at once. Used by MediaField (products, categories; more places later).
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { Upload } from 'lucide-react'
|
||||
import { errorMessage } from '@xenia/api'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import { Button, Dialog, EmptyState, Input, Spinner } from '@xenia/ui'
|
||||
import { listMedia } from './mediaApi'
|
||||
import { ACCEPT } from './mediaLogic'
|
||||
import { MediaGrid, UploadQueue } from './MediaParts'
|
||||
import { useUploadQueue } from './mediaHooks'
|
||||
|
||||
export default function MediaPicker({ open, onClose, onPick, kind = 'image', selectedId }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const [q, setQ] = useState('')
|
||||
const [search, setSearch] = useState('')
|
||||
const [page, setPage] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
const input = useRef(null)
|
||||
|
||||
useEffect(() => { const h = setTimeout(() => setSearch(q.trim()), 250); return () => clearTimeout(h) }, [q])
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
let live = true
|
||||
listMedia({ kind, q: search }).then(p => { if (live) { setPage(p); setError(null) } }).catch(err => live && setError(errorMessage(err, t)))
|
||||
return () => { live = false }
|
||||
}, [open, kind, search, t])
|
||||
|
||||
const { queue, add, clear, summary } = useUploadQueue(useCallback(item => onPick(item, { uploaded: !item.existing }), [onPick]))
|
||||
|
||||
return (
|
||||
<Dialog open={open} size="lg" title={t('media.pickTitle')} onClose={onClose}
|
||||
footer={<Button variant="secondary" onClick={onClose}>{t('fees.cancel')}</Button>}>
|
||||
<div className="grid gap-3" data-testid="media-picker">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Input value={q} onChange={e => setQ(e.target.value)} placeholder={t('media.search')} aria-label={t('media.search')}
|
||||
className="flex-1 min-w-[10rem]" data-testid="media-picker-search" />
|
||||
<Button variant="secondary" icon={<Upload size={16} />} onClick={() => input.current?.click()} disabled={summary.busy}>{t('media.uploadNew')}</Button>
|
||||
<input ref={input} type="file" accept={kind === 'image' ? ACCEPT.split(',').filter(a => a.startsWith('image/')).join(',') : ACCEPT}
|
||||
className="sr-only" data-testid="media-picker-file" onChange={e => { if (e.target.files?.length) add([e.target.files[0]]); e.target.value = '' }} />
|
||||
</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 ? 'media.noMatch' : 'media.empty')} />
|
||||
: <div className="max-h-[55vh] overflow-auto pr-1">
|
||||
<MediaGrid items={page.items} selectedId={selectedId} onOpen={item => onPick(item, { uploaded: false })} testid="media-picker-grid" />
|
||||
</div>}
|
||||
</div>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user