Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ebma committed Nov 1, 2024
1 parent 61a8baa commit 86903ee
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 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 @@ -49,15 +46,13 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
clear,
} = useLocalStorage<string | undefined>({
key: `${storageKeys.ACCOUNT}`,
expire: undefined,
});

const clearLocalStorageWallets = () => {
storageService.remove(LocalStorageKeys.SELECTED_WALLET_NAME);
};

const removeWalletAccount = useCallback(async () => {
console.log('Removing wallet account', walletAccount);
await handleWalletConnectDisconnect(walletAccount);
clear();
clearLocalStorageWallets();
Expand All @@ -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<GlobalState>(
Expand Down

0 comments on commit 86903ee

Please sign in to comment.