|
1 | | -import { Link } from "react-router-dom"; |
2 | | -import { useState, useContext } from "react"; |
| 1 | +import { Link, useNavigate, useLocation } from "react-router-dom"; |
| 2 | +import { useState, useContext, useEffect } from "react"; |
3 | 3 | import { ThemeContext } from "../context/ThemeContext"; |
4 | | -import { Moon, Sun } from 'lucide-react'; |
5 | | - |
| 4 | +import { Moon, Sun } from "lucide-react"; |
| 5 | +import { useRef } from "react"; |
6 | 6 |
|
7 | 7 | const Navbar: React.FC = () => { |
| 8 | + const navigate = useNavigate(); |
| 9 | + const location = useLocation(); |
| 10 | + |
| 11 | + const [menuOpen, setMenuOpen] = useState(false); |
| 12 | + const menuRef = useRef<HTMLDivElement>(null); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + const onClick = (e: MouseEvent) => { |
| 16 | + if (menuRef.current && !menuRef.current.contains(e.target as Node)) |
| 17 | + setMenuOpen(false); |
| 18 | + }; |
| 19 | + |
| 20 | + const onStorage = (e: StorageEvent) => { |
| 21 | + if (e.key === "token") setIsAuthed(!!e.newValue); |
| 22 | + }; |
| 23 | + |
| 24 | + const onAuthChange = () => { |
| 25 | + setIsAuthed(!!localStorage.getItem("token")); |
| 26 | + }; |
| 27 | + |
| 28 | + window.addEventListener("storage", onStorage); |
| 29 | + window.addEventListener("authChange", onAuthChange); |
| 30 | + window.addEventListener("click", onClick); |
| 31 | + return () => { |
| 32 | + window.removeEventListener("storage", onStorage); |
| 33 | + window.removeEventListener("authChange", onAuthChange); |
| 34 | + window.removeEventListener("click", onClick); |
| 35 | + }; |
| 36 | + }, []); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + setIsOpen(false); // close mobile menu on navigation |
| 40 | + }, [location.pathname]); |
| 41 | + |
| 42 | + const handleLogout = () => { |
| 43 | + localStorage.removeItem("token"); |
| 44 | + setIsAuthed(false); |
| 45 | + navigate("/login"); |
| 46 | + }; |
8 | 47 |
|
| 48 | + const [isAuthed, setIsAuthed] = useState<boolean>( |
| 49 | + !!localStorage.getItem("token") |
| 50 | + ); |
9 | 51 | const [isOpen, setIsOpen] = useState<boolean>(false); |
10 | 52 | const themeContext = useContext(ThemeContext); |
11 | 53 |
|
@@ -46,12 +88,97 @@ const Navbar: React.FC = () => { |
46 | 88 | > |
47 | 89 | Contributors |
48 | 90 | </Link> |
49 | | - <Link |
50 | | - to="/login" |
51 | | - className="text-lg font-medium hover:text-gray-300 transition-all px-2 py-1 border border-transparent hover:border-gray-400 rounded" |
52 | | - > |
53 | | - Login |
54 | | - </Link> |
| 91 | + {/* replace your auth block with this */} |
| 92 | + {isAuthed ? ( |
| 93 | + <div className="relative" ref={menuRef}> |
| 94 | + <button |
| 95 | + onClick={() => setMenuOpen((v) => !v)} |
| 96 | + className="flex items-center gap-2 px-2 py-1 rounded hover:bg-gray-100 dark:hover:bg-gray-800 transition" |
| 97 | + aria-haspopup="menu" |
| 98 | + aria-expanded={menuOpen} |
| 99 | + > |
| 100 | + {/* Avatar Icon */} |
| 101 | + <img |
| 102 | + src={ |
| 103 | + JSON.parse(localStorage.getItem("user") || "{}") |
| 104 | + ?.avatarUrl || "/profile.svg" |
| 105 | + } |
| 106 | + alt="avatar" |
| 107 | + className="h-8 w-8 rounded-full object-cover border border-gray-300 dark:border-gray-600" |
| 108 | + /> |
| 109 | + |
| 110 | + {/* Username beside icon */} |
| 111 | + {/* <span className="text-sm font-medium"> |
| 112 | + {JSON.parse(localStorage.getItem("user") || "{}")?.username || |
| 113 | + "Me"} |
| 114 | + </span> */} |
| 115 | + |
| 116 | + {/* Chevron icon */} |
| 117 | + <svg |
| 118 | + className={`h-4 w-4 transition-transform ${ |
| 119 | + menuOpen ? "rotate-180" : "" |
| 120 | + }`} |
| 121 | + viewBox="0 0 20 20" |
| 122 | + fill="currentColor" |
| 123 | + > |
| 124 | + <path d="M5.25 7.5L10 12.25L14.75 7.5H5.25Z" /> |
| 125 | + </svg> |
| 126 | + </button> |
| 127 | + |
| 128 | + {/* Dropdown Menu */} |
| 129 | + {menuOpen && ( |
| 130 | + <div |
| 131 | + role="menu" |
| 132 | + className="absolute right-0 mt-2 w-56 rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-lg p-2" |
| 133 | + > |
| 134 | + {/* User Info */} |
| 135 | + <div className="px-3 py-2 border-b border-gray-100 dark:border-gray-700"> |
| 136 | + <p className="text-sm font-semibold text-gray-900 dark:text-gray-100"> |
| 137 | + {JSON.parse(localStorage.getItem("user") || "{}") |
| 138 | + ?.username || "User"} |
| 139 | + </p> |
| 140 | + <p className="text-xs text-gray-500 dark:text-gray-400 truncate"> |
| 141 | + {JSON.parse(localStorage.getItem("user") || "{}") |
| 142 | + ?.email || "email@example.com"} |
| 143 | + </p> |
| 144 | + </div> |
| 145 | + |
| 146 | + {/* Menu Links */} |
| 147 | + <Link |
| 148 | + to="/profile" |
| 149 | + className="block px-3 py-2 text-sm rounded hover:bg-gray-100 dark:hover:bg-gray-700" |
| 150 | + onClick={() => setMenuOpen(false)} |
| 151 | + > |
| 152 | + View Profile |
| 153 | + </Link> |
| 154 | + <Link |
| 155 | + to="/profile/edit" |
| 156 | + className="block px-3 py-2 text-sm rounded hover:bg-gray-100 dark:hover:bg-gray-700" |
| 157 | + onClick={() => setMenuOpen(false)} |
| 158 | + > |
| 159 | + Edit Profile |
| 160 | + </Link> |
| 161 | + <button |
| 162 | + onClick={() => { |
| 163 | + setMenuOpen(false); |
| 164 | + handleLogout(); |
| 165 | + }} |
| 166 | + className="block w-full text-left px-3 py-2 text-sm rounded hover:bg-gray-100 dark:hover:bg-gray-700" |
| 167 | + > |
| 168 | + Logout |
| 169 | + </button> |
| 170 | + </div> |
| 171 | + )} |
| 172 | + </div> |
| 173 | + ) : ( |
| 174 | + <Link |
| 175 | + to="/login" |
| 176 | + className="text-lg font-medium hover:text-gray-300 transition-all px-2 py-1 border border-transparent hover:border-gray-400 rounded" |
| 177 | + > |
| 178 | + Login |
| 179 | + </Link> |
| 180 | + )} |
| 181 | + |
55 | 182 | <button |
56 | 183 | onClick={toggleTheme} |
57 | 184 | className="text-sm font-semibold px-3 py-1 rounded border border-gray-500 hover:text-gray-300 hover:border-gray-300 transition duration-200" |
@@ -117,13 +244,33 @@ const Navbar: React.FC = () => { |
117 | 244 | > |
118 | 245 | Contributors |
119 | 246 | </Link> |
120 | | - <Link |
121 | | - to="/login" |
122 | | - className="block text-lg font-medium hover:text-gray-300 transition-all px-2 py-1 border border-transparent hover:border-gray-400 rounded" |
123 | | - onClick={() => setIsOpen(false)} |
124 | | - > |
125 | | - Login |
126 | | - </Link> |
| 247 | + {isAuthed ? ( |
| 248 | + <> |
| 249 | + <Link |
| 250 | + to="/profile" |
| 251 | + className="block text-lg font-medium hover:text-gray-300 transition-all px-2 py-1 border border-transparent hover:border-gray-400 rounded" |
| 252 | + onClick={() => setIsOpen(false)} |
| 253 | + > |
| 254 | + Profile |
| 255 | + </Link> |
| 256 | + <button |
| 257 | + onClick={() => { |
| 258 | + handleLogout(); |
| 259 | + }} |
| 260 | + className="block text-left text-lg font-medium px-2 py-1 border border-transparent hover:border-gray-400 rounded w-full" |
| 261 | + > |
| 262 | + Logout |
| 263 | + </button> |
| 264 | + </> |
| 265 | + ) : ( |
| 266 | + <Link |
| 267 | + to="/login" |
| 268 | + className="block text-lg font-medium hover:text-gray-300 transition-all px-2 py-1 border border-transparent hover:border-gray-400 rounded" |
| 269 | + onClick={() => setIsOpen(false)} |
| 270 | + > |
| 271 | + Login |
| 272 | + </Link> |
| 273 | + )} |
127 | 274 | <button |
128 | 275 | onClick={() => { |
129 | 276 | toggleTheme(); |
|
0 commit comments