'use client'; import { useState, useEffect } from 'react'; import { Menu, Avatar, UnstyledButton, Group, Text } from '@mantine/core'; import { useRouter } from 'next/navigation'; interface UserProfile { did: string; handle: string; displayName: string | null; avatar: string | null; } export function UserMenu() { const router = useRouter(); const [profile, setProfile] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { // Fetch user profile on mount fetch('/api/user/profile') .then((res) => res.json()) .then((data) => { if (!data.error) { setProfile(data); } }) .catch((error) => { console.error('Failed to fetch profile:', error); }) .finally(() => { setLoading(false); }); }, []); const handleLogout = async () => { try { await fetch('/api/auth/logout', { method: 'POST' }); router.push('/login'); } catch (error) { console.error('Logout failed:', error); } }; if (loading || !profile) { return ( ? ); } // Get display name or handle const displayText = profile.displayName || profile.handle; // Get initials for fallback const initials = profile.displayName ? profile.displayName .split(' ') .map((n) => n[0]) .join('') .toUpperCase() .slice(0, 2) : profile.handle.slice(0, 2).toUpperCase(); return ( {initials} {displayText} @{profile.handle} Log out ); }