import { useCallback , useEffect , useReducer } from 'react' ; import { MetamaskAdapter } from '../adapters' ; import { Web3ActionsEnum } from './actions' ; import type { IWeb3State , UpdateWeb3Values , UseWeb3State , Web3Action , } from './types' ; const reducer = ( state , action ) : IWeb3State => { switch ( action . type ) { case Web3ActionsEnum . UPDATE_STATE : return { ... state , ... action . payload } ; case Web3ActionsEnum . RESET_STATE : return { } ; default : return state ; } } ; export const useWeb3State = ( ) => { const [ state , dispatch ] = useReducer ( reducer , { } ) ; const handleUpdate = useCallback ( ( payload ) => { dispatch ( { type : Web3ActionsEnum . UPDATE_STATE , payload , } ) ; } , [ ] ) ; const handleClose = useCallback ( ( ) => { dispatch ( { type : Web3ActionsEnum . RESET_STATE } ) ; } , [ ] ) ; const connect = useCallback ( async ( adapter ) => { const { account , provider , chainId } = await adapter . enable ( handleUpdate , handleClose ) ; dispatch ( { type : Web3ActionsEnum . UPDATE_STATE , payload : { account , chainId , provider , adapter : account && provider && chainId ? adapter : undefined , } , } ) ; } , [ ] ) ; const disconnect = useCallback ( async ( ) => { if ( state . adapter ) await state . adapter . disable ( ) ; return ; } , [ state . adapter ] ) ; useEffect ( ( ) => { return ( ) => { disconnect ( ) ; } ; } , [ ] ) ; return { ... state , connect , disconnect , handleUpdate , handleClose , } ; } ;