Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't remove wallet account if storageAddress is undefined #595

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/GlobalStateProvider/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const initTalisman = async (dAppName: string, selected?: string) => {
if (!wallet) return;
await wallet.enable(dAppName);
const accounts = await wallet.getAccounts();
const selectedWallet = accounts.find((a) => a.address === selected) || accounts[0];
return selectedWallet;
return accounts.find((a) => a.address === selected) || accounts[0];
};

const initWalletConnect = async (chainId: string) => {
Expand Down
28 changes: 15 additions & 13 deletions src/GlobalStateProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { ThemeName } from '../models/Theme';
import { storageService } from '../services/storage/local';
import { handleWalletConnectDisconnect, initSelectedWallet } from './helpers';

const SECONDS_IN_A_DAY = 86400;
const EXPIRATION_PERIOD = 2 * SECONDS_IN_A_DAY; // 2 days

export interface GlobalState {
dAppName: string;
tenantName: TenantName;
Expand Down Expand Up @@ -48,8 +45,7 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
set,
clear,
} = useLocalStorage<string | undefined>({
key: `${storageKeys.ACCOUNT}-${tenantName}`,
expire: EXPIRATION_PERIOD,
key: `${storageKeys.ACCOUNT}`,
});

const clearLocalStorageWallets = () => {
Expand All @@ -71,21 +67,27 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
[set],
);

const accountAddress = walletAccount?.address;
useEffect(() => {
const run = async () => {
const delayWalletInitialization = async (storageAddress: string) => {
// Delay the wallet initialization to allow the wallet extension to be injected, otherwise the call to wallet.enable()
// might fail see https://github.com/TalismanSociety/talisman-connect/issues/25
setTimeout(async () => {
const selectedWallet = await initSelectedWallet(dAppName, tenantName, storageAddress);
if (selectedWallet) setWallet(selectedWallet);
}, 400);
};

const initializeWallet = () => {
if (!storageAddress) {
removeWalletAccount();
return;
}
// skip if tenant already initialized
if (tenantRef.current === tenantName || accountAddress) return;
if (tenantRef.current === tenantName) return;
tenantRef.current = tenantName;
const selectedWallet = await initSelectedWallet(dAppName, tenantName, storageAddress);
if (selectedWallet) setWallet(selectedWallet);
delayWalletInitialization(storageAddress).catch(console.error);
};
run();
}, [storageAddress, removeWalletAccount, dAppName, tenantName, accountAddress]);
initializeWallet();
}, [storageAddress, dAppName, tenantName]);

const providerValue = useMemo<GlobalState>(
() => ({
Expand Down
Loading