1- import "./App.css" ;
1+ import "./styles/ App.css" ;
22
33import { Porto } from "porto" ;
44import { useEffect , useMemo , useState } from "react" ;
55import { type Address , type Chain , createWalletClient , custom } from "viem" ;
66import { getAddresses , requestAddresses } from "viem/actions" ;
7- import { applyChainId } from "./helpers.ts" ;
8- import type { AnnounceEvent , EIP1193 , EIP6963ProviderInfo } from "./types.ts" ;
7+ import { applyChainId } from "./utils/ helpers.ts" ;
8+ import type { EIP1193 , EIP6963AnnounceProviderEvent , EIP6963ProviderInfo } from "./utils /types.ts" ;
99
1010declare global {
1111 interface Window {
@@ -25,29 +25,23 @@ export function App() {
2525 ) ;
2626
2727 useEffect ( ( ) => {
28- const onAnnounce = ( e : Event ) => {
29- const ev = e as AnnounceEvent ;
28+ const onAnnounce = ( ev : EIP6963AnnounceProviderEvent ) => {
3029 const { info, provider } = ev . detail ;
30+
3131 setProviders ( ( prev ) =>
3232 prev . some ( ( p ) => p . info . uuid === info . uuid ) ? prev : [ ...prev , { info, provider } ] ,
3333 ) ;
3434 } ;
3535
36- window . addEventListener ( "eip6963:announceProvider" , onAnnounce as EventListener ) ;
36+ window . addEventListener ( "eip6963:announceProvider" , onAnnounce ) ;
3737 window . dispatchEvent ( new Event ( "eip6963:requestProvider" ) ) ;
3838
3939 return ( ) => {
40- window . removeEventListener ( "eip6963:announceProvider" , onAnnounce as EventListener ) ;
40+ window . removeEventListener ( "eip6963:announceProvider" , onAnnounce ) ;
4141 } ;
4242 } , [ ] ) ;
4343
4444 const [ selectedUuid , setSelectedUuid ] = useState < string | null > ( null ) ;
45- useEffect ( ( ) => {
46- if ( providers . length === 1 && ! selectedUuid ) {
47- setSelectedUuid ( providers [ 0 ] . info . uuid ) ;
48- }
49- } , [ providers , selectedUuid ] ) ;
50-
5145 const selected = providers . find ( ( p ) => p . info . uuid === selectedUuid ) ?? null ;
5246
5347 const [ account , setAccount ] = useState < Address > ( ) ;
@@ -58,18 +52,17 @@ export function App() {
5852 ( ) =>
5953 selected
6054 ? createWalletClient ( {
61- chain,
6255 transport : custom ( selected . provider ) ,
6356 } )
6457 : undefined ,
65- [ selected , chain ] ,
58+ [ selected ] ,
6659 ) ;
6760
6861 useEffect ( ( ) => {
6962 if ( ! selected ) return ;
7063
7164 const onAccountsChanged = ( accounts : readonly string [ ] ) =>
72- setAccount ( accounts ?. [ 0 ] as Address | undefined ) ;
65+ setAccount ( ( accounts [ 0 ] as Address ) ?? undefined ) ;
7366
7467 const onChainChanged = ( raw : unknown ) => {
7568 applyChainId ( raw , setChainId , setChain ) ;
@@ -89,7 +82,9 @@ export function App() {
8982 if ( ! selected ) return ;
9083
9184 try {
92- const raw = await selected . provider . request ( { method : "eth_chainId" } ) ;
85+ const raw = await selected . provider . request < string > ( {
86+ method : "eth_chainId" ,
87+ } ) ;
9388 applyChainId ( raw , setChainId , setChain ) ;
9489 } catch {
9590 setChainId ( undefined ) ;
@@ -113,27 +108,16 @@ export function App() {
113108 setAccount ( addrs [ 0 ] as Address | undefined ) ;
114109
115110 try {
116- const raw = await selected . provider . request ( { method : "eth_chainId" } ) ;
111+ const raw = await selected . provider . request < string > ( {
112+ method : "eth_chainId" ,
113+ } ) ;
117114 applyChainId ( raw , setChainId , setChain ) ;
118115 } catch {
119116 setChainId ( undefined ) ;
120117 setChain ( undefined ) ;
121118 }
122119 } ;
123120
124- const disconnect = async ( ) => {
125- setAccount ( undefined ) ;
126- setChainId ( undefined ) ;
127- setChain ( undefined ) ;
128-
129- try {
130- await walletClient ?. transport . request ( {
131- method : "wallet_revokePermissions" ,
132- params : [ { eth_accounts : { } } ] ,
133- } ) ;
134- } catch { }
135- } ;
136-
137121 return (
138122 < div className = "container" >
139123 < h1 className = "title" > Foundry</ h1 >
@@ -168,19 +152,17 @@ rpc: ${chain?.rpcUrls?.default?.http?.[0] ?? chain?.rpcUrls?.public?.http?.[0
168152 </ pre >
169153 ) }
170154
171- { selected &&
172- ( account ? (
173- < >
174- < div className = "output" > Connected: { account } </ div >
175- < button type = "button" className = "disconnect" onClick = { disconnect } >
176- Disconnect
177- </ button >
178- </ >
155+ { selected ? (
156+ account ? (
157+ < div className = "output" > Connected: { account } </ div >
179158 ) : (
180- < button type = "button" onClick = { connect } >
159+ < button type = "button" className = "connect" onClick = { connect } >
181160 Connect Wallet
182161 </ button >
183- ) ) }
162+ )
163+ ) : (
164+ < p > Please select a wallet</ p >
165+ ) }
184166 </ div >
185167 ) ;
186168}
0 commit comments