'use client' import { CircleAlertIcon, FileIcon, UploadIcon } from 'lucide-react' import { useRouter } from 'next/navigation' import { useEffect, useRef, useState, type ChangeEvent } from 'react' import type { ContentPieceUploadResponse } from '@/app/(campaign)/campanha/(app)/comunicacao/conteudos/types' import { Alert, AlertDescription } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Spinner } from '@/components/ui/Spinner' import { CAMPAIGN_CONTENT_PIECE_UPLOAD_HREF } from '@/lib/campaignPaths' import { CONTENT_PIECE_ACCEPT, CONTENT_PIECE_BATCH_HELP, CONTENT_PIECE_BATCH_LIMIT_MESSAGE, CONTENT_PIECE_BATCH_MAX_FILES, CONTENT_PIECE_MAX_BYTES, CONTENT_PIECE_MAX_SIZE_LABEL, contentPieceTooLargeMessage, contentPieceTypeFromMime, } from '@/lib/contentPiece' import { formatRecordingFileSize } from '@/lib/recording' import { cn } from '@/lib/utils' const GENERIC_ERROR_MESSAGE = 'Não foi possível enviar a peça. Tente novamente.' /** Two uploads in flight at once: real progress per file without saturating the box. */ const MAX_CONCURRENT_UPLOADS = 2 type FilePhase = 'queued' | 'uploading' | 'uploaded' | 'failed' type BatchFile = { /** Stable key: the File object identity survives a state update. */ id: string file: File phase: FilePhase progress: number error: string | null } const newId = (): string => typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(16).slice(2)}` /** * C211 — the batch upload surface of the Central (approved design scenes 2 and * 5): each picked file becomes an independent draft, uploads run two at a time * with real `upload.onprogress`, and a failed file never stops the others. The * same raw-body route as the C199 recording upload, one request per file. */ export const ContentPieceUploadDialog = ({ triggerClassName, triggerVariant = 'default', triggerLabel = 'Enviar peças', triggerAriaLabel, }: { triggerClassName?: string triggerVariant?: 'default' | 'outline' triggerLabel?: string triggerAriaLabel?: string }) => { const router = useRouter() const fileInputRef = useRef(null) const xhrsRef = useRef(new Map()) const queueRef = useRef([]) const filesRef = useRef([]) const [open, setOpen] = useState(false) const [files, setFiles] = useState([]) const [notice, setNotice] = useState(null) const uploading = files.some((entry) => entry.phase === 'uploading') useEffect(() => { filesRef.current = files }, [files]) // Navigating away unmounts the dialog: abort the in-flight uploads instead of // leaving invisible multi-GB transfers running. useEffect( () => () => { for (const xhr of xhrsRef.current.values()) xhr.abort() xhrsRef.current.clear() }, [], ) const updateFile = (id: string, patch: Partial) => { setFiles((current) => current.map((entry) => (entry.id === id ? { ...entry, ...patch } : entry)), ) } const reset = () => { for (const xhr of xhrsRef.current.values()) xhr.abort() xhrsRef.current.clear() queueRef.current = [] setFiles([]) setNotice(null) if (fileInputRef.current) fileInputRef.current.value = '' } const drainQueue = () => { while (xhrsRef.current.size < MAX_CONCURRENT_UPLOADS && queueRef.current.length > 0) { const nextId = queueRef.current.shift() if (!nextId) return const entry = filesRef.current.find((item) => item.id === nextId) if (entry && entry.phase === 'queued') uploadOne(entry) } } const uploadOne = (entry: BatchFile) => { const params = new URLSearchParams({ filename: entry.file.name }) const xhr = new XMLHttpRequest() xhrsRef.current.set(entry.id, xhr) updateFile(entry.id, { phase: 'uploading', progress: 0, error: null }) xhr.open('POST', `${CAMPAIGN_CONTENT_PIECE_UPLOAD_HREF}?${params.toString()}`) xhr.setRequestHeader('Accept', 'application/json') xhr.upload.addEventListener('progress', (event) => { if (event.lengthComputable && event.total > 0) { updateFile(entry.id, { progress: Math.round((event.loaded / event.total) * 100) }) } }) const settle = (patch: Partial) => { xhrsRef.current.delete(entry.id) updateFile(entry.id, patch) drainQueue() } xhr.addEventListener('load', () => { try { const payload = JSON.parse(xhr.responseText) as ContentPieceUploadResponse if (xhr.status >= 200 && xhr.status < 300 && payload.status === 'success') { settle({ phase: 'uploaded', progress: 100, error: null }) } else { settle({ phase: 'failed', error: payload.status === 'error' ? payload.message : GENERIC_ERROR_MESSAGE, }) } } catch { settle({ phase: 'failed', error: GENERIC_ERROR_MESSAGE }) } }) const onTransportError = () => settle({ phase: 'failed', error: GENERIC_ERROR_MESSAGE }) xhr.addEventListener('error', onTransportError) xhr.addEventListener('abort', onTransportError) xhr.send(entry.file) } const retry = (entry: BatchFile) => { queueRef.current.push(entry.id) updateFile(entry.id, { phase: 'queued', progress: 0, error: null }) window.setTimeout(() => drainQueue(), 0) } const onFilesChange = (event: ChangeEvent) => { setNotice(null) const picked = [...(event.target.files ?? [])] if (fileInputRef.current) fileInputRef.current.value = '' if (picked.length === 0) return if (filesRef.current.length + picked.length > CONTENT_PIECE_BATCH_MAX_FILES) { setNotice(CONTENT_PIECE_BATCH_LIMIT_MESSAGE) return } const accepted: BatchFile[] = [] let refusedReason: string | null = null for (const file of picked) { if (contentPieceTypeFromMime(file.type, file.name) === null) { refusedReason ??= 'Alguns arquivos foram ignorados: envie vídeo, áudio, foto ou texto.' continue } if (file.size > CONTENT_PIECE_MAX_BYTES) { refusedReason ??= contentPieceTooLargeMessage(file.size) continue } accepted.push({ id: newId(), file, phase: 'queued', progress: 0, error: null }) } setFiles((current) => [...current, ...accepted]) if (refusedReason) setNotice(refusedReason) for (const entry of accepted) queueRef.current.push(entry.id) // Let the state commit before the queue reads it. window.setTimeout(() => drainQueue(), 0) } return ( { if (uploading) return setOpen(next) if (!next) reset() }} > { if (uploading) event.preventDefault() }} onEscapeKeyDown={(event) => { if (uploading) event.preventDefault() }} > Enviar peças {CONTENT_PIECE_BATCH_HELP}
{notice ? ( ) : null}
{files.length > 0 ? (
    {files.map((entry) => (
  • {entry.phase === 'uploading' ? ( <>
    {entry.file.name} {entry.progress}%
    ) : (
    {entry.phase === 'failed' ? ( ) : entry.phase === 'uploaded' ? ( Enviado ) : null}
    )}
  • ))}
) : null} {uploading ? (

Cada arquivo vira um rascunho independente. Mantenha esta janela aberta até concluir.

) : null}
{uploading ? ( ) : ( <> )}
) }