1
1
"use client" ;
2
-
3
- import React , { useState } from "react" ;
2
+ import React , { useState , useEffect } from "react" ;
4
3
import Link from "next/link" ;
5
4
import { usePathname } from "next/navigation" ;
6
5
import { motion , AnimatePresence } from "framer-motion" ;
@@ -11,7 +10,7 @@ import {
11
10
XMarkIcon ,
12
11
Bars3Icon ,
13
12
} from "@heroicons/react/24/outline" ;
14
- import Web3Connect from "../Helper/Web3Connect" ;
13
+ import Web3Connect from "../Helper/Web3Connect" ; // Import Web3Connect
15
14
import Image from "next/image" ;
16
15
17
16
const menuItems = [
@@ -20,12 +19,47 @@ const menuItems = [
20
19
{ name : "Profile" , href : "/profile" , icon : UserIcon } ,
21
20
] ;
22
21
22
+ //if wallet is connected
23
+ async function IsWalletConnected ( ) {
24
+ const { ethereum } = window ;
25
+ if ( ethereum && ethereum . isMetaMask ) {
26
+ const accounts = await ethereum . request ( { method : "eth_accounts" } ) ;
27
+ return accounts . length > 0 ;
28
+ }
29
+ return false ;
30
+ }
31
+
23
32
const Header = ( ) => {
24
33
const pathname = usePathname ( ) ;
34
+
35
+ const [ isWalletConnected , setIsWalletConnected ] = useState < boolean | null > (
36
+ null
37
+ ) ;
38
+
39
+ //handle wallet connection
40
+ const connectWallet = async ( ) => {
41
+ try {
42
+ const connected = await IsWalletConnected ( ) ;
43
+ setIsWalletConnected ( connected ) ;
44
+ } catch ( error ) {
45
+ console . error ( "Failed to connect wallet:" , error ) ;
46
+ setIsWalletConnected ( false ) ;
47
+ }
48
+ } ;
49
+
50
+ // Check wallet connection when the component is mounted
51
+ useEffect ( ( ) => {
52
+ const checkConnection = async ( ) => {
53
+ const connected = await IsWalletConnected ( ) ;
54
+ setIsWalletConnected ( connected ) ;
55
+ } ;
56
+ checkConnection ( ) ;
57
+ } , [ ] ) ;
25
58
const [ isSidebarOpen , setIsSidebarOpen ] = useState ( false ) ;
26
59
27
60
const toggleSidebar = ( ) => setIsSidebarOpen ( ! isSidebarOpen ) ;
28
61
62
+
29
63
return (
30
64
< >
31
65
< motion . header
@@ -49,6 +83,76 @@ const Header = () => {
49
83
</ h1 >
50
84
</ Link >
51
85
86
+
87
+ < nav className = "flex items-center space-x-4" >
88
+ { /* Display Home button always */ }
89
+ < Link href = "/" className = "relative" >
90
+ < motion . button
91
+ className = { `inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md ${
92
+ pathname === "/"
93
+ ? "text-indigo-600"
94
+ : "text-gray-700 hover:text-indigo-600"
95
+ } bg-white hover:bg-gray-50`}
96
+ whileHover = { { scale : 1.05 } }
97
+ whileTap = { { scale : 0.95 } }
98
+ >
99
+ < HomeIcon className = "h-5 w-5 mr-2" aria-hidden = "true" />
100
+ < span className = "hidden sm:inline" > Home</ span >
101
+ </ motion . button >
102
+ { pathname === "/" && (
103
+ < motion . div
104
+ className = "absolute bottom-0 left-0 right-0 h-0.5 bg-indigo-600"
105
+ layoutId = "underline"
106
+ initial = { { opacity : 0 } }
107
+ animate = { { opacity : 1 } }
108
+ transition = { { duration : 0.3 } }
109
+ />
110
+ ) }
111
+ </ Link >
112
+
113
+ { isWalletConnected === null ? (
114
+ // Show "Checking..." button while the connection is being verified
115
+ < button
116
+ className = "px-4 py-2 text-sm font-medium text-white bg-indigo-600 rounded-md hover:bg-indigo-700"
117
+ disabled
118
+ >
119
+ Checking...
120
+ </ button >
121
+ ) : isWalletConnected ? (
122
+ // Show "Create" and "Profile" buttons if wallet is connected
123
+ menuItems
124
+ . filter ( ( item ) => item . name !== "Home" )
125
+ . map ( ( item ) => (
126
+ < Link key = { item . name } href = { item . href } className = "relative" >
127
+ < motion . button
128
+ className = { `inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md ${
129
+ pathname === item . href
130
+ ? "text-indigo-600"
131
+ : "text-gray-700 hover:text-indigo-600"
132
+ } bg-white hover:bg-gray-50`}
133
+ whileHover = { { scale : 1.05 } }
134
+ whileTap = { { scale : 0.95 } }
135
+ >
136
+ < item . icon className = "h-5 w-5 mr-2" aria-hidden = "true" />
137
+ < span className = "hidden sm:inline" > { item . name } </ span >
138
+ </ motion . button >
139
+ { pathname === item . href && (
140
+ < motion . div
141
+ className = "absolute bottom-0 left-0 right-0 h-0.5 bg-indigo-600"
142
+ layoutId = "underline"
143
+ initial = { { opacity : 0 } }
144
+ animate = { { opacity : 1 } }
145
+ transition = { { duration : 0.3 } }
146
+ />
147
+ ) }
148
+ </ Link >
149
+ ) )
150
+ ) : (
151
+ // Show "Connect Wallet" button if wallet is not connected
152
+ < Web3Connect />
153
+ ) }
154
+ </ nav >
155
+
52
156
{ /* Desktop/Tablet Navigation */ }
53
157
< nav className = "hidden lg:flex items-center space-x-4" >
54
158
{ menuItems . map ( ( item ) => (
@@ -94,6 +198,7 @@ const Header = () => {
94
198
</ button >
95
199
</ div >
96
200
</ div >
201
+
97
202
</ div >
98
203
</ motion . header >
99
204
0 commit comments