Skip to content

Commit c1157d3

Browse files
author
dschlabach
committed
teset build
1 parent a56be11 commit c1157d3

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

playground/nextjs-app-router/components/form/wallet-type.tsx

+23-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
33
import { getSlicedAddress } from '@/lib/utils';
44
import { useEffect, useState } from 'react';
55
import { useAccount, useConnect, useConnectors, useDisconnect } from 'wagmi';
6+
import type { GetConnectorsReturnType } from 'wagmi/actions';
67

78
export enum WalletPreference {
89
SMART_WALLET = 'smartWalletOnly',
910
EOA = 'eoaOnly',
1011
}
1112

13+
function getConnector(
14+
walletType: WalletPreference,
15+
connectors: GetConnectorsReturnType,
16+
) {
17+
if (walletType === WalletPreference.SMART_WALLET) {
18+
return connectors[0];
19+
}
20+
return connectors[1];
21+
}
22+
1223
export function WalletType() {
1324
// const { walletType, setWalletType, clearWalletType } = useContext(AppContext);
1425
const { disconnectAsync } = useDisconnect();
@@ -18,18 +29,17 @@ export function WalletType() {
1829

1930
const [walletType, setWalletType] = useState<WalletPreference>();
2031

21-
// Connect to wallet if walletType changes
32+
// Set localStorage ONLY when user has connected
33+
// otherwise, could result in walletType being set to smart wallet when user intended to connect eoa wallet
2234
useEffect(() => {
23-
if (walletType === WalletPreference.SMART_WALLET) {
24-
connect({ connector: connectors[0] });
25-
} else if (walletType === WalletPreference.EOA) {
26-
connect({ connector: connectors[1] });
35+
if (walletType && account.address) {
36+
localStorage.setItem('walletType', walletType);
2737
}
28-
}, [connect, connectors, walletType]);
38+
}, [walletType, account.address]);
2939

3040
async function clearWalletType() {
3141
localStorage.removeItem('walletType');
32-
setWalletType?.(undefined);
42+
setWalletType(undefined);
3343
}
3444

3545
async function disconnectAll() {
@@ -45,7 +55,12 @@ export function WalletType() {
4555
id="wallet-type"
4656
value={walletType}
4757
className="flex items-center justify-between"
48-
onValueChange={(value) => setWalletType?.(value as WalletPreference)}
58+
onValueChange={(value) => {
59+
setWalletType(value as WalletPreference);
60+
connect({
61+
connector: getConnector(value as WalletPreference, connectors),
62+
});
63+
}}
4964
>
5065
<div className="flex items-center gap-2">
5166
<Label

0 commit comments

Comments
 (0)