// 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 ( {confirm ? <> {t('media.deleteConfirm')} : <> } }> {!item ?
: (
{item.kind === 'video' ?
setName(e.target.value)} data-testid="media-name" />

{t(item.builtin ? 'media.builtinHint' : 'media.displayNameHint')}

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 }))]} />
{rows.map(([k, v]) =>
{k}
{v}
)}

{t('media.sizes')}

{t(item.kind === 'video' ? 'media.sizesVideo' : 'media.sizesHint')}

{state &&

{t(`media.processing.${state}`, { progress: item.progress ?? 0 })}

}
    {VARIANT_ORDER.filter(v => item.files?.[v]).map(v => (
  • {t(`media.variants.${v}`)} {resolution(item.files[v].width, item.files[v].height)} {fileSize(item.files[v].bytes, formatNumber)}
  • ))}

{t('media.usedBy')}

{used.length === 0 ?

{t('media.unused')}

: <>
    {used.map(u =>
  • {t(`media.uses.${u.kind}`, { defaultValue: u.kind })}: {u.name}
  • )}

{t('media.usedCantDelete')}

}
)}
) }