Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo authored May 8, 2024
1 parent 3b56f39 commit 49b6879
Showing 1 changed file with 5 additions and 44 deletions.
49 changes: 5 additions & 44 deletions src/contexts/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -87,41 +83,28 @@ 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 = () => {
dispatch({ type: 'LOAD_KEYRING' });
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' });
}
};
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;
Expand All @@ -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 (
<AccountDataContext.Provider
value={{ state, setActiveAccount, connectWallet, disconnectWallet }}
Expand Down

0 comments on commit 49b6879

Please sign in to comment.