'use client' import { strokesReceived, formatVsPar } from '@/lib/handicap' import type { Hole, Player } from '@/app/(app)/rounds/[id]/scorecard/page' type Props = { holes: Hole[] players: Player[] scores: Record } export function LeaderboardView({ holes, players, scores }: Props) { const totalHoles = holes.length const standings = players .map((player) => { let grossTotal = 0 let netTotal = 0 let holesPlayed = 0 let parPlayed = 0 for (const hole of holes) { const gross = scores[`${player.user_id}-${hole.id}`] if (gross !== undefined) { const ch = player.course_handicap ?? 0 const net = gross - strokesReceived(ch, hole.stroke_index, totalHoles) grossTotal += gross netTotal += net parPlayed += hole.par holesPlayed++ } } const netVsPar = holesPlayed > 0 ? netTotal - parPlayed : null const grossVsPar = holesPlayed > 0 ? grossTotal - parPlayed : null return { player, grossTotal, netTotal, holesPlayed, netVsPar, grossVsPar, } }) .sort((a, b) => { if (a.netVsPar === null && b.netVsPar === null) return 0 if (a.netVsPar === null) return 1 if (b.netVsPar === null) return -1 return a.netVsPar - b.netVsPar || a.grossVsPar! - b.grossVsPar! }) return (
{standings.map(({ player, grossTotal, netVsPar, grossVsPar, holesPlayed }, i) => { const isLead = i === 0 && netVsPar !== null return (
{/* Position */}
{holesPlayed > 0 ? i + 1 : '—'}
{/* Avatar */} {/* Name + holes played */}
{player.profile?.display_name}
{holesPlayed > 0 ? `${holesPlayed} of ${totalHoles} holes` : 'No scores yet'} {player.course_handicap !== null && ` · HCP ${player.course_handicap}`}
{/* Net score (primary, bold) */}
{netVsPar !== null ? formatVsPar(netVsPar) : '—'} {grossVsPar !== null && ( {grossTotal} gross )}
) })}
) } function PlayerAvatar({ name, url }: { name: string; url?: string | null }) { const initials = name .split(' ') .map((n) => n[0]) .join('') .toUpperCase() .slice(0, 2) if (url) { return {name} } return (
{initials}
) }