github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/dashboard/frontend/src/hooks/useLoadingErrorHandler.ts (about) 1 import useLoading from './useLoading' 2 import useSnackbar from './useSnackbar' 3 4 type asyncFunction = { 5 (): Promise<void>, 6 } 7 8 type asyncFunctionBoolan = { 9 (): Promise<boolean>, 10 } 11 12 export const useLoadingErrorHandler = ({ 13 withSnackbar = true, 14 withLoading = true, 15 }: { 16 withSnackbar?: boolean, 17 withLoading?: boolean, 18 } = {}) => { 19 const loading = useLoading() 20 const snackbar = useSnackbar() 21 return (handler: asyncFunction): asyncFunctionBoolan => { 22 return async (): Promise<boolean> => { 23 let sawError = false 24 if(withLoading) loading.setLoading(true) 25 try { 26 await handler() 27 } catch(e: any) { 28 sawError = true 29 // if(e.response) console.error(e.response.body) 30 // if(withSnackbar) snackbar.error(e.toString()) 31 } 32 if(withLoading) loading.setLoading(false) 33 return sawError 34 } 35 } 36 } 37 38 export default useLoadingErrorHandler