|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useRef, useEffect } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { |
| 6 | + useMerchantMetadata, |
| 7 | + useMerchantLogout |
| 8 | +} from "@/lib/merchant-store"; |
| 9 | +import { useRouter } from "next/navigation"; |
| 10 | + |
| 11 | +export default function UserNav() { |
| 12 | + const [open, setOpen] = useState(false); |
| 13 | + const merchant = useMerchantMetadata(); |
| 14 | + const logout = useMerchantLogout(); |
| 15 | + const router = useRouter(); |
| 16 | + const menuRef = useRef<HTMLDivElement>(null); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const handleClickOutside = (event: MouseEvent) => { |
| 20 | + if (menuRef.current && !menuRef.current.contains(event.target as Node)) { |
| 21 | + setOpen(false); |
| 22 | + } |
| 23 | + }; |
| 24 | + document.addEventListener("mousedown", handleClickOutside); |
| 25 | + return () => document.removeEventListener("mousedown", handleClickOutside); |
| 26 | + }, []); |
| 27 | + |
| 28 | + const handleLogout = () => { |
| 29 | + logout(); |
| 30 | + router.push("/login"); |
| 31 | + }; |
| 32 | + |
| 33 | + if (!merchant) return null; |
| 34 | + |
| 35 | + const initials = merchant.business_name |
| 36 | + ? merchant.business_name.split(" ").map(n => n[0]).join("").toUpperCase().slice(0, 2) |
| 37 | + : merchant.email[0].toUpperCase(); |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className="relative" ref={menuRef}> |
| 41 | + <button |
| 42 | + onClick={() => setOpen(!open)} |
| 43 | + className="flex items-center gap-3 rounded-full border border-[#E8E8E8] bg-white p-1 pr-4 transition-all hover:border-[#0A0A0A] hover:shadow-sm active:scale-[0.98]" |
| 44 | + > |
| 45 | + <div className="flex h-8 w-8 items-center justify-center rounded-full bg-[#0A0A0A] text-[10px] font-bold text-white uppercase tracking-tighter"> |
| 46 | + {initials} |
| 47 | + </div> |
| 48 | + <div className="hidden flex-col items-start text-left sm:flex"> |
| 49 | + <span className="text-[10px] font-bold uppercase tracking-widest text-[#0A0A0A]"> |
| 50 | + {merchant.business_name} |
| 51 | + </span> |
| 52 | + <span className="text-[9px] font-medium text-[#6B6B6B]"> |
| 53 | + {merchant.email} |
| 54 | + </span> |
| 55 | + </div> |
| 56 | + </button> |
| 57 | + |
| 58 | + {open && ( |
| 59 | + <div className="absolute right-0 mt-3 w-56 origin-top-right rounded-2xl border border-[#E8E8E8] bg-white p-2 shadow-2xl animate-in fade-in zoom-in-95 duration-200 z-50"> |
| 60 | + <div className="px-3 py-2 border-b border-[#F5F5F5] mb-1 sm:hidden"> |
| 61 | + <p className="text-[10px] font-bold uppercase tracking-widest text-[#0A0A0A] truncate">{merchant.business_name}</p> |
| 62 | + <p className="text-[9px] font-medium text-[#6B6B6B] truncate">{merchant.email}</p> |
| 63 | + </div> |
| 64 | + |
| 65 | + <Link |
| 66 | + href="/dashboard" |
| 67 | + onClick={() => setOpen(false)} |
| 68 | + className="flex items-center gap-3 rounded-xl px-3 py-2.5 text-[10px] font-bold uppercase tracking-widest text-[#0A0A0A] bg-[#F5F5F5] transition-all hover:bg-[#E8E8E8]" |
| 69 | + > |
| 70 | + <svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 71 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.8} d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z" /> |
| 72 | + </svg> |
| 73 | + Dashboard |
| 74 | + </Link> |
| 75 | + |
| 76 | + <Link |
| 77 | + href="/settings" |
| 78 | + onClick={() => setOpen(false)} |
| 79 | + className="flex items-center gap-3 rounded-xl px-3 py-2.5 text-[10px] font-bold uppercase tracking-widest text-[#6B6B6B] transition-colors hover:bg-[#F9F9F9] hover:text-[#0A0A0A]" |
| 80 | + > |
| 81 | + <svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 82 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.8} d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4" /> |
| 83 | + </svg> |
| 84 | + Account Settings |
| 85 | + </Link> |
| 86 | + |
| 87 | + <Link |
| 88 | + href="/support" |
| 89 | + onClick={() => setOpen(false)} |
| 90 | + className="flex items-center gap-3 rounded-xl px-3 py-2.5 text-[10px] font-bold uppercase tracking-widest text-[#6B6B6B] transition-colors hover:bg-[#F9F9F9] hover:text-[#0A0A0A]" |
| 91 | + > |
| 92 | + <svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 93 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.8} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> |
| 94 | + </svg> |
| 95 | + Support Center |
| 96 | + </Link> |
| 97 | + |
| 98 | + <div className="my-1 h-px bg-[#F5F5F5]" /> |
| 99 | + |
| 100 | + <button |
| 101 | + onClick={handleLogout} |
| 102 | + className="flex w-full items-center gap-3 rounded-xl px-3 py-2.5 text-[10px] font-bold uppercase tracking-widest text-red-600 transition-colors hover:bg-red-50" |
| 103 | + > |
| 104 | + <svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 105 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.8} d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1" /> |
| 106 | + </svg> |
| 107 | + Sign Out |
| 108 | + </button> |
| 109 | + </div> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
0 commit comments