Skip to content

Commit

Permalink
updated connected accounts when switching personal<>smart account
Browse files Browse the repository at this point in the history
  • Loading branch information
MananTank committed Feb 14, 2024
1 parent c934619 commit 263fe76
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/thirdweb/src/react/ui/ConnectWallet/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { useState, useEffect } from "react";
import {
useActiveAccount,
useActiveWalletChainId,
useConnect,
useDisconnect,
useSetActiveAccount,
} from "../../providers/wallet-provider.js";
import { useTWLocale } from "../../providers/locale-provider.js";
import { Modal } from "../components/Modal.js";
Expand Down Expand Up @@ -51,6 +51,7 @@ import {
type WalletWithPersonalAccount,
personalAccountToSmartAccountMap,
} from "../../../wallets/index.js";
import { connectionManager } from "../../connectionManager.js";
// import { walletIds } from "../../../wallets/walletIds.js";

// TEMP
Expand Down Expand Up @@ -719,14 +720,21 @@ function AccountSwitcher({
account: Account;
name: string;
}) {
const setActiveAccount = useSetActiveAccount();
const { connect } = useConnect();
const locale = useTWLocale().connectWallet;
const currentActiveAccount = useActiveAccount();
const removeConnectedAccount = connectionManager.removeConnectedAccount;

return (
<MenuButton
type="button"
onClick={() => {
setActiveAccount(account);
// remove the current active account as "connected"
if (currentActiveAccount) {
removeConnectedAccount(currentActiveAccount);
}
// set as connected and active
connect(account);
}}
style={{
fontSize: fontSize.sm,
Expand Down
8 changes: 8 additions & 0 deletions packages/thirdweb/src/wallets/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export function createConnectionManager(options?: ConnectionManagerOptions) {
walletIdToConnectedAccountMap.setValue(newValue);
};

const removeConnectedAccount = (account: Account) => {
const oldValue = walletIdToConnectedAccountMap.getValue();
const newValue = new Map(oldValue);
newValue.delete(account.wallet.metadata.id);
walletIdToConnectedAccountMap.setValue(newValue);
};

const onAccountDisconnect = (account: Account) => {
const currentMap = walletIdToConnectedAccountMap.getValue();
const newMap = new Map(currentMap);
Expand Down Expand Up @@ -210,5 +217,6 @@ export function createConnectionManager(options?: ConnectionManagerOptions) {
switchActiveWalletChain,
activeWalletConnectionStatus: activeWalletConnectionStatus,
isAutoConnecting,
removeConnectedAccount,
};
}

0 comments on commit 263fe76

Please sign in to comment.