-
Notifications
You must be signed in to change notification settings - Fork 1
/
layout.tsx
229 lines (200 loc) · 6.73 KB
/
layout.tsx
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
import React, { useContext, useEffect, useRef, useState } from 'react';
import { Bars3Icon } from '@heroicons/react/24/outline';
import { useRouter } from 'utils/router';
import cx from 'classnames';
import dynamic from 'next/dynamic';
import HuntInfoContext from 'components/context';
import MuseumTheme from 'components/themes/museum';
import NavbarLink from 'components/navbar';
import VolumeSlider from 'components/volume_slider';
import ResetLocalDatabaseButton from 'components/reset_local_database_button';
interface Props {
children?: React.ReactNode;
}
/**
* The layout component, rendered on every page. Controls the rendering of the
* navbar as well as global page styling (e.g. color schemes).
*/
const Layout: React.FC<Props> = ({ children }) => {
const router = useRouter();
const { huntInfo, userInfo, round } = useContext(HuntInfoContext);
const [isNavbarCollapsed, setNavbarCollapsed] = useState<boolean>(true);
const teamInfo = userInfo?.teamInfo;
const [isScrolled, setScrolled] = useState<boolean>(false);
const basePath = router.asPath.split('#')[0]; // grab the path before hash
useEffect(() => {
const onScroll = () => void setScrolled(window.scrollY > 0);
document.addEventListener('scroll', onScroll);
return function cleanup() {
document.removeEventListener('scroll', onScroll);
};
}, []);
const handleRouteChange = (url: string) => {
setNavbarCollapsed(true);
};
useEffect(() => {
router.events.on('hashChangeComplete', handleRouteChange);
router.events.on('routeChangeComplete', handleRouteChange);
return () => {
router.events.off('hashChangeComplete', handleRouteChange);
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [router.events]);
// Customize styles based on round.
const themeStyle = <MuseumTheme roundSlug={round.slug} />;
const prehunt = huntInfo.secondsToStartTime > 0;
let loginUrl = '/login';
if (!['/login', ''].includes(router.pathname.replace(/\/$/, ''))) {
loginUrl += `?next=${encodeURIComponent(router.asPath)}`;
}
return (
<>
{/* gradient should not be clickable */}
<div className="nav-container pointer-events-none fixed top-0">
<nav
className={cx('select-none print:hidden', {
scrolled: isScrolled,
collapsed: isNavbarCollapsed,
})}
>
<button
className="border-none md:hidden collapse-menu mt-2 print:hidden"
aria-expanded={!isNavbarCollapsed}
onClick={() => setNavbarCollapsed((collapsed) => !collapsed)}
>
<Bars3Icon className="h-6 w-6" />
</button>
<div className="nav-list">
<ul className="inline-flex">
<NavbarLink href="/" linkText="Home" />
{!(prehunt && !userInfo?.superuser) && teamInfo?.rounds && (
<NavbarLink
linkText="Rounds"
dropdownItems={
teamInfo?.rounds?.map((act) =>
act.map((roundData) => ({
href: roundData.url,
linkText: roundData.name,
}))
) ?? []
}
/>
)}
<NavbarLink
linkText="Hunt"
dropdownItems={[
...(prehunt && !userInfo?.superuser
? []
: [[{ href: '/puzzles', linkText: 'List of Puzzles' }]]),
[
{ href: '/story', linkText: 'Story' },
{ href: '/about', linkText: 'About' },
{ href: '/events', linkText: 'Events' },
{ href: '/sponsors', linkText: 'Sponsors' },
{
href: '/health_and_safety',
linkText: 'Health & Safety',
},
// FIXME: Enable as needed
/*{
href:
router.nextRouter.basePath + '/spoilr/progress/solves',
linkText: 'Hunt Stats',
},
{ href: '/credits', linkText: 'Credits' },
*/
],
]}
/>
{userInfo?.unlocks?.map((unlock) => (
<NavbarLink
key={unlock.url}
href={unlock.url}
linkText={unlock.pageName}
/>
))}
{!!teamInfo ? (
<>
{!process.env.isArchive && (
<NavbarLink
linkText={teamInfo.name}
truncate
dropdownItems={[
[{ href: '/logout', linkText: 'Logout' }],
]}
/>
)}
</>
) : (
<NavbarLink href={loginUrl} linkText="Login" />
)}
{process.env.useWorker && <ResetLocalDatabaseButton />}
<div className="grow" />
<VolumeSlider />
</ul>
</div>
</nav>
</div>
{children}
<style jsx>{`
.nav-container {
left: 0;
right: 0;
z-index: 1000; /* Show above other content */
}
nav {
background: var(--navbar);
pointer-events: initial;
}
ul {
margin: 0 16px;
width: calc(100% - 32px);
}
:global(#__next) {
min-height: calc(100vh - 16px);
}
/* Responsive navbar: show on small devices */
@media (max-width: 800px) {
.nav-container {
position: fixed;
}
nav {
background-color: transparent;
}
nav :global(.menu) {
position: initial;
}
ul {
position: absolute;
padding-inline: 6px;
background-color: var(--theme-translucent);
border: 1px solid var(--on-primary);
color: var(--link);
flex-direction: column;
width: min-content;
}
nav :global(li) {
margin-left: 8px;
}
.nav-list {
margin-top: 50px;
}
.collapsed .nav-list {
display: none;
}
.collapse-menu {
background-color: var(--navbar);
color: var(--link);
padding: 8px 16px !important;
position: absolute;
margin-left: 16px;
top: 0;
line-height: 0;
}
}
`}</style>
{themeStyle}
</>
);
};
export default Layout;