'use client'; import { useState } from 'react'; import Link from 'next/link'; import Image from 'next/image'; import { UserHoverCard } from './UserHoverCard'; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface UserCardUser { id: string; username: string; displayName?: string; avatar?: string | null; bio?: string | null; followersCount: number; isFollowing?: boolean; } interface UserCardProps { user: UserCardUser; variant?: 'compact' | 'full'; onFollowChange?: (userId: string, isFollowing: boolean) => void; } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function formatFollowers(count: number): string { if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`; if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k`; return String(count); } function getInitials(username: string, displayName?: string | null): string { const name = displayName ?? username; const parts = name.trim().split(/\s+/); if (parts.length >= 2) { return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase(); } return name.slice(0, 2).toUpperCase(); } // --------------------------------------------------------------------------- // Avatar sub-component // --------------------------------------------------------------------------- function Avatar({ user, size }: { user: UserCardUser; size: 'sm' | 'lg' }) { const [imgError, setImgError] = useState(false); const dim = size === 'lg' ? 48 : 32; const sizeClass = size === 'lg' ? 'w-12 h-12' : 'w-8 h-8'; const textClass = size === 'lg' ? 'text-base' : 'text-xs'; if (user.avatar && !imgError) { return ( {user.username} setImgError(true)} unoptimized /> ); } return ( ); } // --------------------------------------------------------------------------- // Main component // --------------------------------------------------------------------------- export function UserCard({ user, variant = 'full', onFollowChange }: UserCardProps) { const [isFollowing, setIsFollowing] = useState(user.isFollowing ?? false); const [isPending, setIsPending] = useState(false); async function handleFollowToggle(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); if (isPending) return; const next = !isFollowing; // Optimistic update setIsFollowing(next); setIsPending(true); try { onFollowChange?.(user.id, next); } finally { setIsPending(false); } } if (variant === 'compact') { return (

{user.displayName ?? user.username}

@{user.username}

); } // Full variant return (
{/* Header row */}

{user.displayName ?? user.username}

@{user.username}

{/* Bio */} {user.bio && (

{user.bio}

)} {/* Followers count */}
{formatFollowers(user.followersCount)} {' '} followers
); } export default UserCard;