1- import useAxios from 'axios-hooks ' ;
2- import { type FormEvent , useCallback , useEffect } from 'react' ;
1+ import { createBasicFetcher } from '@h5web/app ' ;
2+ import { type FormEvent , useCallback , useEffect , useState } from 'react' ;
33import { FiLoader } from 'react-icons/fi' ;
44import { useSearchParams } from 'wouter' ;
55
6+ import { FetcherError } from '../../../../packages/app/src/providers/utils' ;
67import { type RemoteFile } from './models' ;
78import styles from './UrlForm.module.css' ;
89
10+ const fetcher = createBasicFetcher ( ) ;
11+
912interface Props {
1013 onLoad : ( h5File : RemoteFile ) => void ;
1114}
@@ -16,20 +19,29 @@ function UrlForm(props: Props) {
1619 const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
1720 const url = searchParams . get ( 'url' ) || '' ;
1821
19- const [ { loading, error } , execute ] = useAxios < ArrayBuffer > (
20- { url, responseType : 'arraybuffer' } ,
21- { manual : true } ,
22- ) ;
22+ const [ loading , setLoading ] = useState ( false ) ;
23+ const [ error , setError ] = useState < unknown > ( ) ;
2324
2425 const fetchFile = useCallback ( async ( ) => {
25- if ( url ) {
26- const { data } = await execute ( ) ;
26+ if ( ! url ) {
27+ return ;
28+ }
29+
30+ setLoading ( true ) ;
31+ setError ( undefined ) ;
32+
33+ try {
34+ const buffer = await fetcher ( url , { } ) ;
2735 onLoad ( {
2836 filename : url . slice ( url . lastIndexOf ( '/' ) + 1 ) ,
29- buffer : data ,
37+ buffer,
3038 } ) ;
39+ } catch ( error_ ) {
40+ setError ( error_ ) ;
41+ } finally {
42+ setLoading ( false ) ;
3143 }
32- } , [ url , execute , onLoad ] ) ;
44+ } , [ url , onLoad ] ) ;
3345
3446 useEffect ( ( ) => {
3547 void fetchFile ( ) ;
@@ -77,9 +89,9 @@ function UrlForm(props: Props) {
7789 </ form >
7890 { error && (
7991 < p className = { styles . hint } role = "alert" >
80- { error . isAxiosError
92+ { error instanceof FetcherError
8193 ? 'Sorry, your file could not be fetched'
82- : `An error occured: ${ error . message } ` }
94+ : `An error occured${ error instanceof Error ? ` : ${ error . message } ` : '' } ` }
8395 </ p >
8496 ) }
8597 </ >
0 commit comments