"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { logout } from "@/app/auth-actions"; type Props = { username: string }; export default function Nav({ username }: Props) { const pathname = usePathname() ?? "/"; const isUsers = pathname.startsWith("/users"); const isAccounts = !isUsers; const initial = (username[0] ?? "?").toUpperCase(); return (
CM CM Bot V2 Account dashboard
); } function NavLink({ href, active, children, }: { href: string; active: boolean; children: React.ReactNode; }) { return ( {children} ); } function AccountMenu({ username, initial, }: { username: string; initial: string; }) { const [open, setOpen] = useState(false); const wrapperRef = useRef(null); useEffect(() => { if (!open) return; function onPointerDown(e: PointerEvent) { if (!wrapperRef.current) return; if (!wrapperRef.current.contains(e.target as Node)) setOpen(false); } function onKey(e: KeyboardEvent) { if (e.key === "Escape") setOpen(false); } document.addEventListener("pointerdown", onPointerDown); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("pointerdown", onPointerDown); document.removeEventListener("keydown", onKey); }; }, [open]); return (
{open && (
{username}
{/* No onClick to close the menu — the click would trigger setOpen (which unmounts the form on next render) and the form submit in parallel; React tears down the form before the POST flushes and sign-out silently no-ops. The Server Action redirects to /cm-auth on success, which navigates away and tears the menu down naturally. */}
)}
); }