LibreChat/client/src/hooks/ApiErrorBoundaryContext.tsx
Dan Orlando 7fdc862042
Build/Refactor: lint pre-commit hook and reformat repo to spec (#314)
* build/refactor: move lint/prettier packages to project root, install husky, add pre-commit hook

* refactor: reformat files

* build: put full eslintrc back with all rules
2023-05-18 14:09:31 -04:00

33 lines
787 B
TypeScript

import React, { useState } from 'react';
export type ApiError = {
error: any;
setError: (error: any) => void;
};
const ApiErrorBoundaryContext = React.createContext<ApiError | undefined>(undefined);
export const ApiErrorBoundaryProvider = ({
value,
children
}: {
value?: ApiError;
children: React.ReactNode;
}) => {
const [error, setError] = useState(false);
return (
<ApiErrorBoundaryContext.Provider value={value ? value : { error, setError }}>
{children}
</ApiErrorBoundaryContext.Provider>
);
};
export const useApiErrorBoundary = () => {
const context = React.useContext(ApiErrorBoundaryContext);
if (context === undefined) {
throw new Error('useApiErrorBoundary must be used inside ApiErrorBoundaryProvider');
}
return context;
};