-
Notifications
You must be signed in to change notification settings - Fork 21
/
app.js
78 lines (69 loc) · 1.92 KB
/
app.js
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
import { Component } from 'preact';
import { Router } from 'preact-router';
import Provider from 'preact-context-provider';
import createStore from 'unistore';
import ossninja from '@lib/ossninja';
import Header from './header';
import Footer from './footer';
import Home from '../routes/home';
import License from '../routes/license';
import config from '../config.json';
const model = ossninja({ config });
const store = createStore();
export default function AppWrapper(props) {
return (
<Provider model={model} store={store}>
<App {...props} />
</Provider>
);
}
class App extends Component {
tq = [];
track = e => {
if (this.ga) this.ga.send('pageview', { dp: e.url });
else this.tq.push(e);
};
handleRoute = e => {
// ignore repeated routes (eg, when editing fields)
if (!e.previous || e.url.split('?')[0]!==e.previous.split('?')[0]) {
// a11y fix
clearTimeout(this.timer);
this.timer = setTimeout( () => {
let h1 = typeof document!=='undefined' && document.querySelector('h1');
if (h1 && h1!==this.h1) {
h1.tabIndex = -1;
h1.style.outline = 'none';
h1.focus();
document.title = h1.textContent + config.appTitleSuffix;
}
this.h1 = h1;
this.track(e);
}, 250);
}
};
componentDidMount() {
setTimeout( () => {
import('ganalytics').then( ({ default: GAnalytics }) => {
this.ga = new GAnalytics(config.gaTrackingId);
this.tq.forEach(this.track);
this.tq.length = 0;
});
}, 250);
}
render({ url }) {
return (
<div id="app">
<Header />
<Router url={url} onChange={this.handleRoute}>
<Home path="/" />
<License path="/:licenseId" />
<License path="/:licenseId/:gh" githubInUrl />
</Router>
<Footer />
{ typeof document==='undefined' && model.getAllLicensesSync && (
<script dangerouslySetInnerHTML={{ __html: `window.ALL_LICENSES=${JSON.stringify(model.getAllLicensesSync())}` }} />
) }
</div>
);
}
}