text-dataset-tiny-code-script-py-format / ysnrfd_messenger /BACKUP2 /frontend /src /hooks /useFileUpload.js
| import { useState, useCallback } from 'react'; | |
| import { uploadService } from '../services/api'; | |
| export const useFileUpload = () => { | |
| const [uploading, setUploading] = useState(false); | |
| const [progress, setProgress] = useState(0); | |
| const [error, setError] = useState(null); | |
| const uploadFile = useCallback(async (file) => { | |
| try { | |
| setUploading(true); | |
| setProgress(0); | |
| setError(null); | |
| const response = await uploadService.uploadFile(file, (progress) => { | |
| setProgress(progress); | |
| }); | |
| setUploading(false); | |
| setProgress(100); | |
| return response; | |
| } catch (error) { | |
| setError(error.error || 'File upload failed'); | |
| setUploading(false); | |
| throw error; | |
| } | |
| }, []); | |
| const reset = useCallback(() => { | |
| setUploading(false); | |
| setProgress(0); | |
| setError(null); | |
| }, []); | |
| return { | |
| uploadFile, | |
| uploading, | |
| progress, | |
| error, | |
| reset | |
| }; | |
| }; |