Create frontend/src/components/UserMenu.jsx
Browse files
frontend/src/components/UserMenu.jsx
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// frontend/src/components/UserMenu.jsx
|
| 2 |
+
import React, { useState, useRef, useEffect } from "react";
|
| 3 |
+
|
| 4 |
+
export default function UserMenu({ name, email, onLogout }) {
|
| 5 |
+
const [open, setOpen] = useState(false);
|
| 6 |
+
const ref = useRef(null);
|
| 7 |
+
|
| 8 |
+
useEffect(() => {
|
| 9 |
+
function handleClickOutside(e) {
|
| 10 |
+
if (ref.current && !ref.current.contains(e.target)) {
|
| 11 |
+
setOpen(false);
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
document.addEventListener("mousedown", handleClickOutside);
|
| 15 |
+
return () => document.removeEventListener("mousedown", handleClickOutside);
|
| 16 |
+
}, []);
|
| 17 |
+
|
| 18 |
+
const initial =
|
| 19 |
+
(name && name.trim().charAt(0).toUpperCase()) ||
|
| 20 |
+
(email && email.trim().charAt(0).toUpperCase()) ||
|
| 21 |
+
"?";
|
| 22 |
+
|
| 23 |
+
return (
|
| 24 |
+
<div className="relative" ref={ref}>
|
| 25 |
+
<button
|
| 26 |
+
type="button"
|
| 27 |
+
onClick={() => setOpen((v) => !v)}
|
| 28 |
+
className="inline-flex items-center gap-2 rounded-full border border-slate-200 bg-white px-3 py-1.5 text-xs text-slate-700 hover:bg-slate-50"
|
| 29 |
+
>
|
| 30 |
+
<span className="inline-flex items-center justify-center h-6 w-6 rounded-full bg-slate-100 text-[11px] font-semibold text-slate-700">
|
| 31 |
+
{initial}
|
| 32 |
+
</span>
|
| 33 |
+
<span className="hidden sm:inline max-w-[120px] truncate">
|
| 34 |
+
{email || name || "Account"}
|
| 35 |
+
</span>
|
| 36 |
+
<span className="text-[10px] text-slate-400">▼</span>
|
| 37 |
+
</button>
|
| 38 |
+
|
| 39 |
+
{open && (
|
| 40 |
+
<div className="absolute right-0 mt-2 w-40 rounded-lg border border-slate-200 bg-white shadow-lg text-xs text-slate-800 z-50">
|
| 41 |
+
<div className="px-3 py-2 border-b border-slate-100">
|
| 42 |
+
<div className="font-semibold truncate">
|
| 43 |
+
{name || "User"}
|
| 44 |
+
</div>
|
| 45 |
+
{email && (
|
| 46 |
+
<div className="text-[11px] text-slate-500 truncate">
|
| 47 |
+
{email}
|
| 48 |
+
</div>
|
| 49 |
+
)}
|
| 50 |
+
</div>
|
| 51 |
+
{/* Extra nav items can be added here later */}
|
| 52 |
+
<button
|
| 53 |
+
type="button"
|
| 54 |
+
onClick={() => {
|
| 55 |
+
setOpen(false);
|
| 56 |
+
onLogout && onLogout();
|
| 57 |
+
}}
|
| 58 |
+
className="w-full text-left px-3 py-2 hover:bg-slate-50 text-[11px]"
|
| 59 |
+
>
|
| 60 |
+
Log out
|
| 61 |
+
</button>
|
| 62 |
+
</div>
|
| 63 |
+
)}
|
| 64 |
+
</div>
|
| 65 |
+
);
|
| 66 |
+
}
|