Open History to see the last commit
1import { useCallback, useState } from "react"
2import { useGameState } from "../../hooks/useGameState"
3import { TeamBadge } from "../ui/TeamBadge"
4import { ScoreBoard } from "../ui/ScoreBoard"
5import { ShareDialog } from "../ui/ShareDialog"
6import { AboutDialog } from "../ui/AboutDialog"
7import "./Header.css"
8
9const ABOUT_DISMISSED_KEY = `boxes:about-dismissed`
10
11export function Header() {
12 const [shareDialogOpen, setShareDialogOpen] = useState(false)
13 // Show about dialog on first load unless already dismissed this session
14 const [aboutDialogOpen, setAboutDialogOpen] = useState(() => {
15 try {
16 return sessionStorage.getItem(ABOUT_DISMISSED_KEY) !== `true`
17 } catch {
18 return true // Show if sessionStorage unavailable
19 }
20 })
21 // Explicitly depend on version to ensure re-render when game state changes
22 const { gameState, version, isLoading } = useGameState()
23 // Get current scores (will update when version changes)
24 const scores = gameState.getScores()
25 // Suppress unused variable warning - version is used to trigger re-renders
26 void version
27
28 // Handle about dialog close - store dismissal in sessionStorage
29 const handleAboutOpenChange = useCallback((open: boolean) => {
30 setAboutDialogOpen(open)
31 if (!open) {
32 try {
33 sessionStorage.setItem(ABOUT_DISMISSED_KEY, `true`)
34 } catch {
35 // Ignore if sessionStorage unavailable
36 }
37 }
38 }, [])
39
40 return (
41 <>
42 <header className="header" data-testid="header">
43 <div className="header-left">
44 <h1 className="header-title">
45 <img src="/logo.svg" alt="1 Million Boxes" className="header-logo header-logo-full" />
46 <img src="/logo-mini.svg" alt="1M Boxes" className="header-logo header-logo-mini" />
47 </h1>
48 <TeamBadge />
49 </div>
50
51 {!isLoading && <ScoreBoard scores={scores} className="header-scoreboard" />}
52
53 <div className="header-right">
54 <button
55 className="header-button"
56 onClick={() => handleAboutOpenChange(true)}
57 aria-label="About"
58 >
59 <svg
60 width="18"
61 height="18"
62 viewBox="0 0 24 24"
63 fill="none"
64 stroke="currentColor"
65 strokeWidth="2"
66 strokeLinecap="round"
67 strokeLinejoin="round"
68 >
69 <circle cx="12" cy="12" r="10" />
70 <line x1="12" y1="16" x2="12" y2="12" />
71 <line x1="12" y1="8" x2="12.01" y2="8" />
72 </svg>
73 <span className="header-button-text">About</span>
74 </button>
75
76 <button
77 className="header-button"
78 onClick={() => setShareDialogOpen(true)}
79 aria-label="Share"
80 >
81 <svg
82 width="18"
83 height="18"
84 viewBox="0 0 24 24"
85 fill="none"
86 stroke="currentColor"
87 strokeWidth="2"
88 strokeLinecap="round"
89 strokeLinejoin="round"
90 >
91 <circle cx="18" cy="5" r="3" />
92 <circle cx="6" cy="12" r="3" />
93 <circle cx="18" cy="19" r="3" />
94 <line x1="8.59" y1="13.51" x2="15.42" y2="17.49" />
95 <line x1="15.41" y1="6.51" x2="8.59" y2="10.49" />
96 </svg>
97 <span className="header-button-text">Share</span>
98 </button>
99 </div>
100
101 <AboutDialog
102 open={aboutDialogOpen}
103 onOpenChange={handleAboutOpenChange}
104 scores={scores}
105 isLoading={isLoading}
106 />
107 <ShareDialog open={shareDialogOpen} onOpenChange={setShareDialogOpen} />
108 </header>
109
110 {/* Floating scoreboard for mobile */}
111 {!isLoading && <ScoreBoard scores={scores} className="floating-scoreboard" />}
112 </>
113 )
114}