Refactor media upload handling in MediaPanel: implement support for multiple file uploads with progress tracking, update UI to display current upload status, and enhance translations for uploaded file count.
This commit is contained in:
@@ -32,7 +32,9 @@ export function MediaPanel() {
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const fileInput = useRef<HTMLInputElement>(null)
|
const fileInput = useRef<HTMLInputElement>(null)
|
||||||
const [progress, setProgress] = useState<number | null>(null)
|
const [upload, setUpload] = useState<{ current: number; total: number; percent: number } | null>(
|
||||||
|
null,
|
||||||
|
)
|
||||||
|
|
||||||
const { data, isLoading } = useQuery({
|
const { data, isLoading } = useQuery({
|
||||||
queryKey: ['admin', 'media'],
|
queryKey: ['admin', 'media'],
|
||||||
@@ -50,18 +52,26 @@ export function MediaPanel() {
|
|||||||
|
|
||||||
const deleteMutation = useMutation({ mutationFn: deleteMedia, onSuccess: invalidate, onError })
|
const deleteMutation = useMutation({ mutationFn: deleteMedia, onSuccess: invalidate, onError })
|
||||||
|
|
||||||
const handleFile = async (file: File) => {
|
// Массовая загрузка: файлы отправляются по одному (обработка всё равно в очереди по одному),
|
||||||
setProgress(0)
|
// прогресс — «текущий/всего · %». Список обновляется после каждого файла.
|
||||||
try {
|
const handleFiles = async (files: FileList) => {
|
||||||
await uploadMedia(file, setProgress)
|
const list = Array.from(files)
|
||||||
toast.success(t('admin.media.uploaded'))
|
let uploaded = 0
|
||||||
invalidate()
|
for (let i = 0; i < list.length; i++) {
|
||||||
} catch (error) {
|
setUpload({ current: i + 1, total: list.length, percent: 0 })
|
||||||
onError(error)
|
try {
|
||||||
} finally {
|
await uploadMedia(list[i], (percent) =>
|
||||||
setProgress(null)
|
setUpload({ current: i + 1, total: list.length, percent }),
|
||||||
if (fileInput.current) fileInput.current.value = ''
|
)
|
||||||
|
uploaded++
|
||||||
|
invalidate()
|
||||||
|
} catch (error) {
|
||||||
|
onError(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
setUpload(null)
|
||||||
|
if (fileInput.current) fileInput.current.value = ''
|
||||||
|
if (uploaded > 0) toast.success(t('admin.media.uploadedCount', { count: uploaded }))
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -69,24 +79,23 @@ export function MediaPanel() {
|
|||||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||||
<h2 className="crt-glow text-xl font-semibold">{t('admin.media.title')}</h2>
|
<h2 className="crt-glow text-xl font-semibold">{t('admin.media.title')}</h2>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
{progress != null && (
|
{upload != null && (
|
||||||
<span className="text-sm text-muted-foreground">{progress}%</span>
|
<span className="text-sm text-muted-foreground">
|
||||||
|
{upload.current}/{upload.total} · {upload.percent}%
|
||||||
|
</span>
|
||||||
)}
|
)}
|
||||||
<input
|
<input
|
||||||
ref={fileInput}
|
ref={fileInput}
|
||||||
type="file"
|
type="file"
|
||||||
accept="video/*,.mkv,.avi,.ts"
|
accept="video/*,.mkv,.avi,.ts"
|
||||||
|
multiple
|
||||||
className="hidden"
|
className="hidden"
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const file = e.target.files?.[0]
|
const files = e.target.files
|
||||||
if (file) void handleFile(file)
|
if (files && files.length > 0) void handleFiles(files)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button size="sm" disabled={upload != null} onClick={() => fileInput.current?.click()}>
|
||||||
size="sm"
|
|
||||||
disabled={progress != null}
|
|
||||||
onClick={() => fileInput.current?.click()}
|
|
||||||
>
|
|
||||||
<Upload className="h-4 w-4" />
|
<Upload className="h-4 w-4" />
|
||||||
{t('admin.media.upload')}
|
{t('admin.media.upload')}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ const resources = {
|
|||||||
title: 'Медиа',
|
title: 'Медиа',
|
||||||
upload: 'Загрузить',
|
upload: 'Загрузить',
|
||||||
uploaded: 'Файл загружен, идёт обработка',
|
uploaded: 'Файл загружен, идёт обработка',
|
||||||
|
uploadedCount: 'Загружено файлов: {{count}}',
|
||||||
name: 'Файл',
|
name: 'Файл',
|
||||||
status: 'Статус',
|
status: 'Статус',
|
||||||
duration: 'Длительность',
|
duration: 'Длительность',
|
||||||
@@ -273,6 +274,7 @@ const resources = {
|
|||||||
title: 'Media',
|
title: 'Media',
|
||||||
upload: 'Upload',
|
upload: 'Upload',
|
||||||
uploaded: 'File uploaded, processing started',
|
uploaded: 'File uploaded, processing started',
|
||||||
|
uploadedCount: 'Uploaded files: {{count}}',
|
||||||
name: 'File',
|
name: 'File',
|
||||||
status: 'Status',
|
status: 'Status',
|
||||||
duration: 'Duration',
|
duration: 'Duration',
|
||||||
|
|||||||
Reference in New Issue
Block a user