import { useEffect } from 'react' ; import useAuthProvider from './useAuthProvider' ; import { UserIdentity } from '../types' ; import { useSafeSetState } from '../util/hooks' ; const defaultIdentity = { id : '' , fullName : null , } ; returns example const useGetIdentity = ( ) => { const [ state , setState ] = useSafeSetState < State > ( { loading : true , loaded : false , } ) ; const authProvider = useAuthProvider ( ) ; useEffect ( ( ) => { if ( authProvider && typeof authProvider . getIdentity === 'function' ) { const callAuthProvider = async ( ) => { try { const identity = await authProvider . getIdentity ( ) ; setState ( { loading : false , loaded : true , identity : identity || defaultIdentity , } ) ; } catch ( error ) { setState ( { loading : false , loaded : true , error , } ) ; } } ; callAuthProvider ( ) ; } else { setState ( { loading : false , loaded : true , identity : defaultIdentity , } ) ; } } , [ authProvider , setState ] ) ; return state ; } ; interface State { loading : boolean ; loaded : boolean ; identity ? : UserIdentity ; error ? : any ; } export default useGetIdentity ;