-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathHeader.tsx
More file actions
485 lines (468 loc) · 17.9 KB
/
Header.tsx
File metadata and controls
485 lines (468 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import Link from 'next/link'
import Image from 'next/image'
import { PlayType } from 'src/types'
import { useRouter } from 'next/router'
import { DiscordIcon } from './Icons'
import { useCallback, useContext, useEffect, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { AuthContext, ModalContext, WindowSizeContext } from 'src/contexts'
import { LeaderboardNavBadge } from '../Leaderboard/LeaderboardNavBadge'
import { useLeaderboardStatus } from 'src/hooks/useLeaderboardStatus'
export const Header: React.FC = () => {
const [showMenu, setShowMenu] = useState(false)
const [showPlayDropdown, setShowPlayDropdown] = useState(false)
const [showMoreDropdown, setShowMoreDropdown] = useState(false)
const [showProfileDropdown, setShowProfileDropdown] = useState(false)
const { isMobile, width } = useContext(WindowSizeContext)
const { user, connectLichess, logout } = useContext(AuthContext)
// Get leaderboard status for the logged in user
const { status: leaderboardStatus, loading: leaderboardLoading } =
useLeaderboardStatus(user?.displayName)
const router = useRouter()
const { setPlaySetupModalProps } = useContext(ModalContext)
const isCompactDesktopNav = !isMobile && width > 0 && width < 1300
const startGame = useCallback(
(playType: PlayType) => {
if (
typeof document !== 'undefined' &&
document.activeElement instanceof HTMLElement
) {
document.activeElement.blur()
}
setPlaySetupModalProps({ playType: playType })
},
[setPlaySetupModalProps],
)
// Close play dialog if page closed
useEffect(
() => () => setPlaySetupModalProps(undefined),
[setPlaySetupModalProps],
)
useEffect(() => {
const handleRouteChange = () => {
setShowMenu(false)
}
router.events.on('routeChangeStart', handleRouteChange)
// If the component is unmounted, unsubscribe
// from the event with the `off` method:
return () => {
router.events.off('routeChangeStart', handleRouteChange)
}
}, [router])
useEffect(() => {
if (showMenu) {
document.body.style.overflow = 'hidden'
} else {
document.body.style.overflow = 'unset'
}
return () => {
document.body.style.overflow = 'unset'
}
}, [showMenu])
const userInfo = user?.lichessId ? (
<div
className="relative flex items-center gap-2 rounded-full border border-glass-border bg-backdrop px-3 py-1.5 transition-all duration-200 hover:border-white/20"
onMouseEnter={() => setShowProfileDropdown(true)}
onMouseLeave={() => setShowProfileDropdown(false)}
>
<span className="material-symbols-outlined text-xl text-primary/80">
account_circle
</span>
<span className="text-sm font-medium text-primary/90">
{user?.displayName}
</span>
<motion.i
className="material-symbols-outlined text-sm text-primary/60"
animate={{ rotate: showProfileDropdown ? 180 : 0 }}
transition={{ duration: 0.2 }}
>
arrow_drop_down
</motion.i>
<AnimatePresence>
{showProfileDropdown && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.2 }}
className="absolute bottom-[100%] left-0 z-50 w-full overflow-hidden rounded-md border border-glass-border bg-backdrop md:bottom-auto md:top-[100%]"
>
<div className="divide-y divide-glass-border tracking-wide">
<Link
href="/profile"
className="flex w-full items-center justify-start px-3 py-2 text-sm text-primary transition-colors hover:bg-glass"
>
Profile
</Link>
<Link
href="/settings"
className="flex w-full items-center justify-start px-3 py-2 text-sm text-primary transition-colors hover:bg-glass"
>
Settings
</Link>
<button
onClick={logout}
className="flex w-full items-center justify-start px-3 py-2 text-sm text-primary transition-colors hover:bg-glass"
>
Logout
</button>
</div>
</motion.div>
)}
</AnimatePresence>
</div>
) : (
<button
onClick={connectLichess}
className="px-2 py-1 text-sm tracking-wide text-primary/80 transition-all duration-200 hover:text-primary"
>
Sign in
</button>
)
const desktopLayout = (
<div className="flex w-[90%] flex-row items-center justify-between">
<div className="flex flex-row items-center justify-start gap-6">
<Link href="/" className="flex flex-row items-center gap-2">
<Image src="/maia.png" width={40} height={40} alt="Maia Logo" />
<h2 className="text-2xl font-bold">Maia Chess</h2>
</Link>
<div className="hidden flex-row gap-1 text-sm tracking-wider md:flex">
<div
className="relative"
onMouseEnter={() => setShowPlayDropdown(true)}
onMouseLeave={() => setShowPlayDropdown(false)}
>
<button
className={`-gap-1 flex items-center px-2 py-1 transition-all duration-200 hover:!text-primary ${router.pathname.startsWith('/play') ? '!text-primary' : '!text-primary/80'}`}
>
<p>PLAY</p>
<motion.i
className="material-symbols-outlined text-sm"
animate={{ rotate: showPlayDropdown ? 180 : 0 }}
transition={{ duration: 0.2 }}
>
arrow_drop_down
</motion.i>
</button>
<AnimatePresence>
{showPlayDropdown && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.2 }}
className="absolute left-0 top-[100%] z-30 w-48 overflow-hidden rounded-md border border-glass-border bg-backdrop"
>
<div className="divide-y divide-glass-border tracking-wide">
<button
onClick={() => startGame('againstMaia')}
className="flex w-full items-center justify-start px-3 py-2 text-sm text-primary transition-colors hover:bg-glass"
>
Play Maia
</button>
<button
onClick={() => startGame('handAndBrain')}
className="flex w-full items-center justify-start px-3 py-2 text-sm text-primary transition-colors hover:bg-glass"
>
Play Hand and Brain
</button>
<a
className="flex w-full items-center justify-start px-3 py-2 text-sm text-primary transition-colors hover:bg-glass"
href="https://lichess.org/@/maia1"
target="_blank"
rel="noreferrer"
>
Play Maia on Lichess
</a>
</div>
</motion.div>
)}
</AnimatePresence>
</div>
<Link
href="/analysis"
className={`px-2 py-1 transition-all duration-200 hover:!text-primary ${router.pathname.startsWith('/analysis') ? '!text-primary' : '!text-primary/80'}`}
>
ANALYSIS
</Link>
<Link
href="/puzzles"
className={`px-2 py-1 transition-all duration-200 hover:!text-primary ${router.pathname.startsWith('/puzzles') ? '!text-primary' : '!text-primary/80'}`}
>
PUZZLES
</Link>
<Link
href="/drills"
className={`px-2 py-1 transition-all duration-200 hover:!text-primary ${router.pathname.startsWith('/drills') ? '!text-primary' : '!text-primary/80'}`}
>
DRILLS
</Link>
<Link
href="/turing"
className={`px-2 py-1 transition-all duration-200 hover:!text-primary ${router.pathname.startsWith('/turing') ? '!text-primary' : '!text-primary/80'}`}
>
BOT-OR-NOT
</Link>
{!isCompactDesktopNav && (
<Link
href="/broadcast"
className={`px-2 py-1 transition-all duration-200 hover:!text-primary ${router.pathname.startsWith('/broadcast') ? '!text-primary' : '!text-primary/80'}`}
>
BROADCASTS
</Link>
)}
<div
className="relative"
onMouseEnter={() => setShowMoreDropdown(true)}
onMouseLeave={() => setShowMoreDropdown(false)}
>
<button className="-gap-1 flex items-center px-2 py-1 !text-primary/80 transition-all duration-200 hover:!text-primary">
<p>MORE</p>
<motion.i
className="material-symbols-outlined text-sm"
animate={{ rotate: showMoreDropdown ? 180 : 0 }}
transition={{ duration: 0.2 }}
>
arrow_drop_down
</motion.i>
</button>
<AnimatePresence>
{showMoreDropdown && (
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.2 }}
className="absolute left-0 top-[100%] z-30 w-32 overflow-hidden rounded-md border border-glass-border bg-backdrop"
>
<div className="divide-y divide-glass-border tracking-wide">
{isCompactDesktopNav && (
<Link
href="/broadcast"
className={`flex w-full items-center justify-start px-3 py-2 text-sm transition-colors hover:bg-glass ${
router.pathname.startsWith('/broadcast')
? 'text-primary'
: 'text-white/90'
}`}
>
Broadcasts
</Link>
)}
<Link
href="/leaderboard"
className={`flex w-full items-center justify-start px-3 py-2 text-sm transition-colors hover:bg-glass ${
router.pathname.startsWith('/leaderboard')
? 'text-primary'
: 'text-white/90'
}`}
>
Leaderboard
</Link>
<Link
href="/blog"
className="flex w-full items-center justify-start px-3 py-2 text-sm text-white/90 transition-colors hover:bg-glass"
>
Maia Blog
</Link>
{/* <a
target="_blank"
rel="noreferrer"
href="https://twitch.tv/maiachess"
className="flex w-full items-center justify-start px-3 py-2 text-sm text-white/90 transition-colors hover:bg-glass"
>
Watch
</a> */}
{/* <a
target="_blank"
rel="noreferrer"
href="https://forms.gle/XYeoTJF4YgUu4Vq28"
className="flex w-full items-center justify-start px-3 py-2 text-sm text-white/90 transition-colors hover:bg-glass"
>
Feedback
</a> */}
</div>
</motion.div>
)}
</AnimatePresence>
</div>
</div>
</div>
<div className="hidden flex-row items-center gap-3 md:flex">
<a
target="_blank"
rel="noreferrer"
href="https://discord.gg/Az93GqEAs7"
>
{DiscordIcon}
</a>
{user?.lichessId && (
<LeaderboardNavBadge
status={leaderboardStatus}
loading={leaderboardLoading}
/>
)}
{userInfo}
</div>
</div>
)
const mobileLayout = (
<div className="flex w-full flex-row justify-between px-4">
<Link href="/" passHref>
<div className="flex flex-row items-center gap-2">
<Image src="/maia.png" width={40} height={40} alt="Maia Logo" />
<h2 className="text-2xl font-bold">Maia Chess</h2>
</div>
</Link>
<button
aria-label="Open navigation menu"
className="block cursor-pointer *:*:fill-primary"
onClick={() => setShowMenu((show) => !show)}
>
<span className="material-symbols-outlined text-3xl">menu</span>
</button>
{showMenu && (
<div className="fixed left-0 top-0 z-40 flex h-screen w-screen flex-col items-start justify-between overflow-y-auto bg-backdrop py-4">
<div className="flex w-full flex-row justify-between px-4">
<Link href="/" passHref>
<div className="flex flex-row items-center gap-2">
<Image src="/maia.png" width={40} height={40} alt="Maia Logo" />
<h2 className="text-2xl font-bold">Maia Chess</h2>
</div>
</Link>
<button
aria-label="Close navigation menu"
className="block cursor-pointer *:*:fill-primary"
onClick={() => setShowMenu(false)}
>
<span className="material-symbols-outlined text-3xl">menu</span>
</button>
</div>
<div className="flex w-full flex-1 flex-col gap-5 overflow-y-auto px-12 py-6 tracking-wider">
<div className="flex flex-col items-start justify-center gap-3">
<button>PLAY</button>
<div className="ml-4 flex flex-col items-start justify-center gap-2">
<button
onClick={() => {
setShowMenu(false)
startGame('againstMaia')
}}
>
Play Maia
</button>
<button
onClick={() => {
setShowMenu(false)
startGame('handAndBrain')
}}
>
Play Hand and Brain
</button>
<a
href="https://lichess.org/@/maia1"
target="_blank"
rel="noreferrer"
onClick={() => setShowMenu(false)}
>
Play Maia on Lichess
</a>
</div>
</div>
<Link href="/analysis" className="uppercase">
Analysis
</Link>
<Link href="/puzzles" className="uppercase">
Puzzles
</Link>
<Link href="/drills" className="uppercase">
Drills
</Link>
<Link href="/turing" className="uppercase">
Bot-or-not
</Link>
<Link href="/broadcast" className="uppercase">
Broadcasts
</Link>
<Link href="/leaderboard" className="uppercase">
Leaderboard
</Link>
<Link href="/blog" className="uppercase">
Maia Blog
</Link>
<a
target="_blank"
rel="noreferrer"
href="https://discord.gg/Az93GqEAs7"
className="uppercase"
onClick={() => setShowMenu(false)}
>
Discord
</a>
{/* <a
target="_blank"
rel="noreferrer"
href="https://twitch.tv/maiachess"
className="uppercase"
>
Watch
</a> */}
{/* <a
target="_blank"
rel="noreferrer"
href="https://forms.gle/XYeoTJF4YgUu4Vq28"
className="uppercase"
>
Feedback
</a> */}
{user?.lichessId && (
<>
<Link href="/profile" className="uppercase">
Profile
</Link>
<Link href="/settings" className="uppercase">
Settings
</Link>
<button onClick={logout} className="text-left uppercase">
Logout
</button>
</>
)}
</div>
<div className="flex w-full flex-row items-center gap-3 px-12">
{user?.lichessId && (
<LeaderboardNavBadge
status={leaderboardStatus}
loading={leaderboardLoading}
/>
)}
{user?.lichessId ? (
<div className="flex items-center gap-2">
<span className="material-symbols-outlined text-2xl text-primary/80">
account_circle
</span>
<span className="text-lg font-medium tracking-wider text-primary/90">
{user?.displayName}
</span>
</div>
) : (
<button onClick={connectLichess} className="uppercase">
Sign in
</button>
)}
</div>
</div>
)}
</div>
)
return (
<>
<div
data-component="app-header"
className="flex w-screen flex-row items-center justify-center pb-1 pt-4 md:pb-0"
>
{isMobile ? mobileLayout : desktopLayout}
</div>
</>
)
}