import { CONNECTOR_TYPES , UnauthenticatedConnector } from '../connectors' ; import { DEFAULT_NETWORKS , localStorageKeys , NetworkNames , WalletTypes , } from '../constants' ; import { Connector , Network } from '../types' ; import localStorage from './localStorage' ; export const loadPreviousConfig = ( defaultNetworkProp ) : { address : string | null ; network : Network | null ; connector : Connector ; } => { let lastUsedNetworkName = defaultNetworkProp . name ; let lastUsedAddress = null ; let lastUsedWalletType = WalletTypes . Unauthenticated ; let lastUsedWalletArguments = [ ] ; if ( typeof localStorage !== 'undefined' ) { const localLastUsedNetworkName = localStorage . getItem ( localStorageKeys . lastUsedNetwork ) ; if ( localLastUsedNetworkName ) { lastUsedNetworkName = localLastUsedNetworkName as NetworkNames ; } lastUsedAddress = localStorage . getItem ( localStorageKeys . lastUsedAddress ) ; const localLastUsedWalletType = localStorage . getItem ( localStorageKeys . lastUsedWalletType ) ; if ( localLastUsedWalletType && localLastUsedWalletType in WalletTypes ) { lastUsedWalletType = localLastUsedWalletType as WalletTypes ; } const localLastUsedWalletArguments = localStorage . getItem ( localStorageKeys . lastUsedWalletArguments ) ; if ( localLastUsedWalletArguments ) { try { lastUsedWalletArguments = JSON . parse ( localLastUsedWalletArguments ) as unknown [ ] ; } catch ( e ) { lastUsedWalletArguments = [ ] ; } } } const lastUsedNetwork = DEFAULT_NETWORKS . find ( ( n ) => n . name === lastUsedNetworkName ) ; let initialConnector ; if ( lastUsedWalletType && lastUsedNetwork ) { try { initialConnector = new CONNECTOR_TYPES [ lastUsedWalletType ] ( lastUsedNetwork , ... lastUsedWalletArguments ) ; } catch ( e ) { initialConnector = new UnauthenticatedConnector ( lastUsedNetwork || defaultNetworkProp ) ; } } else { initialConnector = new UnauthenticatedConnector ( lastUsedNetwork || defaultNetworkProp ) ; } return { address : lastUsedAddress , network : lastUsedNetwork || null , connector : initialConnector , } ; } ; export function clearPreviousConfig ( ) { Object . values ( localStorageKeys ) . forEach ( ( val ) => localStorage . removeItem ( val ) ) ; }