import * as React from 'react' ; import { FC , createContext , useState , useContext } from 'react' ; import { ContextType , AuthType , IAuth } from './types' ; const Context = createContext < any > ( null ) ; interface ProviderProps { value : AuthType ; } interface InjectProps { auth : ContextType [ 0 ] ; useAuth : ContextType [ 1 ] ; } const Provider = ( { value = { } , children } ) => { const [ state , setState ] = useState < AuthType > ( value ) ; const updateState = ( newState = { } ) => { setState ( { ... state , ... newState , } ) ; } ; return < Context . Provider value = { [ state , updateState ] } > { children } < /Context.Provider>; } ; const useAuth = ( ) : ContextType => { const value = useContext ( Context ) ; return value ; } ; function withAuth < Props extends InjectProps > ( Component ) { type OriginalProps = Omit < Props , keyof InjectProps > ; const AuthWrapped = ( props ) => { const [ auth , setAuth ] = useAuth ( ) ; const WrappedComponent = Component as React . ComponentType < OriginalProps > ; return < WrappedComponent { ... props } auth = { auth } setAuth = { setAuth } / > ; } ; return AuthWrapped ; } export { useAuth , withAuth , Provider } ; export type { IAuth } ;