import React from 'react' ; import useScpContext from './scpContextProvider' ; import { FileState } from './types' ; export default function useHttpTransfer ( { blob , url , isUpload } ) { const { createDownloader , createUploader } = useScpContext ( ) ; const [ http ] = React . useState ( ( ) => { return isUpload ? createUploader ( ) : createDownloader ( ) ; } ) ; const [ status , setStatus ] = React . useState ( { response : null , progress : 0 , state : 'processing' as FileState , error : '' , } ) ; React . useEffect ( ( ) => { function handleProgress ( progress ) { setStatus ( { ... status , progress , } ) ; } function handleCompleted ( response ) { setStatus ( { ... status , response , state : 'completed' , } ) ; } function handleFailed ( err ) { setStatus ( { ... status , error : err . message , state : 'error' , } ) ; } http . onProgress ( handleProgress ) ; http . onCompleted ( handleCompleted ) ; http . onError ( handleFailed ) ; http . do ( url , blob ) ; function cleanup ( ) { http . removeAllListeners ( ) ; http . abort ( ) ; } return cleanup ; } , [ ] ) ; return status ; }