11'use client'
22
33import { useEffect , useMemo , useState } from 'react'
4- import { useAccount } from 'wagmi'
5- import { useConnect } from 'wagmi'
6- import { useDisconnect } from 'wagmi'
7- import { config } from '../lib/wagmi'
8-
4+ import { useAccount , useConnect , useDisconnect , useSwitchChain } from 'wagmi'
5+ import { config , baseSepolia } from '../lib/wagmi'
6+ import { useToast } from './ToastProvider'
97
108export function WalletComponent ( ) {
11- const { isConnected, address } = useAccount ( )
9+ const { isConnected, address, connector } = useAccount ( )
10+ const [ walletChainId , setWalletChainId ] = useState < number | undefined > ( )
1211 const { connect, connectors } = useConnect ( { config } )
12+ const { switchChainAsync } = useSwitchChain ( )
1313 const { disconnect } = useDisconnect ( )
14+ const { showToast } = useToast ( )
1415 const [ selectedConnectorId , setSelectedConnectorId ] = useState < string > ( )
1516
17+ useEffect ( ( ) => {
18+ if ( ! connector ) {
19+ setWalletChainId ( undefined )
20+ return
21+ }
22+
23+ let cancelled = false
24+
25+ const refresh = async ( ) => {
26+ try {
27+ const id = await connector . getChainId ( )
28+ if ( ! cancelled ) setWalletChainId ( id )
29+ } catch {
30+ if ( ! cancelled ) setWalletChainId ( undefined )
31+ }
32+ }
33+
34+ void refresh ( )
35+
36+ let provider : { on ?: ( event : string , handler : ( ) => void ) => void ; removeListener ?: ( event : string , handler : ( ) => void ) => void } | undefined
37+ const onChainChanged = ( ) => { void refresh ( ) }
38+
39+ void connector . getProvider ( ) . then ( ( p ) => {
40+ provider = p as typeof provider
41+ provider ?. on ?.( 'chainChanged' , onChainChanged )
42+ } )
43+
44+ return ( ) => {
45+ cancelled = true
46+ provider ?. removeListener ?.( 'chainChanged' , onChainChanged )
47+ }
48+ } , [ connector ] )
49+
50+ // Base Account often stays on mainnet (8453) after connect; nudge to Sepolia.
51+ useEffect ( ( ) => {
52+ if ( ! isConnected || walletChainId === undefined || walletChainId === baseSepolia . id ) return
53+ void switchChainAsync ( { chainId : baseSepolia . id } ) . catch ( ( ) => { } )
54+ } , [ isConnected , walletChainId , switchChainAsync ] )
55+
1656 useEffect ( ( ) => {
1757 if ( selectedConnectorId || connectors . length === 0 ) return
1858
@@ -31,8 +71,32 @@ export function WalletComponent() {
3171 )
3272
3373 if ( isConnected && address ) {
74+ const copyAddress = async ( event : React . MouseEvent ) => {
75+ event . stopPropagation ( )
76+ try {
77+ await navigator . clipboard . writeText ( address )
78+ showToast ( 'Address copied' , 'success' )
79+ } catch {
80+ showToast ( 'Could not copy address' , 'error' )
81+ }
82+ }
83+
3484 return (
35- < div >
85+ < div style = { { display : 'flex' , gap : '0.5rem' , alignItems : 'center' } } >
86+ < div
87+ style = { {
88+ padding : '0.5rem 0.75rem' ,
89+ border : '1px solid #d1d5db' ,
90+ borderRadius : '8px' ,
91+ background : 'white' ,
92+ color : '#374151' ,
93+ fontSize : '0.875rem' ,
94+ fontWeight : '500' ,
95+ fontFamily : 'monospace' ,
96+ } }
97+ >
98+ Chain { walletChainId ?? '…' }
99+ </ div >
36100 < div
37101 onClick = { ( ) => disconnect ( ) }
38102 style = { {
@@ -44,11 +108,27 @@ export function WalletComponent() {
44108 fontSize : '0.875rem' ,
45109 fontWeight : '500' ,
46110 fontFamily : 'monospace' ,
47- cursor : 'pointer'
111+ cursor : 'pointer' ,
48112 } }
49113 >
50114 { address . slice ( 0 , 6 ) } ...{ address . slice ( - 4 ) }
51115 </ div >
116+ < button
117+ type = "button"
118+ onClick = { copyAddress }
119+ style = { {
120+ padding : '0.5rem 0.75rem' ,
121+ border : '1px solid #d1d5db' ,
122+ borderRadius : '8px' ,
123+ background : 'white' ,
124+ color : '#374151' ,
125+ fontSize : '0.875rem' ,
126+ fontWeight : '500' ,
127+ cursor : 'pointer' ,
128+ } }
129+ >
130+ Copy
131+ </ button >
52132 </ div >
53133 )
54134 }
@@ -90,7 +170,10 @@ export function WalletComponent() {
90170 </ select >
91171 < button
92172 type = "button"
93- onClick = { ( ) => selectedConnector && connect ( { connector : selectedConnector } ) }
173+ onClick = { ( ) =>
174+ selectedConnector &&
175+ connect ( { connector : selectedConnector , chainId : baseSepolia . id } )
176+ }
94177 disabled = { ! selectedConnector }
95178 style = { {
96179 padding : '0.5rem 1rem' ,
0 commit comments