Open History to see the last commit
1import { useCallback, useEffect, useRef, useState } from "react"
2import { GAME_START_TIMESTAMP_KEY } from "../../../shared/game-config"
3import { getDebugConfig, setDebugConfig, subscribeDebugConfig } from "../../lib/debug-config"
4import type { DebugConfig } from "../../lib/debug-config"
5import "./DebugOverlay.css"
6
7/**
8 * Debug overlay showing FPS counter and rendering toggles.
9 * Press 'D' to toggle debug mode.
10 */
11export function DebugOverlay() {
12 const [config, setConfig] = useState<DebugConfig>(getDebugConfig)
13 const [fps, setFps] = useState(0)
14 const [gameStartTime, setGameStartTime] = useState<string | null>(null)
15 const frameTimesRef = useRef<Array<number>>([])
16 const lastTimeRef = useRef(performance.now())
17 const rafIdRef = useRef<number>(0)
18
19 // Subscribe to config changes
20 useEffect(() => {
21 return subscribeDebugConfig(setConfig)
22 }, [])
23
24 // Load game start timestamp from localStorage
25 useEffect(() => {
26 if (!config.showFps) return
27
28 const loadTimestamp = () => {
29 try {
30 const stored = localStorage.getItem(GAME_START_TIMESTAMP_KEY)
31 if (stored) {
32 const timestamp = parseInt(stored, 10)
33 const date = new Date(timestamp)
34 setGameStartTime(date.toISOString())
35 } else {
36 setGameStartTime(null)
37 }
38 } catch {
39 setGameStartTime(null)
40 }
41 }
42
43 loadTimestamp()
44 // Refresh periodically in case it changes
45 const interval = setInterval(loadTimestamp, 1000)
46 return () => clearInterval(interval)
47 }, [config.showFps])
48
49 // FPS measurement
50 useEffect(() => {
51 if (!config.showFps) {
52 if (rafIdRef.current) {
53 cancelAnimationFrame(rafIdRef.current)
54 rafIdRef.current = 0
55 }
56 return
57 }
58
59 const measureFps = () => {
60 const now = performance.now()
61 const delta = now - lastTimeRef.current
62 lastTimeRef.current = now
63
64 // Keep last 60 frame times
65 frameTimesRef.current.push(delta)
66 if (frameTimesRef.current.length > 60) {
67 frameTimesRef.current.shift()
68 }
69
70 // Calculate average FPS
71 const avgDelta =
72 frameTimesRef.current.reduce((a, b) => a + b, 0) / frameTimesRef.current.length
73 setFps(Math.round(1000 / avgDelta))
74
75 rafIdRef.current = requestAnimationFrame(measureFps)
76 }
77
78 rafIdRef.current = requestAnimationFrame(measureFps)
79
80 return () => {
81 if (rafIdRef.current) {
82 cancelAnimationFrame(rafIdRef.current)
83 }
84 }
85 }, [config.showFps])
86
87 // Keyboard shortcut: D to toggle debug mode
88 useEffect(() => {
89 const handleKeyDown = (e: KeyboardEvent) => {
90 if (e.key === `d` || e.key === `D`) {
91 // Don't trigger if typing in an input
92 if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
93 return
94 }
95 setDebugConfig({ showFps: !getDebugConfig().showFps })
96 }
97 }
98
99 window.addEventListener(`keydown`, handleKeyDown)
100 return () => window.removeEventListener(`keydown`, handleKeyDown)
101 }, [])
102
103 const handleToggle = useCallback(
104 (key: keyof Omit<DebugConfig, `showFps`>) => {
105 setDebugConfig({ [key]: !config[key] })
106 },
107 [config],
108 )
109
110 if (!config.showFps) {
111 return null
112 }
113
114 return (
115 <div className="debug-overlay">
116 <div className="debug-fps">
117 <span className="debug-fps-value">{fps}</span>
118 <span className="debug-fps-label">FPS</span>
119 </div>
120
121 <div className="debug-toggles">
122 <label className="debug-toggle">
123 <input
124 type="checkbox"
125 checked={config.renderGridLines}
126 onChange={() => handleToggle(`renderGridLines`)}
127 />
128 Grid Lines
129 </label>
130 <label className="debug-toggle">
131 <input
132 type="checkbox"
133 checked={config.renderDots}
134 onChange={() => handleToggle(`renderDots`)}
135 />
136 Dots
137 </label>
138 <label className="debug-toggle">
139 <input
140 type="checkbox"
141 checked={config.renderDrawnLines}
142 onChange={() => handleToggle(`renderDrawnLines`)}
143 />
144 Drawn Lines
145 </label>
146 <label className="debug-toggle">
147 <input
148 type="checkbox"
149 checked={config.renderShadedBoxes}
150 onChange={() => handleToggle(`renderShadedBoxes`)}
151 />
152 Shaded Boxes
153 </label>
154 </div>
155
156 {gameStartTime && (
157 <div className="debug-info">
158 <span className="debug-info-label">Game Start:</span>
159 <span className="debug-info-value">{gameStartTime}</span>
160 </div>
161 )}
162
163 <div className="debug-hint">Press D to close</div>
164 </div>
165 )
166}