Skip to content

Commit 8b606b5

Browse files
handle disconnect when multiple connected wallets + wallet utility exports (#3151)
1 parent 3b03cd0 commit 8b606b5

File tree

8 files changed

+35
-12
lines changed

8 files changed

+35
-12
lines changed

.changeset/rich-ties-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Better handling for multiple accounts connected

packages/thirdweb/src/exports/react-native.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ export {
8787
type AutoConnectProps,
8888
} from "../react/core/hooks/connection/AutoConnect.js";
8989
export { useAutoConnect } from "../react/core/hooks/connection/useAutoConnect.js";
90+
91+
// wallet info
92+
export {
93+
useWalletInfo,
94+
useWalletImage,
95+
} from "../react/web/ui/hooks/useWalletInfo.js";

packages/thirdweb/src/exports/react.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ export {
129129
useConnectModal,
130130
type UseConnectModalOptions,
131131
} from "../react/web/ui/ConnectWallet/useConnectModal.js";
132+
133+
// wallet info
134+
export {
135+
useWalletInfo,
136+
useWalletImage,
137+
} from "../react/web/ui/hooks/useWalletInfo.js";

packages/thirdweb/src/exports/wallets.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ export type {
9696

9797
export { getAllWalletsList } from "../wallets/getAllWalletsList.js";
9898
export { getWalletInfo } from "../wallets/__generated__/getWalletInfo.js";
99+
export { type WalletInfo } from "../wallets/wallet-info.js";
99100

100101
export { createWalletAdapter } from "../adapters/wallet-adapter.js";

packages/thirdweb/src/exports/wallets/in-app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export {
99
} from "../../wallets/in-app/core/authentication/index.js";
1010

1111
export { hasStoredPasskey } from "../../wallets/in-app/web/lib/auth/passkeys.js";
12+
13+
export { socialIcons } from "../../react/web/wallets/in-app/socialIcons.js";

packages/thirdweb/src/react/web/ui/hooks/useWalletInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { WalletInfo } from "../../../../wallets/wallet-info.js";
44
import type { WalletId } from "../../../../wallets/wallet-types.js";
55

66
/**
7-
* @internal
7+
* Returns the wallet info for the provided wallet id.
88
*/
99
export function useWalletInfo(id: WalletId) {
1010
return useQuery<WalletInfo>({
@@ -17,7 +17,7 @@ export function useWalletInfo(id: WalletId) {
1717
}
1818

1919
/**
20-
* @internal
20+
* Returns the wallet image for the provided wallet id.
2121
*/
2222
export function useWalletImage(id: WalletId) {
2323
return useQuery({

packages/thirdweb/src/wallets/manager/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ export function createConnectionManager(storage: AsyncStorage) {
7373
};
7474

7575
const onWalletDisconnect = (wallet: Wallet) => {
76-
const currentMap = walletIdToConnectedWalletMap.getValue();
7776
deleteConnectParamsFromStorage(storage, wallet.id);
77+
removeConnectedWallet(wallet);
7878

79-
const newMap = new Map(currentMap);
80-
newMap.delete(wallet.id);
81-
82-
walletIdToConnectedWalletMap.setValue(newMap);
79+
// there are still connected wallets, switch to the next one
80+
if (connectedWallets.getValue().length > 0) {
81+
const nextWallet = connectedWallets.getValue()[0] as Wallet;
82+
setActiveWallet(nextWallet);
83+
return;
84+
}
8385

84-
// if disconnecting the active wallet
86+
// if disconnecting the last wallet
8587
if (activeWalletStore.getValue() === wallet) {
8688
storage.removeItem(LAST_ACTIVE_EOA_ID);
8789
activeAccountStore.setValue(undefined);

packages/thirdweb/src/wallets/wallet-connect/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,18 @@ async function initProvider(
280280
if (walletId !== "walletConnect") {
281281
function handleSessionRequest() {
282282
const preferNative =
283-
walletInfo.mobile.native || walletInfo.mobile.universal;
283+
provider.session?.peer?.metadata?.redirect?.native ||
284+
walletInfo.mobile.native ||
285+
walletInfo.mobile.universal;
284286

285287
if (isReactNative()) {
286288
const { Linking } = require("react-native");
287-
const appUrl = preferNative;
288-
if (!appUrl) {
289+
if (!preferNative) {
289290
throw new Error(
290291
"No app url found for wallet connect to redirect to.",
291292
);
292293
}
293-
Linking.openURL(appUrl);
294+
Linking.openURL(preferNative);
294295
return;
295296
}
296297

0 commit comments

Comments
 (0)