|
|
|
|
|
import { useState, useCallback } from 'react'; |
|
|
import { FileItem, UploadStatus } from '../types'; |
|
|
import { uploadBatchToHub } from '../services/hfService'; |
|
|
|
|
|
export const useFileUpload = () => { |
|
|
const [files, setFiles] = useState<FileItem[]>([]); |
|
|
const [isUploading, setIsUploading] = useState(false); |
|
|
|
|
|
|
|
|
const BATCH_SIZE = 5; |
|
|
const CONCURRENCY_LIMIT = 3; |
|
|
|
|
|
|
|
|
|
|
|
const addFiles = useCallback((newFilesList: FileItem[]) => { |
|
|
setFiles((prev) => [...prev, ...newFilesList]); |
|
|
}, []); |
|
|
|
|
|
const removeFile = useCallback((id: string) => { |
|
|
setFiles((prev) => prev.filter((f) => f.id !== id)); |
|
|
}, []); |
|
|
|
|
|
const updateFilePath = useCallback((id: string, newPath: string) => { |
|
|
setFiles((prev) => prev.map((f) => (f.id === id ? { ...f, path: newPath } : f))); |
|
|
}, []); |
|
|
|
|
|
const startUpload = useCallback(async () => { |
|
|
const filesToUpload = files.filter( |
|
|
(f) => f.status === UploadStatus.IDLE || f.status === UploadStatus.ERROR |
|
|
); |
|
|
|
|
|
if (filesToUpload.length === 0) return; |
|
|
|
|
|
setIsUploading(true); |
|
|
|
|
|
|
|
|
const batches = []; |
|
|
for (let i = 0; i < filesToUpload.length; i += BATCH_SIZE) { |
|
|
batches.push(filesToUpload.slice(i, i + BATCH_SIZE)); |
|
|
} |
|
|
|
|
|
|
|
|
const processBatch = async (batch: FileItem[]) => { |
|
|
|
|
|
setFiles((prev) => |
|
|
prev.map((f) => |
|
|
batch.find((b) => b.id === f.id) |
|
|
? { ...f, status: UploadStatus.UPLOADING, error: undefined } |
|
|
: f |
|
|
) |
|
|
); |
|
|
|
|
|
try { |
|
|
|
|
|
const batchPayload = batch.map(item => ({ |
|
|
id: item.id, |
|
|
file: item.file, |
|
|
path: item.path |
|
|
})); |
|
|
|
|
|
|
|
|
const urls = await uploadBatchToHub(batchPayload); |
|
|
|
|
|
|
|
|
setFiles((prev) => |
|
|
prev.map((f) => { |
|
|
const index = batch.findIndex(b => b.id === f.id); |
|
|
if (index !== -1) { |
|
|
return { ...f, status: UploadStatus.SUCCESS, url: urls[index] }; |
|
|
} |
|
|
return f; |
|
|
}) |
|
|
); |
|
|
} catch (err: any) { |
|
|
|
|
|
setFiles((prev) => |
|
|
prev.map((f) => |
|
|
batch.find((b) => b.id === f.id) |
|
|
? { ...f, status: UploadStatus.ERROR, error: err.message } |
|
|
: f |
|
|
) |
|
|
); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < batches.length; i += CONCURRENCY_LIMIT) { |
|
|
const activeBatches = batches.slice(i, i + CONCURRENCY_LIMIT); |
|
|
await Promise.allSettled(activeBatches.map(batch => processBatch(batch))); |
|
|
} |
|
|
|
|
|
setIsUploading(false); |
|
|
}, [files]); |
|
|
|
|
|
return { |
|
|
files, |
|
|
isUploading, |
|
|
addFiles, |
|
|
removeFile, |
|
|
updateFilePath, |
|
|
startUpload |
|
|
}; |
|
|
}; |
|
|
|