CurrentUserAvatar
A component that displays the current authenticated user’s avatar with automatic loading and fallback states.Props
Size of the avatar
sm: 8x8 (32px) with text-xsmd: 10x10 (40px) with text-smlg: 12x12 (48px) with text-base
Additional CSS classes to apply to the avatar
Whether to show a fallback avatar when user is not authenticated. If
false, renders null when not authenticated.Hooks Used
The component uses two custom hooks to fetch user data:useCurrentUserImage
Fetches the current user’s avatar image URL. Returns:string | null- The user’s image URL, ornullif not setundefined- While loading
useCurrentUserName
Fetches the current user’s display name. Returns:string | null- The user’s name, ornullif not setundefined- While loading
States
Loading State
Whenimage === undefined or name === undefined, displays a skeleton loader.
Not Authenticated State
- If
showFallback === falseand user is not authenticated: rendersnull - If
showFallback === trueand user is not authenticated: renders avatar with ”?” fallback
Authenticated State
Displays user’s avatar image or initials fallback.Initials Generation
The component generates initials from the user’s name:- Takes the first letter of each word
- Converts to uppercase
- Limits to 2 characters
- Falls back to ”?” if name is null
- “John Doe” → “JD”
- “Alice” → “A”
- null → ”?”
Example
Direct Hook Usage
Integration with Authentication
This component requires:- Convex Auth setup with user authentication
- A
users.currentquery in your Convex backend that returns the current user - User schema with
nameandimagefields
Styling
The component uses Tailwind CSS classes and relies on:Avatar,AvatarImage,AvatarFallbackfrom@/components/ui/avatarSkeletonfrom@/components/ui/skeletoncnutility from@/lib/utilsfor class merging