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 15 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
18 changes: 10 additions & 8 deletions src/GlobalStateProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
set,
clear,
} = useLocalStorage<string | undefined>({
key: `${storageKeys.ACCOUNT}-${tenantName}`,
expire: EXPIRATION_PERIOD,
key: `${storageKeys.ACCOUNT}`,
expire: undefined,
Sharqiewicz marked this conversation as resolved.
Show resolved Hide resolved
});

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

const removeWalletAccount = useCallback(async () => {
console.log('Removing wallet account', walletAccount);
Sharqiewicz marked this conversation as resolved.
Show resolved Hide resolved
await handleWalletConnectDisconnect(walletAccount);
clear();
clearLocalStorageWallets();
Expand All @@ -71,21 +72,22 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
[set],
);

const accountAddress = walletAccount?.address;
useEffect(() => {
const run = async () => {
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);
// Delay this to allow the wallet extension to be injected, otherwise the call to wallet.enable() might fail
setTimeout(async () => {
Sharqiewicz marked this conversation as resolved.
Show resolved Hide resolved
const selectedWallet = await initSelectedWallet(dAppName, tenantName, storageAddress);
if (selectedWallet) setWallet(selectedWallet);
}, 400);
};
run();
}, [storageAddress, removeWalletAccount, dAppName, tenantName, accountAddress]);
}, [storageAddress, dAppName, tenantName]);

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