Skip to content

Commit 12f23d2

Browse files
authored
Merge pull request #94 from vasusadariya/fixbugconnectwalletbutton
Fix the bug now connect wallet appears first and if wallet is connected then profile and create button is visible
2 parents 6e314a1 + 0933ab6 commit 12f23d2

File tree

2 files changed

+127
-11
lines changed

2 files changed

+127
-11
lines changed

client/app/components/Header/Header.tsx

+108-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use client";
2-
3-
import React, { useState } from "react";
2+
import React, { useState, useEffect } from "react";
43
import Link from "next/link";
54
import { usePathname } from "next/navigation";
65
import { motion, AnimatePresence } from "framer-motion";
@@ -11,7 +10,7 @@ import {
1110
XMarkIcon,
1211
Bars3Icon,
1312
} from "@heroicons/react/24/outline";
14-
import Web3Connect from "../Helper/Web3Connect";
13+
import Web3Connect from "../Helper/Web3Connect"; // Import Web3Connect
1514
import Image from "next/image";
1615

1716
const menuItems = [
@@ -20,12 +19,47 @@ const menuItems = [
2019
{ name: "Profile", href: "/profile", icon: UserIcon },
2120
];
2221

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+
2332
const Header = () => {
2433
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+
}, []);
2558
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
2659

2760
const toggleSidebar = () => setIsSidebarOpen(!isSidebarOpen);
2861

62+
2963
return (
3064
<>
3165
<motion.header
@@ -49,6 +83,76 @@ const Header = () => {
4983
</h1>
5084
</Link>
5185

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+
52156
{/* Desktop/Tablet Navigation */}
53157
<nav className="hidden lg:flex items-center space-x-4">
54158
{menuItems.map((item) => (
@@ -94,6 +198,7 @@ const Header = () => {
94198
</button>
95199
</div>
96200
</div>
201+
97202
</div>
98203
</motion.header>
99204

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import React from "react";
22
import { ConnectButton } from "@rainbow-me/rainbowkit";
3+
import { useAccount } from "wagmi";
34
import "@rainbow-me/rainbowkit/styles.css";
5+
46
const Web3Connect = () => {
7+
const { isConnected, address } = useAccount();
8+
59
return (
610
<div className="p-4">
7-
<ConnectButton
8-
accountStatus={{
9-
smallScreen: "avatar",
10-
largeScreen: "full",
11-
}}
12-
showBalance={false}
13-
/>
11+
{/* If connected, show the wallet address, else show the Connect button */}
12+
{isConnected ? (
13+
<div>
14+
<span>Connected to: {address}</span>
15+
</div>
16+
) : (
17+
<ConnectButton
18+
accountStatus={{
19+
smallScreen: "avatar",
20+
largeScreen: "full",
21+
}}
22+
showBalance={false}
23+
/>
24+
)}
1425
</div>
1526
);
1627
};
1728

18-
export default Web3Connect;
29+
export default Web3Connect;

0 commit comments

Comments
 (0)