@@ -3,12 +3,23 @@ import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
3
3
import { getSlicedAddress } from '@/lib/utils' ;
4
4
import { useEffect , useState } from 'react' ;
5
5
import { useAccount , useConnect , useConnectors , useDisconnect } from 'wagmi' ;
6
+ import type { GetConnectorsReturnType } from 'wagmi/actions' ;
6
7
7
8
export enum WalletPreference {
8
9
SMART_WALLET = 'smartWalletOnly' ,
9
10
EOA = 'eoaOnly' ,
10
11
}
11
12
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
+
12
23
export function WalletType ( ) {
13
24
// const { walletType, setWalletType, clearWalletType } = useContext(AppContext);
14
25
const { disconnectAsync } = useDisconnect ( ) ;
@@ -18,18 +29,17 @@ export function WalletType() {
18
29
19
30
const [ walletType , setWalletType ] = useState < WalletPreference > ( ) ;
20
31
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
22
34
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 ) ;
27
37
}
28
- } , [ connect , connectors , walletType ] ) ;
38
+ } , [ walletType , account . address ] ) ;
29
39
30
40
async function clearWalletType ( ) {
31
41
localStorage . removeItem ( 'walletType' ) ;
32
- setWalletType ?. ( undefined ) ;
42
+ setWalletType ( undefined ) ;
33
43
}
34
44
35
45
async function disconnectAll ( ) {
@@ -45,7 +55,12 @@ export function WalletType() {
45
55
id = "wallet-type"
46
56
value = { walletType }
47
57
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
+ } }
49
64
>
50
65
< div className = "flex items-center gap-2" >
51
66
< Label
0 commit comments