-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNavigation.tsx
251 lines (241 loc) · 10.9 KB
/
Navigation.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { SUPABASE } from '../supabase/supabaseClient';
import { Link } from 'react-router';
import { Session } from '../types';
import React, { useState, useEffect, MouseEvent } from 'react';
import type { Theme } from '@mui/material';
import AppBar from '@mui/material/AppBar';
import Divider from '@mui/material/Divider';
import IconButton from '@mui/material/IconButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import Toolbar from '@mui/material/Toolbar';
import Tooltip from '@mui/material/Tooltip';
import Typ from '@mui/material/Typography';
import AccountCircle from '@mui/icons-material/AccountCircle';
import DarkMode from '@mui/icons-material/DarkMode';
import ExpandLess from '@mui/icons-material/ExpandLess';
import ExpandMore from '@mui/icons-material/ExpandMore';
import Explore from '@mui/icons-material/Explore';
import LightMode from '@mui/icons-material/LightMode';
import Login from '@mui/icons-material/Login';
import Logout from '@mui/icons-material/Logout';
import PersonAddAlt from '@mui/icons-material/PersonAddAlt';
import Search from '@mui/icons-material/Search';
import Settings from '@mui/icons-material/Settings';
import SearchInput from './SearchInput';
import { lightTheme } from '../theme';
interface NavigationProps {
session: Session | null;
theme: Theme;
switchTheme: () => void;
}
/**
* This component will be rendered in AppWrapper.tsx - on every page.
* Navigation elements are placeholder for the time being for development purposes.
* @returns {JSX.Element} | Navigation
*/
const Navigation: React.FC<NavigationProps> = ({ session, theme, switchTheme }): JSX.Element => {
const [themeIcon, setThemeIcon] = useState(<DarkMode />);
const [anchorElUser, setAnchorElUser] = useState<null | HTMLElement>(null);
const [expandedMenu, setExpandedMenu] = useState(false);
const [expandedSearch, setExpandedSearch] = useState(false);
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const open = Boolean(anchorElUser);
// On component render, listen for changes to window size
useEffect(() => {
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
// Set theme icon according to theme from props
useEffect(() => {
if (theme === lightTheme) {
setThemeIcon(<LightMode className='mr-2' />);
} else {
setThemeIcon(<DarkMode className='mr-2' />);
}
}, [theme]);
// Anytime browser window changes size, check if expanded view should be shown
const handleResize = () => {
setWindowWidth(window.innerWidth);
if (windowWidth > 768) {
setExpandedSearch(false);
}
};
// Handle drop down menu visibility
const toggleUserMenu = (event: MouseEvent<HTMLElement> | null, open: boolean) => {
if (event) {
setAnchorElUser(event.currentTarget);
} else {
setAnchorElUser(null);
}
setExpandedMenu(open);
};
const logoutHandler = async () => {
// TODO: Error handling if any
await SUPABASE?.auth.signOut();
};
return (
// TODO: #162 Use MUI ThemeProvider
<AppBar position='static'>
<Toolbar className='flex flex-col sm:flex-row items-center justify-between bg-foreground px-8 py-3 flex-wrap'>
<Link to='/' className='!text-text flex items-center'>
<img
src='/images/logo-transparent.png'
className='w-16 inline'
alt='Streamability Logo'
></img>
<Typ
variant='h5'
sx={{
display: 'inline',
m: 1,
letterSpacing: 2.5,
fontSize: { xs: 20, sm: 24, md: 36 },
}}
>
Streamability
</Typ>
</Link>
<div className='flex items-center'>
{windowWidth <= 768 ? (
// 768px Tailwind breakpoint prefix `md`
<IconButton
aria-label='search'
className='!text-text'
onClick={() => setExpandedSearch(!expandedSearch)}
>
<Search />
</IconButton>
) : (
<SearchInput />
)}
<div className='grow-0'>
<Tooltip title='Expand Menu'>
<IconButton
onClick={(event) => toggleUserMenu(event, true)}
size='large'
sx={{ ml: 2 }}
aria-haspopup='true'
aria-controls={open ? 'menu-appbar' : undefined}
aria-expanded={open ? 'true' : undefined}
>
<AccountCircle fontSize='inherit' className='!fill-text' />
{expandedMenu ? (
<ExpandLess className='!fill-text' />
) : (
<ExpandMore className='!fill-text' />
)}
</IconButton>
</Tooltip>
<Menu
anchorEl={anchorElUser}
id='menu-appbar'
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
open={open}
onClose={() => toggleUserMenu(null, false)}
// TODO: Fix deprecated prop
PaperProps={{
className: '!bg-background',
sx: {
overflow: 'visible',
filter: 'drop-shadow(0px 1px 16px rgba(5,0,10,0.30))',
mt: 6,
},
}}
>
{session ? (
<div>
<MenuItem
className='!p-2'
onClick={() => toggleUserMenu(null, false)}
component={Link}
to='/dashboard'
>
<ListItemIcon className='!text-text'>
<Settings className='mr-2' />
Dashboard
</ListItemIcon>
</MenuItem>
</div>
) : (
<div>
<MenuItem
className='!p-2'
onClick={() => toggleUserMenu(null, false)}
component={Link}
to='/login'
>
<ListItemIcon className='!text-text'>
<Login className='mr-2' />
Login
</ListItemIcon>
</MenuItem>
<MenuItem
className='!p-2'
onClick={() => toggleUserMenu(null, false)}
component={Link}
to='/signup'
>
<ListItemIcon className='!text-text'>
<PersonAddAlt className='mr-2' />
Sign Up
</ListItemIcon>
</MenuItem>
</div>
)}
<Divider />
<MenuItem
className='!p-2'
onClick={() => toggleUserMenu(null, false)}
component={Link}
to='/discover'
>
<ListItemIcon className='!text-text'>
<Explore className='mr-2' />
Discover
</ListItemIcon>
</MenuItem>
<MenuItem className='!p-2' onClick={switchTheme}>
<ListItemIcon className='!text-text'>
{themeIcon}
Switch Theme
</ListItemIcon>
</MenuItem>
{session && (
<MenuItem
className='!p-2'
onClick={() => {
logoutHandler();
toggleUserMenu(null, false);
}}
>
<ListItemIcon className='!text-text'>
<Logout className='mr-2' />
Logout
</ListItemIcon>
</MenuItem>
)}
</Menu>
</div>
</div>
</Toolbar>
{/* TODO: Add a transition when search is expanded or collapsed */}
{expandedSearch && (
<div className='flex items-center justify-center bg-foreground px-8 py-3'>
<SearchInput />
</div>
)}
</AppBar>
);
};
export default Navigation;