From 86903eea8d3b18757eb6a359f0f4c464c5914425 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Fri, 1 Nov 2024 17:47:59 +0100 Subject: [PATCH] Address comments --- src/GlobalStateProvider/index.tsx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/GlobalStateProvider/index.tsx b/src/GlobalStateProvider/index.tsx index 1a3373ff..d7b0d735 100644 --- a/src/GlobalStateProvider/index.tsx +++ b/src/GlobalStateProvider/index.tsx @@ -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; @@ -49,7 +46,6 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => { clear, } = useLocalStorage({ key: `${storageKeys.ACCOUNT}`, - expire: undefined, }); const clearLocalStorageWallets = () => { @@ -57,7 +53,6 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => { }; const removeWalletAccount = useCallback(async () => { - console.log('Removing wallet account', walletAccount); await handleWalletConnectDisconnect(walletAccount); clear(); clearLocalStorageWallets(); @@ -73,20 +68,25 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => { ); 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) { return; } // skip if tenant already initialized if (tenantRef.current === tenantName) return; tenantRef.current = tenantName; - // Delay this to allow the wallet extension to be injected, otherwise the call to wallet.enable() might fail - setTimeout(async () => { - const selectedWallet = await initSelectedWallet(dAppName, tenantName, storageAddress); - if (selectedWallet) setWallet(selectedWallet); - }, 400); + delayWalletInitialization(storageAddress).catch(console.error); }; - run(); + initializeWallet(); }, [storageAddress, dAppName, tenantName]); const providerValue = useMemo(