// 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. // Folders (6R.16): the bar under the toolbar picks a folder (uploads go into it); a file's folder is // changed in its details. 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, Page, Select, Spinner, cx } from '@xenia/ui' import { listFolders, listMedia } from './mediaApi' import MediaFolders from './MediaFolders' import { ACCEPT, preparing } 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 [collection, setCollection] = 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 [folder, setFolder] = useState('') // '' all | 'none' | a folder id const [folders, setFolders] = useState(null) // { folders, none } const loadFolders = useCallback(async () => { try { setFolders(await listFolders()) } catch { /* the bar waits */ } }, []) useEffect(() => { loadFolders() }, [loadFolders]) 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, collection, folder })); setError(null) } catch (err) { setError(errorMessage(err, t)) } }, [kind, search, collection, folder, t]) useEffect(() => { load() }, [load]) // Videos are prepared for screens in the background (5.4e): read the list again while any is. const busy = preparing(page?.items) useEffect(() => { if (!busy) return const h = setInterval(load, 3000) return () => clearInterval(h) }, [busy, 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(); loadFolders() } wasBusy.current = summary.busy }, [summary.busy, load, loadFolders]) const onDrop = e => { e.preventDefault(); setDragging(false) if (e.dataTransfer.files?.length) add(e.dataTransfer.files, upTo) } 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), []) const upTo = /^\d+$/.test(folder) ? Number(folder) : null // uploads go into the folder shown const allCount = folders ? folders.none + folders.folders.reduce((n, f) => n + f.count, 0) : undefined return ( { if ([...(e.dataTransfer?.types || [])].includes('Files')) { e.preventDefault(); setDragging(true) } }} onDragLeave={e => { if (e.currentTarget === e.target) setDragging(false) }} onDrop={onDrop}>

{t('media.title')}

{t('media.intro')}

{ if (e.target.files?.length) add(e.target.files, upTo); e.target.value = '' }} /> {t('media.dropHint')}
setQ(e.target.value)} placeholder={t('media.search')} aria-label={t('media.search')} className="flex-1 sm:w-56" data-testid="media-search" /> setCollection(e.target.value)} aria-label={t('media.collection')} data-testid="media-collection" options={[{ value: '', label: t('media.collections.all') }, { value: 'own', label: t('media.collections.own') }, { value: 'builtin', label: t('media.collections.builtin') }]} />
{error ? : !page ?
: page.items.length === 0 ? : <>

{t('media.count', { count: page.total })}

setOpen(item.id)} /> {page.total > page.items.length &&

{t('media.more', { shown: page.items.length, total: page.total })}

} } {open && { setPage(p => p && { ...p, items: p.items.map(x => (x.id === m.id ? { ...x, ...m } : x)) }); loadFolders() }} onDeleted={id => setPage(p => p && { ...p, items: p.items.filter(x => x.id !== id), total: p.total - 1 })} />}
) }