-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
78 lines (70 loc) · 2.08 KB
/
Copy pathmain.tsx
File metadata and controls
78 lines (70 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider, MutationCache } from '@tanstack/react-query';
import App from './App.tsx';
import './index.css';
import { toast } from 'react-hot-toast';
import { AxiosError } from 'axios';
declare module '@tanstack/react-query' {
interface Register {
mutationMeta: {
ignoreGlobalError?: boolean
}
}
}
interface ApiErrorResponse {
message?: string;
errors?: string | Record<string, unknown>;
}
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 1000 * 60,
},
},
mutationCache: new MutationCache({
onError: (error: unknown, _variables, _context, mutation) => {
if (mutation.meta?.ignoreGlobalError) {
return;
}
const axiosError = error as AxiosError<ApiErrorResponse>;
const status = axiosError.response?.status;
const backendErrors = axiosError.response?.data?.errors;
if (typeof backendErrors === 'string') {
toast.error(backendErrors);
return;
}
if (status && status >= 500) {
toast.error('Error interno del servidor. Por favor intenta más tarde.');
return;
}
if (!axiosError.response) {
toast.error('No se pudo conectar con el servidor. Revisa tu conexión.');
return;
}
toast.error('Ocurrió un error inesperado.');
},
}),
});
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<BrowserRouter>
<App />
</BrowserRouter>
{import.meta.env.DEV && (
<React.Suspense fallback={null}>
{React.createElement(
React.lazy(() =>
import('@tanstack/react-query-devtools').then(res => ({
default: res.ReactQueryDevtools
}))
),
{ initialIsOpen: false }
)}
</React.Suspense>
)}
</QueryClientProvider>
</React.StrictMode>,
);