import { LogOut, User } from "lucide-react"; import { useEffect, useRef, useState } from "react"; import { Link, useLocation } from "react-router-dom"; import { useAuth } from "@/client/auth"; import { ConfirmDialog } from "@/client/components/ui/confirm-dialog"; export const UserMenu = () => { const { user, isAuthenticated, isInitializing, signOut } = useAuth(); const location = useLocation(); const [open, setOpen] = useState(false); const [confirmSignOut, setConfirmSignOut] = useState(false); const menuRef = useRef(null); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(event.target as Node)) { setOpen(false); } }; if (open) { document.addEventListener("mousedown", handleClickOutside); } return () => document.removeEventListener("mousedown", handleClickOutside); }, [open]); useEffect(() => { if (!open) return; const handleEscape = (event: KeyboardEvent) => { if (event.key === "Escape") { setOpen(false); } }; document.addEventListener("keydown", handleEscape); return () => document.removeEventListener("keydown", handleEscape); }, [open]); if (!isAuthenticated || !user) { if (["/", "/app/login"].includes(location.pathname)) return null; return (
{isInitializing ? Checking session... : null} Sign in
); } return (
{open ? (

{user.displayName}

@{user.slug}

setOpen(false)} > Profile & Settings
) : null} { setConfirmSignOut(false); void signOut(); }} onCancel={() => setConfirmSignOut(false)} title="Sign out?" description="Your local session will be cleared." confirmLabel="Sign Out" variant="danger" />
); };