Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/happy-suns-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Autoconnect previously linked wallets
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function StyledConnectEmbed(

return account ? (
<div className="flex flex-col gap-8">
<StyledConnectButton />
<StyledConnectButton {...props} />
</div>
) : (
<ConnectEmbed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
*/
export function useAddConnectedWallet() {
const manager = useConnectionManagerCtx("useAddConnectedWallet");
return manager.handleConnection;
return manager.addConnectedWallet;

Check warning on line 21 in packages/thirdweb/src/react/core/hooks/wallets/useAddConnectedWallet.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L21 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function useAutoConnectCore(
}

// then connect wallets that were last connected but were not set as active
const otherWallets = wallets.filter(
const otherWallets = availableWallets.filter(
(w) =>
w.id !== lastActiveWalletId && lastConnectedWalletIds.includes(w.id),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js";
import type { AuthArgsType } from "../../../../wallets/in-app/core/authentication/types.js";
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
import { linkProfile } from "../../../../wallets/in-app/web/lib/auth/index.js";
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";

/**
* Links a web2 or web3 profile to the connected in-app or ecosystem account.
Expand Down Expand Up @@ -77,16 +77,25 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
* @wallet
*/
export function useLinkProfile() {
const wallet = useAdminWallet();
const wallets = useConnectedWallets();
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["profiles"],
mutationFn: async (options: Omit<AuthArgsType, "ecosystem">) => {
const ecosystem: Ecosystem | undefined =
wallet && isEcosystemWallet(wallet)
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
: undefined;
mutationFn: async (options: AuthArgsType) => {
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
const ecosystem: Ecosystem | undefined = ecosystemWallet
? {
id: ecosystemWallet.id,
partnerId: ecosystemWallet.getConfig()?.partnerId,
}
: undefined;
const optionsWithEcosystem = { ...options, ecosystem } as AuthArgsType;
return linkProfile(optionsWithEcosystem);
},
onSuccess() {
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ["profiles"] });
}, 500);
},
});
}
25 changes: 17 additions & 8 deletions packages/thirdweb/src/react/native/hooks/wallets/useProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wa
import type { Profile } from "../../../../wallets/in-app/core/authentication/types.js";
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
import { getProfiles } from "../../../../wallets/in-app/web/lib/auth/index.js";
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";

/**
* Retrieves all linked profiles of the connected in-app or ecosystem account.
Expand All @@ -29,15 +29,24 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
export function useProfiles(args: {
client: ThirdwebClient;
}): UseQueryResult<Profile[]> {
const wallet = useAdminWallet();
const wallets = useConnectedWallets();
const enabled =
wallets.length > 0 &&
wallets.some((w) => w.id === "inApp" || isEcosystemWallet(w));
return useQuery({
queryKey: ["profiles", wallet?.id, wallet?.getAccount()?.address],
enabled: !!wallet && (wallet.id === "inApp" || isEcosystemWallet(wallet)),
queryKey: [
"profiles",
wallets.map((w) => `${w.id}-${w.getAccount()?.address}`),
],
enabled,
queryFn: async () => {
const ecosystem: Ecosystem | undefined =
wallet && isEcosystemWallet(wallet)
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
: undefined;
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
const ecosystem: Ecosystem | undefined = ecosystemWallet
? {
id: ecosystemWallet.id,
partnerId: ecosystemWallet.getConfig()?.partnerId,
}
: undefined;
return getProfiles({
client: args.client,
ecosystem,
Expand Down
23 changes: 16 additions & 7 deletions packages/thirdweb/src/react/web/hooks/wallets/useLinkProfile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js";
import type { AuthArgsType } from "../../../../wallets/in-app/core/authentication/types.js";
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
import { linkProfile } from "../../../../wallets/in-app/web/lib/auth/index.js";
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";

/**
* Links a web2 or web3 profile to the connected in-app or ecosystem account.
Expand Down Expand Up @@ -76,16 +76,25 @@ import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
* @wallet
*/
export function useLinkProfile() {
const wallet = useAdminWallet();
const wallets = useConnectedWallets();
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["profiles"],
mutationFn: async (options: AuthArgsType) => {
const ecosystem: Ecosystem | undefined =
wallet && isEcosystemWallet(wallet)
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
: undefined;
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
const ecosystem: Ecosystem | undefined = ecosystemWallet
? {
id: ecosystemWallet.id,
partnerId: ecosystemWallet.getConfig()?.partnerId,
}
: undefined;
const optionsWithEcosystem = { ...options, ecosystem } as AuthArgsType;
return linkProfile(optionsWithEcosystem);
},
onSuccess() {
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ["profiles"] });
}, 500);
},
});
}
25 changes: 17 additions & 8 deletions packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { Profile } from "../../../../wallets/in-app/core/authentication/types.js";
import type { Ecosystem } from "../../../../wallets/in-app/core/wallet/types.js";
import { getProfiles } from "../../../../wallets/in-app/web/lib/auth/index.js";
import { useAdminWallet } from "../../../core/hooks/wallets/useAdminWallet.js";
import { useConnectedWallets } from "../../../core/hooks/wallets/useConnectedWallets.js";

/**
* Retrieves all linked profiles of the connected in-app or ecosystem account.
Expand All @@ -29,15 +29,24 @@
export function useProfiles(args: {
client: ThirdwebClient;
}): UseQueryResult<Profile[]> {
const wallet = useAdminWallet();
const wallets = useConnectedWallets();
const enabled =
wallets.length > 0 &&
wallets.some((w) => w.id === "inApp" || isEcosystemWallet(w));

Check warning on line 35 in packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts#L32-L35

Added lines #L32 - L35 were not covered by tests
return useQuery({
queryKey: ["profiles", wallet?.id, wallet?.getAccount()?.address],
enabled: !!wallet && (wallet.id === "inApp" || isEcosystemWallet(wallet)),
queryKey: [
"profiles",
wallets.map((w) => `${w.id}-${w.getAccount()?.address}`),
],
enabled,

Check warning on line 41 in packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts#L37-L41

Added lines #L37 - L41 were not covered by tests
queryFn: async () => {
const ecosystem: Ecosystem | undefined =
wallet && isEcosystemWallet(wallet)
? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId }
: undefined;
const ecosystemWallet = wallets.find((w) => isEcosystemWallet(w));
const ecosystem: Ecosystem | undefined = ecosystemWallet
? {
id: ecosystemWallet.id,
partnerId: ecosystemWallet.getConfig()?.partnerId,
}
: undefined;

Check warning on line 49 in packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/hooks/wallets/useProfiles.ts#L43-L49

Added lines #L43 - L49 were not covered by tests
return getProfiles({
client: args.client,
ecosystem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
walletConnect={props.walletConnect}
wallet={activeWallet as Wallet<"inApp">}
done={() => {
queryClient.invalidateQueries({ queryKey: ["profiles"] });
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ["profiles"] });
}, 500);

Check warning on line 48 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/LinkProfileScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/LinkProfileScreen.tsx#L46-L48

Added lines #L46 - L48 were not covered by tests
props.onBack();
}}
connectLocale={props.locale}
Expand All @@ -67,7 +69,9 @@
<EcosystemWalletConnectUI
wallet={activeWallet as Wallet<EcosystemWalletId>}
done={() => {
queryClient.invalidateQueries({ queryKey: ["profiles"] });
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ["profiles"] });
}, 500);

Check warning on line 74 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/LinkProfileScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/LinkProfileScreen.tsx#L72-L74

Added lines #L72 - L74 were not covered by tests
props.onBack();
}}
connectLocale={props.locale}
Expand Down
8 changes: 6 additions & 2 deletions packages/thirdweb/src/wallets/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,15 @@ export function createConnectionManager(storage: AsyncStorage) {

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

storage.setItem(CONNECTED_WALLET_IDS, stringify(ids));
storage.setItem(
CONNECTED_WALLET_IDS,
stringify(Array.from(new Set([...prevAccounts, ...ids]))),
);
},
[connectedWallets],
false,
Expand Down
Loading