Folders are only a grouping in the database (media_folders, media.folder_id, Alembic 0046): made, renamed, deleted (files stay) on the Media page, a file filed from its details, uploads go into the folder shown, the picker filters by folder. The gallery widget: its own pictures/videos in order or a folder as it is now, seconds each, fade / slide / zoom, cover / contain. Plan 6R.16, F-114. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
7.8 KiB
React
142 lines
7.8 KiB
React
// 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, Dropdown, Field, Input, Spinner, cx } from '@xenia/ui'
|
|
import { deleteMedia, getMedia, renameMedia, setFolder } from './mediaApi'
|
|
import { duration, fileSize, preparing, resolution, VARIANT_ORDER, videoState } from './mediaLogic'
|
|
|
|
export default function MediaDetails({ id, onClose, onChanged, onDeleted, folders = [] }) {
|
|
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])
|
|
// A video being prepared for screens (5.4e): follow it until it is done.
|
|
const changed = useRef(onChanged)
|
|
useEffect(() => { changed.current = onChanged })
|
|
const working = preparing([item])
|
|
useEffect(() => {
|
|
if (!working) return
|
|
const h = setInterval(() => { getMedia(id).then(m => { setItem(it => ({ ...it, ...m })); changed.current?.(m) }).catch(() => {}) }, 2000)
|
|
return () => clearInterval(h)
|
|
}, [working, id])
|
|
const state = videoState(item)
|
|
|
|
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 fileIn = async folderId => {
|
|
try { const m = await setFolder(id, folderId); setItem(it => ({ ...it, ...m })); onChanged?.(m); toast.success(t('media.folders.moved')) }
|
|
catch (err) { toast.error(errorMessage(err, t)) }
|
|
}
|
|
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 || item.builtin} 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.play || item.urls.original} poster={item.urls.full} 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} disabled={item.builtin} onChange={e => setName(e.target.value)} data-testid="media-name" />
|
|
</Field>
|
|
<Button variant="secondary" loading={busy} disabled={item.builtin || !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(item.builtin ? 'media.builtinHint' : 'media.displayNameHint')}</p>
|
|
</div>
|
|
|
|
<Field label={t('media.folders.folder')} className="max-w-sm">
|
|
<Dropdown value={item.folder_id ?? ''} onChange={v => fileIn(v === '' ? null : Number(v))} data-testid="media-folder-pick"
|
|
options={[{ value: '', label: t('media.folders.none') }, ...folders.map(f => ({ value: f.id, label: f.name }))]} />
|
|
</Field>
|
|
|
|
<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>
|
|
{state && <p className={cx('text-label font-medium', state === 'failed' ? 'text-danger-ink' : state === 'ready' || state === 'asIs' ? 'text-success-ink' : 'text-accent-ink')}
|
|
data-testid="media-video-status" data-state={state}>{t(`media.processing.${state}`, { progress: item.progress ?? 0 })}</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>
|
|
)
|
|
}
|