Skip to content

Commit

Permalink
fix the disconnect method
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalzieu committed Dec 6, 2024
1 parent a31242d commit 2df37ea
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
// TODO: move out of ConnectViaWallet
import { memo, useState } from "react";
import { memo, useCallback, useEffect, useRef, useState } from "react";
import { ActivityIndicator } from "react-native";
import { ethereum } from "thirdweb/chains";
import {
useDisconnect as useThirdwebDisconnect,
useSetActiveWallet as useSetThirdwebActiveWallet,
Expand Down Expand Up @@ -86,9 +85,24 @@ export const InstalledWalletsTableView = memo(

const { connect: thirdwebConnect } = useThirdwebConnect();
const { disconnect: disconnectThirdweb } = useThirdwebDisconnect();

const thirdwebActiveWallet = useThirdwebActiveWallet();
const thirdwebActiveWalletRef = useRef(thirdwebActiveWallet);
useEffect(() => {
thirdwebActiveWalletRef.current = thirdwebActiveWallet;
}, [thirdwebActiveWallet]);

const setThirdwebActiveWallet = useSetThirdwebActiveWallet();

const disconnectActiveThirdweb = useCallback(async () => {
if (!thirdwebActiveWalletRef.current) return;
disconnectThirdweb(thirdwebActiveWalletRef.current);
// Wait for the disconnect to complete
while (!!thirdwebActiveWalletRef.current) {
await new Promise((r) => setTimeout(r, 100));
}
}, [disconnectThirdweb]);

const [isProcessingWalletId, setIsProcessingWalletId] = useState<
string | null
>(null);
Expand Down Expand Up @@ -125,22 +139,13 @@ export const InstalledWalletsTableView = memo(

setIsProcessingWalletId(wallet.name);

if (thirdwebActiveWallet) {
disconnectThirdweb(thirdwebActiveWallet);
}
await disconnectActiveThirdweb();

try {
let walletAddress: string = "";

// Specific flow for Coinbase Wallet
if (wallet.thirdwebId === "com.coinbase.wallet") {
// @todo => this is a hack to remove the smart wallet key from AsyncStorage
// because it's not being removed by the wallet itself
const storageKeys = await AsyncStorage.getAllKeys();
const wcKeys = storageKeys.filter((k) =>
k.startsWith("-Coinbase Smart Wallet:")
);
await AsyncStorage.multiRemove(wcKeys);
const thirdwebWallet = await thirdwebConnect(async () => {
const coinbaseWallet =
thirdwebWallets[wallet.name as ISupportedWalletName];
Expand Down

1 comment on commit 2df37ea

@technoplato
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

Please sign in to comment.