autobrr/web/src/App.tsx
stacksmash76 1b6fd01575
fix(web): infinitely retry queries, remove ago from age cells (#528)
- infinitely retry web queries so we avoid the "failed to fetch" error when the web server is unavailable
- remove the "ago" suffix from age cells (closes #497)
2022-11-10 12:32:57 +01:00

54 lines
No EOL
1.7 KiB
TypeScript

import { QueryClient, QueryClientProvider, useQueryErrorResetBoundary } from "react-query";
import { ReactQueryDevtools } from "react-query/devtools";
import { ErrorBoundary } from "react-error-boundary";
import { toast, Toaster } from "react-hot-toast";
import { LocalRouter } from "./domain/routes";
import { AuthContext, SettingsContext } from "./utils/Context";
import { ErrorPage } from "./components/alerts";
import Toast from "./components/notifications/Toast";
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
// The retries will have exponential delay.
// See https://tanstack.com/query/v4/docs/guides/query-retries#retry-delay
// delay = Math.min(1000 * 2 ** attemptIndex, 30000)
retry: true,
useErrorBoundary: true
},
mutations: {
onError: (error) => {
// Use a format string to convert the error object to a proper string without much hassle.
const message = (
typeof (error) === "object" && typeof ((error as Error).message) ?
(error as Error).message :
`${error}`
);
toast.custom((t) => <Toast type="error" body={message} t={t} />);
}
}
}
});
export function App() {
const { reset } = useQueryErrorResetBoundary();
const authContext = AuthContext.useValue();
const settings = SettingsContext.useValue();
return (
<ErrorBoundary
onReset={reset}
fallbackRender={ErrorPage}
>
<QueryClientProvider client={queryClient}>
<Toaster position="top-right" />
<LocalRouter isLoggedIn={authContext.isLoggedIn} />
{settings.debug ? (
<ReactQueryDevtools initialIsOpen={false} />
) : null}
</QueryClientProvider>
</ErrorBoundary>
);
}