forked from johreh/gloomycompanion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.jsx
48 lines (41 loc) · 1.32 KB
/
App.jsx
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
import base58 from 'base-58';
import React from 'react';
import { hot } from 'react-hot-loader';
import MainUI from './MainUI';
import { useFirebase } from './firebase';
import { StorageContext } from './storage';
class App extends React.Component {
state = { locationHash: window.location.hash };
componentDidMount() {
window.addEventListener('hashchange', this.handleHashChange);
this.handleHashChange();
}
componentWillUnmount() {
window.removeEventListener('hashchange', this.handleHashChange);
}
handleHashChange = () => {
if (useFirebase && window.location.hash.length === 0) {
const randBits = new Uint8Array(8);
window.crypto.getRandomValues(randBits);
window.location.hash = `#${base58.encode(randBits)}`;
}
if (window.location.hash !== this.state.locationHash) {
this.setState({ locationHash: window.location.hash });
}
}
render() {
const storageContext = { useFirebase };
if (useFirebase && this.state.locationHash) {
const gameUuid = this.state.locationHash.slice(1);
storageContext.firebaseRoot = `games/${gameUuid}/`;
}
return (
<React.StrictMode>
<StorageContext.Provider value={storageContext}>
<MainUI />
</StorageContext.Provider>
</React.StrictMode>
);
}
}
export default hot(module)(App);