"use client";
/**
* UserMenu — the right-most item in SiteHeader. Three states:
*
* 1. Loading (auth context hasn't hydrated) — render a quiet placeholder
* so the row width doesn't jump when the user data arrives.
* 2. Logged out — single "Log in" link.
* 3. Logged in — avatar / initials chip that opens a popover with
* "Profile" and "Log out". Built on @base-ui/react/popover so we
* don't pull in additional shadcn primitives.
*/
import { Popover } from "@base-ui/react/popover";
import Link from "next/link";
import Image from "next/image";
import { useRouter } from "next/navigation";
import { UserRound } from "lucide-react";
import { useAuth } from "./auth-provider";
import { cn } from "@/lib/utils";
function initials(name: string): string {
return (
name
.split(/\s+/)
.map((p) => p[0])
.slice(0, 2)
.join("")
.toUpperCase() || "?"
);
}
export function UserMenu() {
const { user, loading, logOut } = useAuth();
const router = useRouter();
if (loading) {
// Placeholder keeps the action row width stable until auth hydrates.
return ;
}
if (!user) {
// Mobile: compact icon button so the header row fits on 375px viewports.
// Desktop: full "Log in" text — there's plenty of space.
// Same muted→foreground pattern as the search / reading-settings buttons
// so the icon sits at a consistent quiet shade across all themes.
return (
Log in
);
}
async function onLogOut() {
await logOut();
router.push("/");
router.refresh();
}
return (
{user.avatar_url ? (
) : (
{initials(user.name)}
)}