Kyoo/front/src/providers/error-consumer.tsx
2025-06-07 19:32:57 +02:00

15 lines
563 B
TypeScript

import { type ReactNode, useContext } from "react";
import { ErrorView, errorHandlers } from "~/ui/errors";
import { ErrorContext } from "./error-provider";
export const ErrorConsumer = ({ children, scope }: { children: ReactNode; scope: string }) => {
const error = useContext(ErrorContext);
if (!error) return children;
const handler = errorHandlers[error.key] ?? { view: ErrorView };
if (handler.forbid && handler.forbid !== scope) return children;
const Handler = handler.view;
const { key, ...val } = error;
return <Handler {...(val as any)} />;
};