'use client' import { type ReactElement } from 'react' import Link from 'next/link' import Image from 'next/image' import { useQuery } from '@tanstack/react-query' import { userService } from '@/services/userService' import { useAuthStore } from '@/store/authStore' import { Tooltip } from './ui/tooltip' import { FollowButton } from './FollowButton' interface HoverProfile { id: string username: string displayName?: string bio?: string avatar?: string followersCount: number followingCount: number isFollowing: boolean githubUsername?: string } function normalize(raw: unknown): HoverProfile { const r = (raw ?? {}) as Record return { id: String(r.id ?? ''), username: String(r.username ?? ''), displayName: (r.displayName as string) ?? (r.fullName as string) ?? undefined, bio: r.bio ? String(r.bio) : undefined, avatar: (r.avatar as string) ?? (r.avatarUrl as string) ?? undefined, followersCount: typeof r.followersCount === 'number' ? r.followersCount : 0, followingCount: typeof r.followingCount === 'number' ? r.followingCount : 0, isFollowing: Boolean(r.isFollowing), githubUsername: r.githubUsername ? String(r.githubUsername) : undefined, } } function formatCount(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) } interface UserHoverCardProps { /** Username to preview. The card lazily fetches the full profile on hover. */ username: string /** Trigger element (a link, avatar, etc.). */ children: ReactElement } /** * Wraps a trigger element with a profile-preview hover card showing the user's * avatar, display name, bio, follower/following counts, and a follow button. * * Adapted from the source UserTooltip/UserLinkWithTooltip pair onto the * gitgram-web stack: data comes from `userService.getProfile` via React Query * (fetched lazily once on first hover and cached), the popover uses this * product's `ui/tooltip` primitive, and follow state reuses the existing * `FollowButton`. */ export function UserHoverCard({ username, children }: UserHoverCardProps) { const { user: currentUser } = useAuthStore() const { data, status } = useQuery({ queryKey: ['hover-profile', username], queryFn: async () => normalize(await userService.getProfile(username)), staleTime: 5 * 60 * 1000, enabled: Boolean(username), retry: false, }) const isSelf = Boolean(currentUser?.username && currentUser.username === username) const card = ( {status === 'pending' && (