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,120 @@
|
||||
// One library item in full (plan step 5.2): the picture or video, its display name (editable),
|
||||
// everything known about it (type, resolution, length, sizes, who uploaded it and when, the
|
||||
// original file's name), the sizes the server made, what uses it, and delete (refused while used).
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { Trash2 } from 'lucide-react'
|
||||
import { errorMessage } from '@xenia/api'
|
||||
import { useFormat, useTranslation } from '@xenia/i18n'
|
||||
import { Button, Dialog, Field, Input, Spinner } from '@xenia/ui'
|
||||
import { deleteMedia, getMedia, renameMedia } from './mediaApi'
|
||||
import { duration, fileSize, resolution, VARIANT_ORDER } from './mediaLogic'
|
||||
|
||||
export default function MediaDetails({ id, onClose, onChanged, onDeleted }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const { formatDateTime, formatNumber } = useFormat()
|
||||
const [item, setItem] = useState(null)
|
||||
const [name, setName] = useState('')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [confirm, setConfirm] = useState(false)
|
||||
|
||||
const close = useRef(onClose)
|
||||
useEffect(() => { close.current = onClose })
|
||||
useEffect(() => {
|
||||
let live = true
|
||||
getMedia(id).then(m => { if (live) { setItem(m); setName(m.name) } })
|
||||
.catch(err => { toast.error(errorMessage(err, t)); close.current() })
|
||||
return () => { live = false }
|
||||
}, [id, t])
|
||||
|
||||
const save = async () => {
|
||||
setBusy(true)
|
||||
try { const m = await renameMedia(id, name.trim()); setItem(it => ({ ...it, ...m })); onChanged?.(m); toast.success(t('media.renamed')) }
|
||||
catch (err) { toast.error(errorMessage(err, t)) }
|
||||
setBusy(false)
|
||||
}
|
||||
const remove = async () => {
|
||||
setBusy(true)
|
||||
try { await deleteMedia(id); toast.success(t('media.deleted')); onDeleted?.(id); onClose() }
|
||||
catch (err) { toast.error(errorMessage(err, t)); setConfirm(false) }
|
||||
setBusy(false)
|
||||
}
|
||||
|
||||
const used = item?.used_by || []
|
||||
const rows = item ? [
|
||||
[t('media.kind'), t(`media.kinds.${item.kind}`) + ` (${item.content_type.split('/')[1].toUpperCase()})`],
|
||||
[t('media.resolution'), resolution(item.width, item.height)],
|
||||
[t('media.length'), item.kind === 'video' ? duration(item.duration_ms) : ''],
|
||||
[t('media.fileSize'), fileSize(item.bytes, formatNumber)],
|
||||
[t('media.originalName'), item.original_name],
|
||||
[t('media.uploadedAt'), formatDateTime(item.created_at)],
|
||||
[t('media.uploadedBy'), item.uploaded_by_name],
|
||||
].filter(([, v]) => v) : []
|
||||
|
||||
return (
|
||||
<Dialog open size="lg" title={item?.name || t('media.title')} onClose={onClose}
|
||||
footer={item && <>
|
||||
{confirm ? <>
|
||||
<span className="text-label text-ink-2 mr-auto">{t('media.deleteConfirm')}</span>
|
||||
<Button variant="secondary" onClick={() => setConfirm(false)}>{t('fees.cancel')}</Button>
|
||||
<Button variant="danger" loading={busy} onClick={remove} data-testid="media-delete-confirm">{t('media.delete')}</Button>
|
||||
</> : <>
|
||||
<Button variant="ghost" icon={<Trash2 size={16} />} disabled={busy || used.length > 0} onClick={() => setConfirm(true)}
|
||||
className="mr-auto" data-testid="media-delete">{t('media.delete')}</Button>
|
||||
<Button variant="secondary" onClick={onClose}>{t('media.close')}</Button>
|
||||
</>}
|
||||
</>}>
|
||||
{!item ? <div className="flex justify-center p-6"><Spinner /></div> : (
|
||||
<div className="grid gap-4" data-testid="media-details" data-id={item.id}>
|
||||
<div className="rounded-card bg-surface-3 overflow-hidden flex items-center justify-center" style={{ maxHeight: '45vh' }}>
|
||||
{item.kind === 'video'
|
||||
? <video src={item.urls.original} controls preload="metadata" className="max-h-[45vh] max-w-full" />
|
||||
: <img src={item.urls.full} alt={item.name} className="max-h-[45vh] max-w-full object-contain" />}
|
||||
</div>
|
||||
|
||||
<div className="grid gap-1">
|
||||
<div className="flex gap-2 items-end">
|
||||
<Field label={t('media.displayName')} className="flex-1">
|
||||
<Input value={name} maxLength={120} onChange={e => setName(e.target.value)} data-testid="media-name" />
|
||||
</Field>
|
||||
<Button variant="secondary" loading={busy} disabled={!name.trim() || name.trim() === item.name} onClick={save}
|
||||
data-testid="media-rename">{t('media.saveName')}</Button>
|
||||
</div>
|
||||
<p className="text-caption text-ink-3">{t('media.displayNameHint')}</p>
|
||||
</div>
|
||||
|
||||
<dl className="grid gap-x-4 gap-y-1 text-label" style={{ gridTemplateColumns: 'max-content 1fr' }}>
|
||||
{rows.map(([k, v]) => <div key={k} className="contents"><dt className="text-ink-3">{k}</dt><dd className="text-ink break-all">{v}</dd></div>)}
|
||||
</dl>
|
||||
|
||||
<section className="grid gap-1">
|
||||
<h3 className="text-label font-semibold text-ink">{t('media.sizes')}</h3>
|
||||
<p className="text-caption text-ink-3">{t(item.kind === 'video' ? 'media.sizesVideo' : 'media.sizesHint')}</p>
|
||||
<ul className="grid gap-1" data-testid="media-sizes">
|
||||
{VARIANT_ORDER.filter(v => item.files?.[v]).map(v => (
|
||||
<li key={v} className="flex gap-3 text-label">
|
||||
<span className="text-ink w-28">{t(`media.variants.${v}`)}</span>
|
||||
<span className="text-ink-2 w-28">{resolution(item.files[v].width, item.files[v].height)}</span>
|
||||
<span className="text-ink-2">{fileSize(item.files[v].bytes, formatNumber)}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section className="grid gap-1">
|
||||
<h3 className="text-label font-semibold text-ink">{t('media.usedBy')}</h3>
|
||||
{used.length === 0
|
||||
? <p className="text-label text-ink-3">{t('media.unused')}</p>
|
||||
: <>
|
||||
<ul className="flex flex-wrap gap-1.5" data-testid="media-used-by">
|
||||
{used.map(u => <li key={`${u.kind}:${u.id}`} className="px-2 py-0.5 rounded-control bg-surface-2 text-label text-ink">
|
||||
{t(`media.uses.${u.kind}`, { defaultValue: u.kind })}: {u.name}</li>)}
|
||||
</ul>
|
||||
<p className="text-caption text-ink-3">{t('media.usedCantDelete')}</p>
|
||||
</>}
|
||||
</section>
|
||||
</div>
|
||||
)}
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user