@@ -24,65 +24,61 @@ import { useWallet } from "@solana/wallet-adapter-react";
24
24
import { WalletMultiButton } from "@solana/wallet-adapter-react-ui" ;
25
25
import { useUser } from "@clerk/nextjs" ;
26
26
import { Copy } from "lucide-react" ;
27
- import WalletBalance from "@/components/WalletBalance" ;
28
- import { Connection , clusterApiUrl } from "@solana/web3.js " ;
27
+ import WalletBalance from "@/components/WalletBalance" ;
28
+ import { useSolanaConnection } from "@/lib/solanaConnection " ;
29
29
30
30
const WalletConnector = ( ) => {
31
31
const { publicKey, connected, disconnect } = useWallet ( ) ;
32
32
const { user } = useUser ( ) ;
33
+ const connection = useSolanaConnection ( ) ;
33
34
const [ isLinked , setIsLinked ] = useState < boolean > ( false ) ;
34
35
const [ loadingLinkStatus , setLoadingLinkStatus ] = useState < boolean > ( true ) ;
35
36
36
- const connection = new Connection ( clusterApiUrl ( "devnet" ) ) ;
37
-
38
- // Fetch the current wallet link status from your API
39
37
useEffect ( ( ) => {
40
38
async function fetchWalletStatus ( ) {
41
- const res = await fetch ( "/api/wallet" ) ;
42
- if ( res . ok ) {
43
- const data = await res . json ( ) ;
44
- setIsLinked ( ! ! data . walletAddress ) ;
39
+ try {
40
+ const res = await fetch ( "/api/wallet" ) ;
41
+ if ( res . ok ) {
42
+ const data = await res . json ( ) ;
43
+ setIsLinked ( ! ! data . walletAddress ) ;
44
+ }
45
+ } catch ( error ) {
46
+ console . error ( "Error fetching wallet status:" , error ) ;
47
+ } finally {
48
+ setLoadingLinkStatus ( false ) ;
45
49
}
46
- setLoadingLinkStatus ( false ) ;
47
50
}
51
+
48
52
if ( user ) {
49
53
fetchWalletStatus ( ) ;
50
54
}
51
55
} , [ user ] ) ;
52
56
53
- // Add listener for transactions affecting the wallet
54
- useEffect ( ( ) => {
55
- if ( ! publicKey || ! connected ) return ;
56
-
57
- const listenerId = connection . onAccountChange ( publicKey , ( ) => {
58
- console . log ( "🔄 Wallet transaction detected! Refreshing balance..." ) ;
59
- window . dispatchEvent ( new Event ( "walletTransaction" ) ) ; // Dispatch event for balance update
60
- } ) ;
61
-
62
- return ( ) => {
63
- connection . removeAccountChangeListener ( listenerId ) ;
64
- } ;
65
- } , [ publicKey , connected ] ) ;
66
-
67
- // Call API to link wallet
68
57
const handleLinkWallet = async ( ) => {
69
58
if ( ! publicKey ) return ;
70
- const res = await fetch ( "/api/wallet" , {
71
- method : "POST" ,
72
- headers : { "Content-Type" : "application/json" } ,
73
- body : JSON . stringify ( { walletAddress : publicKey . toBase58 ( ) } ) ,
74
- } ) ;
75
- if ( res . ok ) {
76
- setIsLinked ( true ) ;
59
+ try {
60
+ const res = await fetch ( "/api/wallet" , {
61
+ method : "POST" ,
62
+ headers : { "Content-Type" : "application/json" } ,
63
+ body : JSON . stringify ( { walletAddress : publicKey . toBase58 ( ) } ) ,
64
+ } ) ;
65
+ if ( res . ok ) {
66
+ setIsLinked ( true ) ;
67
+ }
68
+ } catch ( error ) {
69
+ console . error ( "Error linking wallet:" , error ) ;
77
70
}
78
71
} ;
79
72
80
- // Unlink wallet (and optionally disconnect)
81
73
const handleUnlinkWallet = async ( ) => {
82
- const res = await fetch ( "/api/wallet" , { method : "DELETE" } ) ;
83
- if ( res . ok ) {
84
- setIsLinked ( false ) ;
85
- disconnect ( ) ;
74
+ try {
75
+ const res = await fetch ( "/api/wallet" , { method : "DELETE" } ) ;
76
+ if ( res . ok ) {
77
+ setIsLinked ( false ) ;
78
+ disconnect ( ) ;
79
+ }
80
+ } catch ( error ) {
81
+ console . error ( "Error unlinking wallet:" , error ) ;
86
82
}
87
83
} ;
88
84
@@ -96,13 +92,11 @@ const WalletConnector = () => {
96
92
< div className = "flex items-center space-x-4" >
97
93
{ connected && publicKey ? (
98
94
< >
99
- { /* Display SOL balance using WalletBalance component */ }
100
95
< WalletBalance />
101
-
102
- { /* Display wallet address */ }
96
+
103
97
< div className = "flex items-center space-x-2" >
104
98
< span className = "text-sm font-mono" >
105
- { publicKey . toBase58 ( ) . slice ( 0 , 8 ) + " ..." + publicKey . toBase58 ( ) . slice ( - 8 ) }
99
+ { publicKey . toBase58 ( ) . slice ( 0 , 8 ) } ...{ publicKey . toBase58 ( ) . slice ( - 8 ) }
106
100
</ span >
107
101
< button
108
102
onClick = { handleCopyAddress }
@@ -113,16 +107,22 @@ const WalletConnector = () => {
113
107
</ button >
114
108
</ div >
115
109
116
- { /* Link/Unlink wallet */ }
117
- { ! loadingLinkStatus && ! isLinked && (
118
- < button onClick = { handleLinkWallet } className = "px-2 py-1 bg-green-500 text-white rounded" >
119
- Link Wallet
120
- </ button >
121
- ) }
122
- { ! loadingLinkStatus && isLinked && (
123
- < button onClick = { handleUnlinkWallet } className = "px-2 py-1 bg-red-500 text-white rounded" >
124
- Unlink Wallet
125
- </ button >
110
+ { ! loadingLinkStatus && (
111
+ isLinked ? (
112
+ < button
113
+ onClick = { handleUnlinkWallet }
114
+ className = "px-2 py-1 bg-red-500 text-white rounded hover:bg-red-600 transition-colors"
115
+ >
116
+ Unlink Wallet
117
+ </ button >
118
+ ) : (
119
+ < button
120
+ onClick = { handleLinkWallet }
121
+ className = "px-2 py-1 bg-green-500 text-white rounded hover:bg-green-600 transition-colors"
122
+ >
123
+ Link Wallet
124
+ </ button >
125
+ )
126
126
) }
127
127
</ >
128
128
) : (
0 commit comments