| // src/components/Navbar.tsx | |
| import { useState, useEffect, useRef } from "react"; | |
| import { Link, useNavigate } from "react-router-dom"; | |
| import { useAuth } from "@/context/AuthContext"; | |
| import { NotificationDropdown } from "@/components/notifications/NotificationDropdown"; | |
| import { Search, Menu, X, ChevronDown, User, LogOut, Settings, LayoutDashboard, Users, PlusCircle, Package, Terminal } from "lucide-react"; | |
| export default function Navbar() { | |
| const { user, logout, refreshUser } = useAuth(); | |
| const navigate = useNavigate(); | |
| const [searchQuery, setSearchQuery] = useState(""); | |
| const [mobileMenuOpen, setMobileMenuOpen] = useState(false); | |
| const [avatarMenuOpen, setAvatarMenuOpen] = useState(false); | |
| const avatarMenuRef = useRef<HTMLDivElement>(null); | |
| useEffect(() => { | |
| const cookieExists = document.cookie.includes("auth_token="); | |
| if (!user && cookieExists) { | |
| refreshUser().catch(() => {}); | |
| } | |
| }, [user, refreshUser]); | |
| useEffect(() => { | |
| const handleClickOutside = (e: MouseEvent) => { | |
| if (avatarMenuRef.current && !avatarMenuRef.current.contains(e.target as Node)) { | |
| setAvatarMenuOpen(false); | |
| } | |
| }; | |
| document.addEventListener("mousedown", handleClickOutside); | |
| return () => document.removeEventListener("mousedown", handleClickOutside); | |
| }, []); | |
| const handleLogout = async () => { | |
| await logout(); | |
| navigate("/"); | |
| }; | |
| const handleSearch = (e: React.FormEvent) => { | |
| e.preventDefault(); | |
| if (searchQuery.trim()) { | |
| navigate(`/search?q=${encodeURIComponent(searchQuery)}`); | |
| setMobileMenuOpen(false); | |
| } | |
| }; | |
| return ( | |
| <nav className="sticky top-0 z-50 premium-glass"> | |
| <div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8"> | |
| <div className="flex h-16 items-center justify-between"> | |
| {/* Logo */} | |
| <Link to="/" className="flex hover-lift shrink-0 items-center gap-2 text-xl font-bold text-primary" aria-label="IE Research home"> | |
| <span className="flex h-9 w-9 items-center justify-center rounded-2xl bg-gradient-to-r from-teal-500 to-emerald-400 text-white shadow-lg">IE</span> | |
| <span className="hidden sm:inline premium-gradient">IE Research</span> | |
| </Link> | |
| {/* Desktop nav links */} | |
| <div className="hidden md:flex md:items-center md:gap-6"> | |
| <Link to="/articles" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Articles</Link> | |
| <Link to="/groups" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Groups</Link> | |
| <Link to="/projects" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Projects</Link> | |
| <Link to="/pricing" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Pricing</Link> | |
| <Link to="/messages" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Messages</Link> | |
| <Link to="/calendar" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Calendar</Link> | |
| <Link to="/tools" className="flex items-center gap-1 text-gray-700 transition hover:text-primary dark:text-gray-200"> | |
| <Package className="h-4 w-4" /> Tools | |
| </Link> | |
| <Link to="/cli-docs" className="flex items-center gap-1 text-gray-700 transition hover:text-primary dark:text-gray-200"> | |
| <Terminal className="h-4 w-4" /> CLI | |
| </Link> | |
| {user && ( | |
| <Link to="/tools/installed" className="text-gray-700 transition hover:text-primary dark:text-gray-200"> | |
| Installed Apps | |
| </Link> | |
| )} | |
| {user && ( | |
| <> | |
| <Link to="/articles/new" className="flex items-center gap-1 text-gray-700 transition hover:text-primary dark:text-gray-200"> | |
| <PlusCircle className="h-4 w-4" /> Write | |
| </Link> | |
| <Link to="/dashboard" className="text-gray-700 transition hover:text-primary dark:text-gray-200">Dashboard</Link> | |
| </> | |
| )} | |
| </div> | |
| {/* Search bar (desktop) */} | |
| <div className="hidden flex-1 max-w-md mx-4 md:block"> | |
| <form onSubmit={handleSearch} className="relative"> | |
| <label htmlFor="navbar-search" className="sr-only">Search articles, authors, groups, or projects</label> | |
| <input | |
| id="navbar-search" | |
| type="text" | |
| placeholder="Search articles, authors..." | |
| value={searchQuery} | |
| onChange={(e) => setSearchQuery(e.target.value)} | |
| className="w-full rounded-full border border-slate-200/50 bg-white/40 backdrop-blur-md py-2 pl-10 pr-4 focus:outline-none focus:ring-2 focus:ring-indigo-500 dark:border-slate-800/50 dark:bg-slate-900/40 text-slate-800 dark:text-slate-100 transition-all shadow-sm" | |
| /> | |
| <Search className="absolute left-3 top-2.5 h-4 w-4 text-gray-400" /> | |
| </form> | |
| </div> | |
| {/* Right side */} | |
| <div className="flex items-center gap-4"> | |
| {user ? ( | |
| <> | |
| <NotificationDropdown /> | |
| <div className="relative" ref={avatarMenuRef}> | |
| <button | |
| onClick={() => setAvatarMenuOpen(!avatarMenuOpen)} | |
| className="group flex items-center gap-2 focus:outline-none" | |
| aria-label="User menu" | |
| aria-expanded={avatarMenuOpen} | |
| > | |
| <div className="flex h-8 w-8 items-center justify-center rounded-full bg-gradient-to-r from-primary to-secondary font-bold text-white"> | |
| {user.username[0].toUpperCase()} | |
| </div> | |
| <span className="hidden text-gray-700 transition group-hover:text-primary dark:text-gray-200 md:inline"> | |
| {user.username} | |
| </span> | |
| <ChevronDown className="h-4 w-4 text-gray-500 transition group-hover:text-primary" /> | |
| </button> | |
| {avatarMenuOpen && ( | |
| <div className="absolute right-0 z-20 mt-2 w-52 rounded-2xl border border-slate-200/50 bg-white/80 p-1.5 backdrop-blur-xl shadow-xl dark:border-slate-800/50 dark:bg-slate-950/80"> | |
| <Link to="/dashboard" className="flex items-center gap-2.5 px-3 py-2 text-sm rounded-xl hover:bg-slate-100 dark:hover:bg-slate-900 transition-colors" onClick={() => setAvatarMenuOpen(false)}> | |
| <LayoutDashboard className="h-4 w-4 text-indigo-500" /> Dashboard | |
| </Link> | |
| <Link to={`/users/${user.username}`} className="flex items-center gap-2.5 px-3 py-2 text-sm rounded-xl hover:bg-slate-100 dark:hover:bg-slate-900 transition-colors" onClick={() => setAvatarMenuOpen(false)}> | |
| <User className="h-4 w-4 text-emerald-500" /> Profile | |
| </Link> | |
| <Link to="/settings" className="flex items-center gap-2.5 px-3 py-2 text-sm rounded-xl hover:bg-slate-100 dark:hover:bg-slate-900 transition-colors" onClick={() => setAvatarMenuOpen(false)}> | |
| <Settings className="h-4 w-4 text-violet-500" /> Settings | |
| </Link> | |
| {user.role === "admin" && ( | |
| <Link to="/admin" className="flex items-center gap-2.5 px-3 py-2 text-sm rounded-xl hover:bg-slate-100 dark:hover:bg-slate-900 transition-colors" onClick={() => setAvatarMenuOpen(false)}> | |
| <Users className="h-4 w-4 text-rose-500" /> Admin | |
| </Link> | |
| )} | |
| <hr className="my-1.5 border-slate-200 dark:border-slate-800" /> | |
| <button onClick={handleLogout} className="flex w-full items-center gap-2.5 px-3 py-2 text-sm rounded-xl text-red-600 hover:bg-red-50 dark:hover:bg-red-950/30 transition-colors"> | |
| <LogOut className="h-4 w-4" /> Logout | |
| </button> | |
| </div> | |
| )} | |
| </div> | |
| </> | |
| ) : ( | |
| <div className="flex gap-2"> | |
| <Link to="/login" className="btn-secondary px-4 py-1.5 text-xs font-semibold rounded-full">Login</Link> | |
| <Link to="/register" className="btn-primary px-4 py-1.5 text-xs font-semibold rounded-full">Register</Link> | |
| </div> | |
| )} | |
| {/* Mobile menu button */} | |
| <button | |
| onClick={() => setMobileMenuOpen(!mobileMenuOpen)} | |
| className="text-gray-700 focus:outline-none md:hidden dark:text-gray-200" | |
| aria-label="Toggle menu" | |
| > | |
| {mobileMenuOpen ? <X className="h-6 w-6" /> : <Menu className="h-6 w-6" />} | |
| </button> | |
| </div> | |
| </div> | |
| {/* Mobile menu */} | |
| {mobileMenuOpen && ( | |
| <div className="space-y-4 border-t py-4 dark:border-gray-700 md:hidden"> | |
| <div className="space-y-2"> | |
| <div className="text-[10px] font-semibold uppercase tracking-[0.22em] text-gray-400">Explore</div> | |
| <Link to="/articles" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Articles</Link> | |
| <Link to="/groups" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Groups</Link> | |
| <Link to="/projects" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Projects</Link> | |
| <Link to="/pricing" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Pricing</Link> | |
| <Link to="/tools" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Tools</Link> | |
| </div> | |
| {user && ( | |
| <div className="space-y-2"> | |
| <div className="text-[10px] font-semibold uppercase tracking-[0.22em] text-gray-400">Workspace</div> | |
| <Link to="/dashboard" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Dashboard</Link> | |
| <Link to="/messages" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Messages</Link> | |
| <Link to="/calendar" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Calendar</Link> | |
| <Link to="/tools/installed" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Installed Apps</Link> | |
| <Link to="/articles/new" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>New Article</Link> | |
| </div> | |
| )} | |
| {user && ( | |
| <> | |
| <Link to="/profile" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Profile</Link> | |
| <Link to="/settings" className="block text-gray-700 dark:text-gray-200" onClick={() => setMobileMenuOpen(false)}>Settings</Link> | |
| </> | |
| )} | |
| <form onSubmit={handleSearch} className="relative mt-2"> | |
| <label htmlFor="navbar-search-mobile" className="sr-only">Search</label> | |
| <input | |
| id="navbar-search-mobile" | |
| type="text" | |
| placeholder="Search..." | |
| value={searchQuery} | |
| onChange={(e) => setSearchQuery(e.target.value)} | |
| className="w-full rounded-full border border-slate-200/50 bg-white/40 backdrop-blur-md py-2 pl-10 pr-4 focus:outline-none focus:ring-2 focus:ring-indigo-500 dark:border-slate-800/50 dark:bg-slate-900/40 text-slate-800 dark:text-slate-100 transition-all" | |
| /> | |
| <Search className="absolute left-3 top-2.5 h-4 w-4 text-gray-400" /> | |
| </form> | |
| </div> | |
| )} | |
| </div> | |
| </nav> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 12.2 kB
- Xet hash:
- e1f69b96a9fb7e758cd4d1a370336c6954093596e446bb6786470ab32f375e4d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.