Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

ScoreBoard.tsx

123 lines3.4 KB

1import { useEffect, useRef, useState } from "react"
2import { TEAMS, TEAM_COLORS } from "../../../shared/teams"
3import { useQuota } from "../../contexts/quota-context"
4import type { CSSProperties } from "react"
5import type { TeamName } from "../../../shared/teams"
6import "./ScoreBoard.css"
7​
8export interface ScoreBoardProps {
9 scores?: [number, number, number, number]
10 className?: string
12​
13interface TeamScore {
14 team: TeamName
15 score: number
16 color: string
18​
19/** Animated score value that glows when it changes */
20function AnimatedScore({
21 value,
22 color,
23 className = ``,
24}: {
25 value: number
26 color?: string
27 className?: string
28}) {
29 const [glowKey, setGlowKey] = useState(0)
30 const prevValueRef = useRef(value)
31 const isFirstRender = useRef(true)
32​
33 useEffect(() => {
34 // Skip animation on first render
35 if (isFirstRender.current) {
36 isFirstRender.current = false
37 prevValueRef.current = value
38 return
39 }
40​
41 // Trigger glow when value changes by incrementing key
42 if (value !== prevValueRef.current) {
43 prevValueRef.current = value
44 setGlowKey((k) => k + 1)
45 }
46 }, [value])
47​
48 const formattedValue = value.toLocaleString()
49​
50 return (
51 <span
52 className={`scoreboard-value ${className}`.trim()}
53 style={color ? { [`--glow-color` as string]: color } : undefined}
54 >
55 {formattedValue}
56 {glowKey > 0 && (
57 <span key={glowKey} className="scoreboard-glow-overlay" aria-hidden="true">
58 {formattedValue}
59 </span>
60 )}
61 </span>
62 )
64​
65export function ScoreBoard({ scores = [0, 0, 0, 0], className = `` }: ScoreBoardProps) {
66 const { localBoxesCompleted } = useQuota()
67​
68 // Calculate if there are any scores to determine sorting
69 const totalClaimed = scores.reduce((a, b) => a + b, 0)
70 const hasScores = totalClaimed > 0
71​
72 // Create team scores - sort by score if there are any, otherwise keep original order
73 const teamScores: Array<TeamScore> = TEAMS.map((team, i) => ({
74 team,
75 score: scores[i],
76 color: TEAM_COLORS[team].primary,
77 }))
78​
79 // Only sort if there are actual scores
80 if (hasScores) {
81 teamScores.sort((a, b) => b.score - a.score)
82 }
83​
84 return (
85 <div className={`scoreboard ${className}`.trim()} data-testid="scoreboard">
86 {/* Local user's score with person icon */}
87 <div
88 className="scoreboard-item scoreboard-local"
89 data-testid="score-local"
90 title="Your completed boxes"
91 >
92 <svg
93 className="scoreboard-icon"
94 width="12"
95 height="12"
96 viewBox="0 0 24 24"
97 fill="currentColor"
98 >
99 <circle cx="12" cy="7" r="4" />
100 <path d="M12 14c-6 0-9 3-9 6v1h18v-1c0-3-3-6-9-6z" />
101 </svg>
102 <AnimatedScore
103 value={localBoxesCompleted}
104 className="scoreboard-value-local"
105 color="rgba(120, 120, 120, 0.8)"
106 />
107 </div>
108 <span className="scoreboard-divider" />
109 {teamScores.map(({ team, score, color }) => (
110 <div
111 key={team}
112 className="scoreboard-item scoreboard-team"
113 data-testid={`score-${team.toLowerCase()}`}
114 style={{ "--score-color": color } as CSSProperties}
115 title={`${team.charAt(0) + team.slice(1).toLowerCase()} team: ${score.toLocaleString()} boxes`}
116 >
117 <span className="scoreboard-indicator" style={{ background: color }} />
118 <AnimatedScore value={score} color={color} />
119 </div>
120 ))}
121 </div>
122 )