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>
111 lines
6.7 KiB
React
111 lines
6.7 KiB
React
// 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 (
|
|
<Page className={cx(dragging && 'outline-2 outline-dashed outline-accent -outline-offset-8')}
|
|
testId="media-page"
|
|
onDragOver={e => { if ([...(e.dataTransfer?.types || [])].includes('Files')) { e.preventDefault(); setDragging(true) } }}
|
|
onDragLeave={e => { if (e.currentTarget === e.target) setDragging(false) }}
|
|
onDrop={onDrop}>
|
|
<header className="grid gap-1">
|
|
<h1 className="text-heading font-semibold text-ink">{t('media.title')}</h1>
|
|
<p className="text-body text-ink-2 max-w-3xl">{t('media.intro')}</p>
|
|
</header>
|
|
|
|
<div className="flex flex-wrap gap-2 items-center">
|
|
<Button icon={<Upload size={16} />} onClick={() => input.current?.click()} data-testid="media-upload">{t('media.upload')}</Button>
|
|
<input ref={input} type="file" multiple accept={ACCEPT} className="sr-only" data-testid="media-file-input"
|
|
onChange={e => { if (e.target.files?.length) add(e.target.files, upTo); e.target.value = '' }} />
|
|
<span className="text-label text-ink-3 hidden md:inline">{t('media.dropHint')}</span>
|
|
<div className="flex gap-2 ml-auto w-full sm:w-auto">
|
|
<Input value={q} onChange={e => setQ(e.target.value)} placeholder={t('media.search')} aria-label={t('media.search')}
|
|
className="flex-1 sm:w-56" data-testid="media-search" />
|
|
<Select value={kind} onChange={e => setKind(e.target.value)} options={kinds} aria-label={t('media.kind')} data-testid="media-kind" />
|
|
<Select value={collection} onChange={e => 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') }]} />
|
|
</div>
|
|
</div>
|
|
|
|
<MediaFolders folders={folders?.folders} none={folders?.none} total={allCount} value={folder} onChange={setFolder} onChanged={loadFolders} />
|
|
|
|
<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 || kind || collection ? 'media.noMatch' : 'media.empty')} description={search || kind || collection ? undefined : t('media.emptyHint')} />
|
|
: <>
|
|
<p className="text-label text-ink-3" data-testid="media-count">{t('media.count', { count: page.total })}</p>
|
|
<MediaGrid items={page.items} onOpen={item => setOpen(item.id)} />
|
|
{page.total > page.items.length && <p className="text-label text-ink-3">{t('media.more', { shown: page.items.length, total: page.total })}</p>}
|
|
</>}
|
|
|
|
{open && <MediaDetails id={open} onClose={closeDetails} folders={folders?.folders || []}
|
|
onChanged={m => { 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 })} />}
|
|
</Page>
|
|
)
|
|
}
|