Skip to content

Commit da5d8ec

Browse files
fix: Autoconnect previously linked wallets (#5688)
1 parent 8020bdb commit da5d8ec

File tree

10 files changed

+87
-38
lines changed

10 files changed

+87
-38
lines changed

.changeset/happy-suns-nail.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+
Autoconnect previously linked wallets

apps/playground-web/src/components/styled-connect-embed.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function StyledConnectEmbed(
2525

2626
return account ? (
2727
<div className="flex flex-col gap-8">
28-
<StyledConnectButton />
28+
<StyledConnectButton {...props} />
2929
</div>
3030
) : (
3131
<ConnectEmbed

packages/thirdweb/src/react/core/hooks/wallets/useAddConnectedWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ import { useConnectionManagerCtx } from "../../providers/connection-manager.js";
1818
*/
1919
export function useAddConnectedWallet() {
2020
const manager = useConnectionManagerCtx("useAddConnectedWallet");
21-
return manager.handleConnection;
21+
return manager.addConnectedWallet;
2222
}

packages/thirdweb/src/react/core/hooks/wallets/useAutoConnect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function useAutoConnectCore(
131131
}
132132

133133
// then connect wallets that were last connected but were not set as active
134-
const otherWallets = wallets.filter(
134+
const otherWallets = availableWallets.filter(
135135
(w) =>
136136
w.id !== lastActiveWalletId && lastConnectedWalletIds.includes(w.id),
137137
);

packages/thirdweb/src/react/native/hooks/wallets/useLinkProfile.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useMutation } from "@tanstack/react-query";
1+
import { useMutation, useQueryClient } from "@tanstack/react-query";
22
import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js";
33
import type { AuthArgsType } from "../../../../wallets/in-app/core/authentication/types.js";
44
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
55
import { linkProfile } from "../../../../wallets/in-app/web/lib/auth/index.js";
6-
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
6+
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";
77

88
/**
99
* Links a web2 or web3 profile to the connected in-app or ecosystem account.
@@ -77,16 +77,25 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
7777
* @wallet
7878
*/
7979
export function useLinkProfile() {
80-
const wallet = useAdminWallet();
80+
const wallets = useConnectedWallets();
81+
const queryClient = useQueryClient();
8182
return useMutation({
8283
mutationKey: ["profiles"],
83-
mutationFn: async (options: Omit<AuthArgsType, "ecosystem">) => {
84-
const ecosystem: Ecosystem | undefined =
85-
wallet && isEcosystemWallet(wallet)
86-
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
87-
: undefined;
84+
mutationFn: async (options: AuthArgsType) => {
85+
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
86+
const ecosystem: Ecosystem | undefined = ecosystemWallet
87+
? {
88+
id: ecosystemWallet.id,
89+
partnerId: ecosystemWallet.getConfig()?.partnerId,
90+
}
91+
: undefined;
8892
const optionsWithEcosystem = { ...options, ecosystem } as AuthArgsType;
8993
return linkProfile(optionsWithEcosystem);
9094
},
95+
onSuccess() {
96+
setTimeout(() => {
97+
queryClient.invalidateQueries({ queryKey: ["profiles"] });
98+
}, 500);
99+
},
91100
});
92101
}

packages/thirdweb/src/react/native/hooks/wallets/useProfiles.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wa
44
import type { Profile } from "../../../../wallets/in-app/core/authentication/types.js";
55
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
66
import { getProfiles } from "../../../../wallets/in-app/web/lib/auth/index.js";
7-
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
7+
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";
88

99
/**
1010
* Retrieves all linked profiles of the connected in-app or ecosystem account.
@@ -29,15 +29,24 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
2929
export function useProfiles(args: {
3030
client: ThirdwebClient;
3131
}): UseQueryResult<Profile[]> {
32-
const wallet = useAdminWallet();
32+
const wallets = useConnectedWallets();
33+
const enabled =
34+
wallets.length > 0 &&
35+
wallets.some((w) => w.id === "inApp" || isEcosystemWallet(w));
3336
return useQuery({
34-
queryKey: ["profiles", wallet?.id, wallet?.getAccount()?.address],
35-
enabled: !!wallet && (wallet.id === "inApp" || isEcosystemWallet(wallet)),
37+
queryKey: [
38+
"profiles",
39+
wallets.map((w) => `${w.id}-${w.getAccount()?.address}`),
40+
],
41+
enabled,
3642
queryFn: async () => {
37-
const ecosystem: Ecosystem | undefined =
38-
wallet && isEcosystemWallet(wallet)
39-
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
40-
: undefined;
43+
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
44+
const ecosystem: Ecosystem | undefined = ecosystemWallet
45+
? {
46+
id: ecosystemWallet.id,
47+
partnerId: ecosystemWallet.getConfig()?.partnerId,
48+
}
49+
: undefined;
4150
return getProfiles({
4251
client: args.client,
4352
ecosystem,

packages/thirdweb/src/react/web/hooks/wallets/useLinkProfile.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useMutation } from "@tanstack/react-query";
1+
import { useMutation, useQueryClient } from "@tanstack/react-query";
22
import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js";
33
import type { AuthArgsType } from "../../../../wallets/in-app/core/authentication/types.js";
44
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
55
import { linkProfile } from "../../../../wallets/in-app/web/lib/auth/index.js";
6-
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
6+
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";
77

88
/**
99
* Links a web2 or web3 profile to the connected in-app or ecosystem account.
@@ -76,16 +76,25 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
7676
* @wallet
7777
*/
7878
export function useLinkProfile() {
79-
const wallet = useAdminWallet();
79+
const wallets = useConnectedWallets();
80+
const queryClient = useQueryClient();
8081
return useMutation({
8182
mutationKey: ["profiles"],
8283
mutationFn: async (options: AuthArgsType) => {
83-
const ecosystem: Ecosystem | undefined =
84-
wallet && isEcosystemWallet(wallet)
85-
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
86-
: undefined;
84+
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
85+
const ecosystem: Ecosystem | undefined = ecosystemWallet
86+
? {
87+
id: ecosystemWallet.id,
88+
partnerId: ecosystemWallet.getConfig()?.partnerId,
89+
}
90+
: undefined;
8791
const optionsWithEcosystem = { ...options, ecosystem } as AuthArgsType;
8892
return linkProfile(optionsWithEcosystem);
8993
},
94+
onSuccess() {
95+
setTimeout(() => {
96+
queryClient.invalidateQueries({ queryKey: ["profiles"] });
97+
}, 500);
98+
},
9099
});
91100
}

packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wa
44
import type { Profile } from "../../../../wallets/in-app/core/authentication/types.js";
55
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
66
import { getProfiles } from "../../../../wallets/in-app/web/lib/auth/index.js";
7-
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
7+
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";
88

99
/**
1010
* Retrieves all linked profiles of the connected in-app or ecosystem account.
@@ -29,15 +29,24 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
2929
export function useProfiles(args: {
3030
client: ThirdwebClient;
3131
}): UseQueryResult<Profile[]> {
32-
const wallet = useAdminWallet();
32+
const wallets = useConnectedWallets();
33+
const enabled =
34+
wallets.length > 0 &&
35+
wallets.some((w) => w.id === "inApp" || isEcosystemWallet(w));
3336
return useQuery({
34-
queryKey: ["profiles", wallet?.id, wallet?.getAccount()?.address],
35-
enabled: !!wallet && (wallet.id === "inApp" || isEcosystemWallet(wallet)),
37+
queryKey: [
38+
"profiles",
39+
wallets.map((w) => `${w.id}-${w.getAccount()?.address}`),
40+
],
41+
enabled,
3642
queryFn: async () => {
37-
const ecosystem: Ecosystem | undefined =
38-
wallet && isEcosystemWallet(wallet)
39-
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
40-
: undefined;
43+
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
44+
const ecosystem: Ecosystem | undefined = ecosystemWallet
45+
? {
46+
id: ecosystemWallet.id,
47+
partnerId: ecosystemWallet.getConfig()?.partnerId,
48+
}
49+
: undefined;
4150
return getProfiles({
4251
client: args.client,
4352
ecosystem,

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/LinkProfileScreen.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export function LinkProfileScreen(props: {
4343
walletConnect={props.walletConnect}
4444
wallet={activeWallet as Wallet<"inApp">}
4545
done={() => {
46-
queryClient.invalidateQueries({ queryKey: ["profiles"] });
46+
setTimeout(() => {
47+
queryClient.invalidateQueries({ queryKey: ["profiles"] });
48+
}, 500);
4749
props.onBack();
4850
}}
4951
connectLocale={props.locale}
@@ -67,7 +69,9 @@ export function LinkProfileScreen(props: {
6769
<EcosystemWalletConnectUI
6870
wallet={activeWallet as Wallet<EcosystemWalletId>}
6971
done={() => {
70-
queryClient.invalidateQueries({ queryKey: ["profiles"] });
72+
setTimeout(() => {
73+
queryClient.invalidateQueries({ queryKey: ["profiles"] });
74+
}, 500);
7175
props.onBack();
7276
}}
7377
connectLocale={props.locale}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,15 @@ export function createConnectionManager(storage: AsyncStorage) {
230230

231231
// save last connected wallet ids to storage
232232
effect(
233-
() => {
233+
async () => {
234+
const prevAccounts = (await getStoredConnectedWalletIds(storage)) || [];
234235
const accounts = connectedWallets.getValue();
235236
const ids = accounts.map((acc) => acc?.id).filter((c) => !!c) as string[];
236237

237-
storage.setItem(CONNECTED_WALLET_IDS, stringify(ids));
238+
storage.setItem(
239+
CONNECTED_WALLET_IDS,
240+
stringify(Array.from(new Set([...prevAccounts, ...ids]))),
241+
);
238242
},
239243
[connectedWallets],
240244
false,

0 commit comments

Comments
 (0)