import * as React from 'react' ; import { UserNoPassword } from 'YesterTech/types' ; import { makeAutoObservable } from 'mobx' ; function getInitialState ( ) { return { authenticated : false , user : null , } ; } class Auth { authenticated = false ; user : null | UserNoPassword = null ; constructor ( ) { makeAutoObservable ( this ) ; } dispatch ( action ) { switch ( action . type ) { case 'LOGIN' : { this . authenticated = true ; this . user = action . user ; break ; } case 'LOGOUT' : { this . authenticated = false ; this . user = null ; break ; } } } } let auth = new Auth ( ) ; let AuthContext = React . createContext < Auth > ( null ! ) ; export const AuthStateProvider = ( { children } ) => { return < AuthContext . Provider value = { auth } > { children } < /AuthContext.Provider>; } ; export function useAuthDispatch ( ) { const auth = React . useContext ( AuthContext ) ; return React . useCallback ( ( action ) => { return auth . dispatch ( action ) ; } , [ ] ) ; } export function useAuthState ( ) { return React . useContext ( AuthContext ) ; } export interface AuthState { authenticated : boolean ; user : null | UserNoPassword ; } type AuthActions = { type : 'LOGIN' ; user : UserNoPassword ; } | { type : 'LOGOUT' ; } ;