diff --git a/src/contexts/account/index.tsx b/src/contexts/account/index.tsx index e53d229b..b8a7339d 100644 --- a/src/contexts/account/index.tsx +++ b/src/contexts/account/index.tsx @@ -2,10 +2,6 @@ import type { Signer } from '@polkadot/api/types'; import { InjectedAccountWithMeta } from '@polkadot/extension-inject/types'; import React, { createContext, useContext, useEffect, useReducer } from 'react'; -const APP_NAME = 'Corehub'; -const LOCAL_STORAGE_ACCOUNTS = 'accounts'; -const LOCAL_STORAGE_ACTIVE_ACCOUNT = 'active-account'; - export enum KeyringState { // eslint-disable-next-line no-unused-vars DISCONNECTED = 'disconnected', @@ -87,8 +83,7 @@ const AccountProvider = ({ children }: Props) => { const [state, dispatch] = useReducer(reducer, initialState); const setActiveAccount = (acct: any) => { - localStorage.setItem(LOCAL_STORAGE_ACTIVE_ACCOUNT, JSON.stringify(acct)); - dispatch({ type: 'SET_ACTIVE_ACCOUNT', payload: acct.address }); + dispatch({ type: 'SET_ACTIVE_ACCOUNT', payload: acct }); }; const connectWallet = () => { @@ -96,10 +91,13 @@ const AccountProvider = ({ children }: Props) => { const asyncLoadAccounts = async () => { try { const extensionDapp = await import('@polkadot/extension-dapp'); - const { web3Accounts } = extensionDapp; + const { web3Accounts, web3Enable } = extensionDapp; + await web3Enable('Corehub'); const accounts: InjectedAccountWithMeta[] = await web3Accounts(); dispatch({ type: 'KEYRING_READY' }); dispatch({ type: 'SET_ACCOUNTS', payload: accounts }); + if (accounts.length) + dispatch({ type: 'SET_ACTIVE_ACCOUNT', payload: accounts[0] }); } catch (e) { dispatch({ type: 'KEYRING_ERROR' }); } @@ -107,21 +105,6 @@ const AccountProvider = ({ children }: Props) => { asyncLoadAccounts(); }; - useEffect(() => { - const accounts = state.accounts; - if (accounts.length) { - const activeAccount = localStorage.getItem(LOCAL_STORAGE_ACTIVE_ACCOUNT); - const account = activeAccount - ? accounts.find((acc: any) => acc.address == activeAccount) ?? - accounts[0] - : accounts[0]; - - dispatch({ type: 'SET_ACTIVE_ACCOUNT', payload: account }); - - localStorage.setItem(LOCAL_STORAGE_ACCOUNTS, JSON.stringify(accounts)); - } - }, [state.accounts]); - useEffect(() => { const getInjector = async () => { if (!state.activeAccount) return; @@ -134,28 +117,6 @@ const AccountProvider = ({ children }: Props) => { const disconnectWallet = () => dispatch({ type: 'DISCONNECT' }); - useEffect(() => { - const asyncLoad = async () => { - const { web3Enable } = await import('@polkadot/extension-dapp'); - await web3Enable(APP_NAME); - - const item = localStorage.getItem(LOCAL_STORAGE_ACCOUNTS); - if (!item) return; - try { - const accounts = JSON.parse(item) as InjectedAccountWithMeta[]; - if (accounts.length > 0) { - // load accounts automatically - dispatch({ type: 'KEYRING_READY' }); - dispatch({ type: 'SET_ACCOUNTS', payload: accounts }); - } - } catch { - // error handling - } - }; - - if ((window as any).injectedWeb3) asyncLoad(); - }, []); - return (