Spaces:
Paused
Paused
Download hooks/useFileUpload.ts from TwanAPI/testupload: direct link, hf CLI and curl.
- Browser
- Download file 1.94 kB
-
https://huggingface.co/spaces/TwanAPI/testupload/resolve/94b2ef3b9ff9f05c67f3897a2fc05dc8fd9cee29/hooks/useFileUpload.ts
- Command line
-
hf download hf://spaces/TwanAPI/testupload@94b2ef3b9ff9f05c67f3897a2fc05dc8fd9cee29/hooks/useFileUpload.ts
-
curl -L -o useFileUpload.ts https://huggingface.co/spaces/TwanAPI/testupload/resolve/94b2ef3b9ff9f05c67f3897a2fc05dc8fd9cee29/hooks/useFileUpload.ts
1.94 kB
| import { useState, useCallback } from 'react'; | |
| import { FileItem, UploadStatus } from '../types'; | |
| import { uploadFileToHub } from '../services/hfService'; | |
| export const useFileUpload = () => { | |
| // Local Upload State | |
| const [files, setFiles] = useState<FileItem[]>([]); | |
| const [isUploading, setIsUploading] = useState(false); | |
| // --- UPLOAD LOGIC --- | |
| 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 uploadPromises = filesToUpload.map(async (item) => { | |
| setFiles((prev) => | |
| prev.map((f) => | |
| f.id === item.id ? { ...f, status: UploadStatus.UPLOADING, error: undefined } : f | |
| ) | |
| ); | |
| try { | |
| const url = await uploadFileToHub({ | |
| file: item.file, | |
| path: item.path | |
| }); | |
| setFiles((prev) => | |
| prev.map((f) => | |
| f.id === item.id ? { ...f, status: UploadStatus.SUCCESS, url } : f | |
| ) | |
| ); | |
| } catch (err: any) { | |
| setFiles((prev) => | |
| prev.map((f) => | |
| f.id === item.id ? { ...f, status: UploadStatus.ERROR, error: err.message } : f | |
| ) | |
| ); | |
| } | |
| }); | |
| await Promise.allSettled(uploadPromises); | |
| setIsUploading(false); | |
| }, [files]); | |
| return { | |
| files, | |
| isUploading, | |
| addFiles, | |
| removeFile, | |
| updateFilePath, | |
| startUpload | |
| }; | |
| }; |