'use client'; import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { Button } from '@/components/ui/button'; import { RiUserLine } from '@remixicon/react'; interface SessionCustomer { displayName: string; email: string; firstName?: string | null; } const AccountMenu: React.FC = () => { const router = useRouter(); const [customer, setCustomer] = useState(null); const [open, setOpen] = useState(false); // The token lives in an httpOnly cookie, so the signed-in state has to come // from the server rather than being read directly. useEffect(() => { let cancelled = false; fetch('/api/account/me') .then((response) => response.json()) .then((data) => { if (!cancelled) setCustomer(data.customer ?? null); }) .catch(() => { if (!cancelled) setCustomer(null); }); return () => { cancelled = true; }; }, []); const handleSignOut = async () => { setOpen(false); await fetch('/api/account/logout', { method: 'POST' }); setCustomer(null); router.push('/'); router.refresh(); }; // Signed out: straight to the sign-in page, no menu. if (!customer) { return ( ); } return (
{open && ( <>
setOpen(false)} />

{customer.firstName || customer.displayName}

{customer.email}

setOpen(false)} className="block px-4 py-2 text-sm text-foreground hover:bg-accent" > Order history
)}
); }; export default AccountMenu;