aboutsummaryrefslogtreecommitdiff
path: root/components/navbar.tsx
blob: 932669da48a74e6749bfa57e25565eacbe7b66ab (plain)
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
import axios from 'axios';
import { useContext, useEffect, useState } from 'react';

import { userInfo } from '../api/api';
import Logo from '../components/logo';
import { AccountAvatar } from './account';
import { NotificationsArea } from './notificationsArea';
import { SocketContext } from './socketContext';

import ExtensionIcon from '@material-ui/icons/Extension';
import Home from '@material-ui/icons/Home';
import NotificationsIcon from '@material-ui/icons/Notifications';
import PersonIcon from '@material-ui/icons/Person';
import SearchIcon from '@material-ui/icons/Search';
import SettingsIcon from '@material-ui/icons/Settings';
import VideogameAssetIcon from '@material-ui/icons/VideogameAsset';

export function NavBar() {
	var [loggedIn, setLoggedIn] = useState(false);
	var [gotData, setGotData] = useState(false);

	var [friendRequests, setFriendRequests] = useState<Array<userInfo>>(null);

	var [notificationsAreaVisible, setNotificationsAreaVisible] = useState(false);
	var [gotNotifications, setGotNotifications] = useState(false);

	var { io } = useContext(SocketContext);

	async function getNotifications() {
		var friendRequestsReq = await axios.request<{ requests: Array<userInfo>; }>({
			method: 'get',
			url: `/api/social/list/requests`,
		});
		setFriendRequests(friendRequestsReq.data.requests);

		setGotNotifications(friendRequestsReq.data.requests.length > 0);
	}

	useEffect(() => {
		(async () => {
			if (gotData) return;
			if (typeof window === 'undefined') return;

			var loggedIn = document.cookie.includes('token');
			setLoggedIn(loggedIn);

			if (loggedIn) {
				await getNotifications();
				io.on('incomingFriendRequest', getNotifications);
				io.on('changedRelation', getNotifications);
			}

			setGotData(true);
		})();
	}, []);

	return <div
		className="navbar bg-800"
		style={{
			width: 48,
			height: '100%',

			lineHeight: 0,

			display: 'inline-block',

			position: 'fixed',
			top: 0,
			left: 0,

			overflow: 'visible',
			whiteSpace: 'nowrap',
			zIndex: 2,
		}}
	>
		<div className="item">
			<Logo />
		</div>
		<a href='/' className="item">
			<Home />
		</a>
		<a href='/game' className="item">
			<VideogameAssetIcon />
		</a>
		{false && <a href='/' className="item">
			<ExtensionIcon />
		</a>}
		<a href='/search' className="item">
			<SearchIcon />
		</a>

		<div className="bg-800 bottomArea">
			{loggedIn && <a
				className="item"
				style={{
					overflow: 'visible',
					position: 'relative'
				}}
			>
				<div
					style={{ cursor: 'pointer' }}
					onClick={() => setNotificationsAreaVisible(!notificationsAreaVisible)}
				>
					<NotificationsIcon />
					{gotNotifications && <div
						style={{
							backgroundColor: 'var(--disk-a)',
							width: 8,
							height: 8,
							borderRadius: 4,
							position: 'absolute',
							top: 2,
							right: 2,
						}}
					/>}
				</div>
				<NotificationsArea
					visible={notificationsAreaVisible}
					friendRequests={friendRequests}
					rerender={getNotifications}
				/>
			</a>}
			<a href={loggedIn ? '/user' : '/login'} className="item">
				{loggedIn
					? <AccountAvatar size={24} round />
					: <PersonIcon />}
			</a>
			{loggedIn && <a href='/settings' className="item">
				<SettingsIcon />
			</a>}
		</div>
	</div>;
}