import { createContext, FC, ReactNode, useCallback, useContext, useState } from 'react' import type { Error as ErrorProps } from '../models' import { ErrorComp } from '../components' import { useCustomRender } from './config' interface ErrorDetails { title: string description: string statusCode?: number } interface ErrorDisplayProps extends ErrorDetails { children?: ReactNode } interface ErrorContextType { error: ErrorDetails | null setError: (error: ErrorDetails | null) => void } export const DisplayError: FC = ({ title, description, statusCode, children }) => { const props: ErrorProps = { title, description, statusCode, children, type: 'Error', } const CustomRenderComp = useCustomRender(props) if (CustomRenderComp) { return } else { return } } export const ErrorContext = createContext({ error: null, setError: () => null, }) const MaybeError: FC<{ children: ReactNode }> = ({ children }) => { const { error } = useContext(ErrorContext) if (error) { return {children} } else { return <>{children} } } interface Props { children: ReactNode } export const ErrorContextProvider: FC = ({ children }) => { const [error, setErrorState] = useState(null) const setError = useCallback( (error: ErrorDetails | null) => { if (error) { console.warn('setting error:', error) } setErrorState(error) }, [setErrorState], ) return ( {children} ) }