Files
xenia-pos-local/manager_dashboard/src/pages/media/mediaHooks.js
T
bonaminandClaude Opus 5.5 e09056a7a4 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>
2026-11-17 15:57:50 +02:00

52 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Hooks of the media library's screens (plan step 5.2).
import { useCallback, useEffect, useRef, useState } from 'react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { uploadMedia } from './mediaApi'
import { duration, fileSize, queueFrom, queueSummary, resolution } from './mediaLogic'
/** "1600 × 1200 · 245 KB" (or the length for a video) */
export function useMediaMeta() {
const { formatNumber } = useFormat()
return useCallback(item => [
item.kind === 'video' ? duration(item.duration_ms) : '',
resolution(item.width, item.height),
fileSize(item.bytes, formatNumber),
].filter(Boolean).join(' · '), [formatNumber])
}
/**
* Uploads files one after another. `add(files)` queues them; `onDone(item, existing)` runs for
* every file that made it into the library.
*/
export function useUploadQueue(onDone) {
const { t } = useTranslation('manager')
const [queue, setQueue] = useState([])
const running = useRef(false)
const done = useRef(onDone)
useEffect(() => { done.current = onDone })
const counter = useRef(0)
const add = useCallback(files => {
const list = queueFrom(files, counter.current)
counter.current += list.length
setQueue(q => [...q, ...list])
}, [])
const clear = useCallback(() => setQueue(q => q.filter(x => x.status === 'waiting' || x.status === 'uploading')), [])
useEffect(() => {
if (running.current) return
const next = queue.find(q => q.status === 'waiting')
if (!next) return
running.current = true
const set = patch => setQueue(q => q.map(x => (x.key === next.key ? { ...x, ...patch } : x)))
set({ status: 'uploading' })
uploadMedia(next.file, { onProgress: progress => set({ progress }) })
.then(item => { set({ status: item.existing ? 'existing' : 'added', progress: 100, item }); done.current?.(item, !!item.existing) })
.catch(err => set({ status: 'failed', error: errorMessage(err, t) }))
.finally(() => { running.current = false; setQueue(q => [...q]) })
}, [queue, t])
return { queue, add, clear, summary: queueSummary(queue) }
}