-
Notifications
You must be signed in to change notification settings - Fork 6
/
Navigation.tsx
249 lines (226 loc) · 8.28 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
/**
* Navigation: A side navigation component with metamask login
* Includes items for page navigation, theme selection, and language selection
*/
import React, { useContext, useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { useMetamask } from 'use-metamask';
import { useSnackbar } from 'notistack';
import { Menu, Palette, People, Translate, FormatListBulleted } from '@mui/icons-material';
import {
Button,
Divider,
Drawer,
Hidden,
IconButton,
List,
ListItem,
ListItemIcon,
ListItemText,
MenuItem,
Select,
Tooltip
} from '@mui/material';
import superagent from 'superagent';
import Web3 from 'web3';
import config from '../../../config';
import UserContext from '../../util/UserContext';
interface INavigation {
logout: Function;
page: string;
setContext: Function;
setPage: Function;
}
export default function (props: INavigation) {
const [mobileOpen, setMobileOpen] = useState<boolean>(false);
const { connect, getAccounts, getChain, metaState } = useMetamask();
const { enqueueSnackbar } = useSnackbar();
const userContext = useContext(UserContext);
const { address, settings, translations } = userContext;
const { language, theme } = settings;
useEffect(() => {
if (metaState.isAvailable && !metaState.isConnected) {
connect(metaState.web3 || Web3);
}
}, [metaState.isConnected, metaState.isAvailable]);
function handleDrawerToggle() {
setMobileOpen(!mobileOpen);
}
async function handleSignMessage(publicAddress, nonce): Promise<{ publicAddress: string; signature: string }> {
// Define instance of web3
const web3 = new Web3((window as any).ethereum);
return new Promise((resolve, reject) =>
web3.eth.personal.sign(web3.utils.fromUtf8(`Nonce: ${nonce}`), publicAddress, '', (err, signature) => {
if (err) return reject(err);
return resolve({ publicAddress, signature });
})
);
}
// if metamask is installed in browser, create a new user with the current network address
// and verify the user with a signature through the metamask wallet
async function login() {
const network = config.network;
if (!metaState?.isAvailable) {
window.open('https://metamask.io/download/', '_blank');
} else {
try {
const accounts = await getAccounts();
const chain = await getChain();
if (accounts.length === 0) {
enqueueSnackbar(translations.auth?.loginMetamask, { variant: 'warning' });
} else if (chain.name !== network) {
enqueueSnackbar(`${translations.auth?.connectNetwork}: ${network}`, { variant: 'error' });
} else {
const ethAddress = accounts[0];
const expressURL = config.express.url;
await superagent
.post(`${expressURL}/api/users/signup`)
.send({ address: ethAddress })
.then(async (dbUser) => {
if (dbUser) {
const nonce = dbUser.body.nonce;
const signedMessage = await handleSignMessage(ethAddress, nonce);
if (signedMessage) {
await superagent
.post(`${expressURL}/api/users/login`)
.send({ address: signedMessage.publicAddress, signature: signedMessage.signature })
.then(async (newUser) => {
const token = newUser?.body?.token;
const checksumAddress = newUser?.body?.address;
props.setContext({
...userContext,
address: checksumAddress,
token
});
});
}
}
});
}
} catch (error) {
enqueueSnackbar(error.message, { variant: 'error' });
}
}
}
const drawer = (
<div className="w-64 h-screen dark:bg-gray-900 dark:text-gray-100">
<div className="m-2">
{address ? (
<div>
<Button
variant="outlined"
sx={{ marginBottom: 1 }}
className="dark:text-gray-100"
disableElevation
onClick={() => props.logout()}
>
{translations.auth?.logout}
</Button>
<p className="break-words w-56 mr-2">{address}</p>
</div>
) : (
<Button variant="outlined" className="dark:text-gray-100" disableElevation onClick={() => login()}>
{translations.auth?.login}
</Button>
)}
</div>
<List>
<Link to="/characters" onClick={() => props.setPage('/characters')}>
<ListItem button key="dashboard-icon" selected={props.page.includes('/characters') || props.page === '/'}>
<ListItemIcon>
<Tooltip title={translations?.nav?.characters} placement="right">
<People className="dark:text-gray-100" aria-label={translations?.nav?.characters} />
</Tooltip>
</ListItemIcon>
<ListItemText primary={translations?.nav?.characters} />
</ListItem>
</Link>
<Link to="/transactions" onClick={() => props.setPage('/transactions')}>
<ListItem button key="dashboard-icon" selected={props.page.includes('/transactions')}>
<ListItemIcon>
<Tooltip title={translations?.nav?.transactions} placement="right">
<FormatListBulleted className="dark:text-gray-100" aria-label={translations?.nav?.transactions} />
</Tooltip>
</ListItemIcon>
<ListItemText primary={translations?.nav?.transactions} />
</ListItem>
</Link>
</List>
<Divider />
<List>
<ListItem button key="navigation__theme" aria-label={translations?.nav?.theme}>
<ListItemIcon>
<Palette className="dark:text-gray-100" />
</ListItemIcon>
<Select
aria-label={translations?.nav?.theme}
onChange={(e) =>
props.setContext({
...userContext,
settings: {
...userContext.settings,
theme: e.target.value
}
})
}
value={theme}
sx={{ height: 42 }}
className="dark:text-gray-100"
>
<MenuItem value={'light'}>{translations?.nav?.light}</MenuItem>
<MenuItem value={'dark'}>{translations?.nav?.dark}</MenuItem>
</Select>
</ListItem>
<ListItem button key="navigation__language" aria-label={translations?.nav?.language}>
<ListItemIcon>
<Translate className="dark:text-gray-100" />
</ListItemIcon>
<Select
aria-label={translations?.nav?.language}
onChange={(e) =>
props.setContext({
...userContext,
settings: {
...userContext.settings,
language: e.target.value
}
})
}
value={language}
sx={{ height: 42 }}
className="dark:text-gray-100"
>
<MenuItem value={'english'}>{translations?.languages?.english}</MenuItem>
<MenuItem value={'spanish'}>{translations?.languages?.spanish}</MenuItem>
<MenuItem value={'japanese'}>{translations?.languages?.japanese}</MenuItem>
</Select>
</ListItem>
</List>
</div>
);
return (
<div className="md:w-64 sm:w-4 dark:bg-gray-700">
<IconButton color="inherit" edge="start" onClick={handleDrawerToggle}>
<Menu />
</IconButton>
<Hidden lgUp implementation="js">
<Drawer
anchor="left"
onClose={handleDrawerToggle}
open={mobileOpen}
variant="temporary"
ModalProps={{
keepMounted: true
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden mdDown implementation="js">
<Drawer variant="permanent" open>
{drawer}
</Drawer>
</Hidden>
</div>
);
}