Open History to see the last commit
1import { useEffect, useRef, useState } from "react"
2import type { CSSProperties } from "react"
3import { Dialog } from "@base-ui/react/dialog"
4import { TEAMS, TEAM_COLORS } from "../../../shared/teams"
5import { useTeam } from "../../contexts/team-context"
6import {
7 MAX_QUOTA,
8 QUOTA_BONUS_DOUBLE,
9 QUOTA_BONUS_SINGLE,
10 QUOTA_REFILL_INTERVAL_MS,
11 TOTAL_BOX_COUNT,
12} from "../../lib/config"
13import type { TeamName } from "../../../shared/teams"
14import "./AboutDialog.css"
15
16export interface AboutDialogProps {
17 open: boolean
18 onOpenChange: (open: boolean) => void
19 scores?: [number, number, number, number]
20 isLoading?: boolean
21}
22
23interface TeamScore {
24 team: TeamName
25 score: number
26 color: string
27}
28
29export function AboutDialog({
30 open,
31 onOpenChange,
32 scores = [0, 0, 0, 0],
33 isLoading = false,
34}: AboutDialogProps) {
35 const { team: userTeam } = useTeam()
36 const userTeamColor = TEAM_COLORS[userTeam].primary
37 const contentRef = useRef<HTMLDivElement>(null)
38 const [hasOverflowTop, setHasOverflowTop] = useState(false)
39 const [hasOverflowBottom, setHasOverflowBottom] = useState(false)
40 const resizeObserverRef = useRef<ResizeObserver | null>(null)
41
42 // Check scroll position and overflow state
43 useEffect(() => {
44 if (!open) {
45 setHasOverflowTop(false)
46 setHasOverflowBottom(false)
47 resizeObserverRef.current?.disconnect()
48 return
49 }
50
51 // Wait for portal to mount, then set up observers
52 const timeoutId = setTimeout(() => {
53 const content = contentRef.current
54 if (!content) return
55
56 // Ensure dialog starts at top on open
57 content.scrollTop = 0
58
59 const checkOverflow = () => {
60 const { scrollTop, scrollHeight, clientHeight } = content
61 // Has content hidden above?
62 setHasOverflowTop(scrollTop > 1)
63 // Has content hidden below? (1px threshold for rounding)
64 setHasOverflowBottom(scrollTop + clientHeight < scrollHeight - 1)
65 }
66
67 // Check immediately
68 checkOverflow()
69
70 // Listen for scroll events
71 content.addEventListener(`scroll`, checkOverflow, { passive: true })
72
73 // Use ResizeObserver to detect size changes
74 resizeObserverRef.current = new ResizeObserver(checkOverflow)
75 resizeObserverRef.current.observe(content)
76
77 // Store scroll listener cleanup
78 return () => {
79 content.removeEventListener(`scroll`, checkOverflow)
80 }
81 }, 50)
82
83 return () => {
84 clearTimeout(timeoutId)
85 resizeObserverRef.current?.disconnect()
86 contentRef.current?.removeEventListener(`scroll`, () => {})
87 }
88 }, [open])
89
90 // Calculate claimed and remaining
91 const totalClaimed = scores.reduce((a, b) => a + b, 0)
92 const remaining = TOTAL_BOX_COUNT - totalClaimed
93
94 // Create team scores sorted by score (descending)
95 const hasScores = totalClaimed > 0
96 const teamScores: Array<TeamScore> = TEAMS.map((team, i) => ({
97 team,
98 score: scores[i],
99 color: TEAM_COLORS[team].primary,
100 }))
101
102 if (hasScores) {
103 teamScores.sort((a, b) => b.score - a.score)
104 }
105
106 const refillSeconds = QUOTA_REFILL_INTERVAL_MS / 1000
107
108 // Format team name for display (capitalize first letter only)
109 const teamDisplayName = userTeam.charAt(0) + userTeam.slice(1).toLowerCase()
110
111 // Yellow team needs dark text for contrast
112 const buttonTextColor = userTeam === `YELLOW` ? `#333` : `white`
113
114 return (
115 <Dialog.Root open={open} onOpenChange={onOpenChange}>
116 <Dialog.Portal>
117 <Dialog.Backdrop className="about-dialog-backdrop" />
118 <Dialog.Popup className="about-dialog-popup">
119 <Dialog.Title className={`about-dialog-title${hasOverflowTop ? ` has-overflow` : ``}`}>
120 <img src="/logo.svg" alt="1 Million Boxes" className="about-dialog-logo" />
121 </Dialog.Title>
122
123 <div className="about-dialog-content" ref={contentRef}>
124 <p>
125 A massive multiplayer{` `}
126 <a
127 href="https://en.wikipedia.org/wiki/Dots_and_boxes"
128 target="_blank"
129 rel="noopener noreferrer"
130 >
131 Dots & Boxes
132 </a>
133 {` `}
134 game with{` `}
135 <strong>1,000,000 boxes</strong> on a 1000×1000 grid.
136 </p>
137
138 <h3 className="about-dialog-section-title">How to Play</h3>
139 <ul className="about-dialog-rules">
140 <li>
141 <span className="desktop-only">Click</span>
142 <span className="mobile-only">Tap</span> between two dots to draw a line
143 </li>
144 <li>Complete all four sides of a box to claim it for your team</li>
145 <li>The team with the most boxes wins!</li>
146 </ul>
147
148 <h3 className="about-dialog-section-title">Moves & refills</h3>
149 <ul className="about-dialog-rules">
150 <li>
151 You have up to <strong>{MAX_QUOTA} moves</strong> available at a time
152 </li>
153 <li>
154 Moves refill by <strong>1 every {refillSeconds} seconds</strong>
155 </li>
156 <li>
157 <strong>Bonus:</strong> Completing a box gives you{` `}
158 <strong>+{QUOTA_BONUS_SINGLE} moves</strong> (refund + bonus)
159 </li>
160 <li>
161 Complete 2 boxes with one line? Get{` `}
162 <strong>+{QUOTA_BONUS_DOUBLE} moves</strong> back!
163 </li>
164 </ul>
165
166 <h3 className="about-dialog-section-title">Team Leaderboard</h3>
167 <div className="about-dialog-leaderboard">
168 {isLoading
169 ? TEAMS.map((team, index) => (
170 <div
171 key={team}
172 className="about-leaderboard-item about-leaderboard-item--loading"
173 >
174 <span className="about-leaderboard-rank">{index + 1}</span>
175 <span className="about-leaderboard-dot about-skeleton" />
176 <span className="about-leaderboard-team about-skeleton about-skeleton-text" />
177 <span className="about-leaderboard-score about-skeleton about-skeleton-number" />
178 </div>
179 ))
180 : teamScores.map(({ team, score, color }, index) => (
181 <div
182 key={team}
183 className="about-leaderboard-item"
184 style={{ "--score-color": color } as CSSProperties}
185 >
186 <span className="about-leaderboard-rank">{index + 1}</span>
187 <span className="about-leaderboard-dot" style={{ background: color }} />
188 <span className="about-leaderboard-team">
189 {team.charAt(0) + team.slice(1).toLowerCase()}
190 </span>
191 <span className="about-leaderboard-score">{score.toLocaleString()}</span>
192 </div>
193 ))}
194 </div>
195
196 <div className="about-dialog-stats">
197 <div className="about-stat">
198 {isLoading ? (
199 <span className="about-stat-value about-skeleton about-skeleton-number" />
200 ) : (
201 <span className="about-stat-value">{remaining.toLocaleString()}</span>
202 )}
203 <span className="about-stat-label">boxes available</span>
204 </div>
205 <div className="about-stat">
206 {isLoading ? (
207 <span className="about-stat-value about-skeleton about-skeleton-number" />
208 ) : (
209 <span className="about-stat-value">{totalClaimed.toLocaleString()}</span>
210 )}
211 <span className="about-stat-label">boxes claimed</span>
212 </div>
213 </div>
214
215 <p className="about-dialog-tech">
216 Built with{` `}
217 <a
218 href="https://neon.com/docs/realtime/overview"
219 target="_blank"
220 rel="noopener noreferrer"
221 >
222 Neon Realtime
223 </a>
224 {` `}for real-time synchronization,{` `}
225 <a
226 href="https://github.com/samwillis/one-million-boxes"
227 target="_blank"
228 rel="noopener noreferrer"
229 >
230 learn how we built it
231 </a>
232 .
233 </p>
234 </div>
235
236 <div
237 className={`about-dialog-button-container${hasOverflowBottom ? ` has-overflow` : ``}`}
238 >
239 <Dialog.Close
240 className="about-dialog-close-button"
241 style={{
242 background: userTeamColor,
243 color: buttonTextColor,
244 }}
245 >
246 You're on the {teamDisplayName} team, Play!
247 </Dialog.Close>
248 </div>
249 </Dialog.Popup>
250 </Dialog.Portal>
251 </Dialog.Root>
252 )
253}