11'use client' ;
22
3- import { useConnect , useAccount } from 'wagmi ' ;
4- import { cronosTestnet } from 'wagmi/chains ' ;
3+ import { useState , useEffect } from 'react ' ;
4+ import { useConnect , useAccount , useSwitchChain } from 'wagmi' ;
55import { Button } from '@/components/ui/button' ;
6- import { Wallet } from 'lucide-react' ;
6+ import { Wallet , Loader2 , AlertCircle } from 'lucide-react' ;
77import { useRouter } from 'next/navigation' ;
88import { useAuthStore } from '@/store/useAuthStore' ;
9- import { useEffect } from 'react' ;
109
1110export function WalletConnect ( ) {
12- const { connectors, connect } = useConnect ( ) ;
13- const { address, isConnected } = useAccount ( ) ;
11+ const { connectors, connect, isPending, error, variables } = useConnect ( ) ;
12+ const { address, isConnected, chain } = useAccount ( ) ;
13+ const { chains, switchChain } = useSwitchChain ( ) ;
1414 const router = useRouter ( ) ;
1515 const setAuth = useAuthStore ( ( state ) => state . setAuth ) ;
1616
17+ // Prevent hydration mismatch errors in Next.js
18+ const [ mounted , setMounted ] = useState ( false ) ;
19+ useEffect ( ( ) => {
20+ const timer = setTimeout ( ( ) => setMounted ( true ) , 0 ) ;
21+ return ( ) => clearTimeout ( timer ) ;
22+ } , [ ] ) ;
23+
24+ // Preserve the original auth & redirect logic!
1725 useEffect ( ( ) => {
1826 if ( isConnected && address ) {
1927 setAuth ( {
@@ -24,20 +32,70 @@ export function WalletConnect() {
2432 }
2533 } , [ isConnected , address , setAuth , router ] ) ;
2634
35+ if ( ! mounted ) return null ;
36+
2737 return (
28- < div className = "space-y-3 mt-6" >
29- { connectors . map ( ( connector ) => (
30- < Button
31- key = { connector . id }
32- variant = "outline"
33- className = "w-full justify-start gap-3 h-12"
34- onClick = { ( ) => connect ( { connector, chainId : cronosTestnet . id } ) }
35- >
36- < Wallet className = "h-5 w-5" />
37- Connect with { connector . name }
38- </ Button >
39- ) ) }
38+ < div className = "space-y-4 w-full" >
39+ { /* 1. Connection Buttons with Progress States */ }
40+ < div className = "space-y-3" >
41+ { connectors . map ( ( connector ) => {
42+ // Track exactly which button was clicked for the loading spinner
43+ // FIX: Changed .uid to .name to bypass TypeScript errors
44+ const isThisLoading = isPending && variables ?. connector ?. name === connector . name ;
45+
46+ return (
47+ < Button
48+ // FIX: Changed .uid to .name to bypass TypeScript errors
49+ key = { connector . name }
50+ variant = "outline"
51+ className = "w-full justify-start gap-3 h-12 relative transition-all"
52+ onClick = { ( ) => connect ( { connector } ) }
53+ disabled = { isPending }
54+ >
55+ { isThisLoading ? (
56+ < Loader2 className = "h-5 w-5 animate-spin text-gray-500" />
57+ ) : (
58+ < Wallet className = "h-5 w-5 text-gray-700" />
59+ ) }
60+ < span className = "font-medium" >
61+ { isThisLoading ? 'Connecting...' : `Connect with ${ connector . name } ` }
62+ </ span >
63+ </ Button >
64+ ) ;
65+ } ) }
66+ </ div >
67+
68+ { /* 2. Graceful Error Handling */ }
69+ { error && (
70+ < div className = "p-3 text-sm text-red-600 bg-red-50 rounded-md border border-red-100 flex items-start gap-2 animate-in fade-in slide-in-from-top-2" >
71+ < AlertCircle className = "w-4 h-4 flex-shrink-0 mt-0.5" />
72+ < p className = "leading-tight" >
73+ { error . message . split ( '.' ) [ 0 ] || 'Failed to connect wallet. Please try again.' }
74+ </ p >
75+ </ div >
76+ ) }
77+
78+ { /* 3. Network Switching Support */ }
79+ { /* This will show up if the user cancels a signature or before the redirect fires */ }
80+ { switchChain && (
81+ < div className = "pt-2" >
82+ < p className = "text-xs text-gray-500 mb-2 font-medium" > Available Networks</ p >
83+ < div className = "flex flex-wrap gap-2" >
84+ { chains . map ( ( x ) => (
85+ < Button
86+ key = { x . id }
87+ variant = { x . id === chain ?. id ? "default" : "outline" }
88+ size = "sm"
89+ onClick = { ( ) => switchChain ( { chainId : x . id } ) }
90+ disabled = { x . id === chain ?. id }
91+ className = "flex-1 text-xs h-8"
92+ >
93+ { x . name }
94+ </ Button >
95+ ) ) }
96+ </ div >
97+ </ div >
98+ ) }
4099 </ div >
41100 ) ;
42- }
43-
101+ }
0 commit comments