Rework error provider

This commit is contained in:
Zoe Roux
2025-02-03 00:55:13 +01:00
parent 5d6bb63ba2
commit 7211699e87
7 changed files with 118 additions and 70 deletions
+12 -3
View File
@@ -1,5 +1,6 @@
import type { ReactElement } from "react";
import { ErrorView, OfflineView } from "~/ui/errors";
import { useSetError } from "~/providers/error-provider";
import { ErrorView } from "~/ui/errors";
import { type QueryIdentifier, useFetch } from "./query";
export const Fetch = <Data,>({
@@ -12,9 +13,17 @@ export const Fetch = <Data,>({
Loader: () => ReactElement;
}): JSX.Element | null => {
const { data, isPaused, error } = useFetch(query);
const setError = useSetError();
if (error) return <ErrorView error={error} />;
if (isPaused) return <OfflineView />;
if (error) {
if (error.status === 401 || error.status === 403) {
setError({ key: "unauthorized", error });
}
return <ErrorView error={error} />;
}
if (isPaused) {
setError({ key: "offline" });
}
if (!data) return <Loader />;
return <Render {...data} />;
};