-
Notifications
You must be signed in to change notification settings - Fork 106
/
App.tsx
263 lines (244 loc) · 9.85 KB
/
App.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
252
253
254
255
256
257
258
259
260
261
262
263
import React from 'react'
import { CssBaseline, Theme, createStyles, makeStyles, AppBar, Toolbar, Typography, Button, Box, Container, FormControl, InputLabel, Select, MenuItem, OutlinedInput, Paper, Tabs, Tab } from '@material-ui/core';
import { navigate, useRoutes, HookRouter, A } from 'hookrouter';
// import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter/prism-light';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { useAsync } from 'react-use';
import request from 'request';
import { ExamplesRepo, ExampleCodeUrl } from './examples/Index';
import ReactGA from 'react-ga';
import ExampleIds from './examples/ids';
const packageJson = require('../package.json');
const packageDependencies = packageJson.dependencies
// import { prismStyle } from './highlightStyles';
const highlightStyle = undefined;
// declare var highlightStyle: any;
// const highlightStyle = require('react-syntax-highlighter/dist/esm/styles/prism/tomorrow').default;
// import { default as highlightStyle } from require('react-syntax-highlighter/dist/esm/styles/prism/duotone-light');
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
flexGrow: 1,
},
menuButton: {
marginRight: theme.spacing(2),
},
title: {
flexGrow: 1,
},
button: {
margin: theme.spacing(1),
},
}),
);
const VersionInfo = () => {
const packs = Object.keys(packageDependencies)
.filter(i => i.startsWith('@hookstate/'));
const labels = packs.map((p, i) => <span key={p}>
{p}: {packageDependencies[p]} {i !== packs.length - 1 && ' | '}
</span>)
return <p style={{ fontSize: '0.5em' }}>Version info:<br/>{labels}</p>;
}
const ButtonAppBar = () => {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<Typography variant="h6" className={classes.title}>
<A href="/" style={{ color: 'white' }}>Hookstate</A>
</Typography>
<Button
color="inherit"
variant="outlined"
onClick={() => {
ReactGA.event({
category: 'button-click',
action: 'fork-me-on-github'
});
window.open('https://github.com/avkonst/hookstate', '_blank')
}}
>
Fork me on Github
</Button>
</Toolbar>
</AppBar>
</div>
);
}
const SourceCodeView = (props: { url: string }) => {
const code = useAsync(() => new Promise<string>((resolve, reject) => {
request.get(
props.url,
(err, resp, body) => err ? reject(err) : resolve(body));
}
))
let codeString = ''
if (code.loading) {
codeString = `Loading code sample from: ${props.url}`;
} else if (code.error) {
codeString = `Failure to load code sample from: ${props.url} (${code.error.toString()})`;
} else {
codeString = code.value ? code.value.toString() : `Failure to load code sample from: ${props.url}`;
}
return (
<SyntaxHighlighter
// TODO tsx highlighter is broken
language="jsx" style={highlightStyle}>
{codeString}
</SyntaxHighlighter>
);
};
const HomePage = (props: { example?: string }) => {
const [tab, setTab] = React.useState(2);
const classes = useStyles();
const exampleId = props.example && ExamplesRepo.has(props.example)
? props.example : ExampleIds.GlobalPrimitive;
const exampleMeta = ExamplesRepo.get(exampleId)!;
function handleChange(event: React.ChangeEvent<{}>, newValue: number) {
setTab(newValue);
}
return <>
<Container maxWidth="md">
<Box paddingTop={4}>
<Typography variant="h4" gutterBottom={true} align="center">
<b>Hookstate</b>: the simple but very powerful and incredibly fast state management for React that is based on hooks.
</Typography>
</Box>
<Box
paddingTop={2}
display="flex"
justifyContent="center"
>
<Button
variant="contained"
color="secondary"
className={classes.button}
onClick={() => {
ReactGA.event({
category: 'button-click',
action: 'why-hookstate'
});
window.location.href = 'https://github.com/avkonst/hookstate#why-hookstate'
}}
>Why hookstate
</Button>
<Button
variant="contained"
color="primary"
className={classes.button}
onClick={() => {
ReactGA.event({
category: 'button-click',
action: 'api-documentation'
});
window.location.href = 'https://github.com/avkonst/hookstate#api-documentation'
}}
>See documentation
</Button>
<Button
variant="contained"
color="primary"
className={classes.button}
onClick={() => {
ReactGA.event({
category: 'button-click',
action: 'plugins'
});
window.location.href = 'https://github.com/avkonst/hookstate#plugins'
}}
>Browse plugins
</Button>
</Box>
</Container>
<Container maxWidth="md" key={exampleId}>
<Box paddingTop={2}>
<Paper square>
<Box padding={2} paddingTop={4}>
<FormControl fullWidth={true} variant="outlined">
<InputLabel htmlFor="example-simple">Selected example:</InputLabel>
<Select
value={exampleId}
onChange={(v) => {
const exampleId = v.target.value as string;
ReactGA.event({
category: 'example-select',
action: exampleId,
});
navigate(exampleId)
}}
input={<OutlinedInput labelWidth={140} name="example" id="example-simple" />}
inputProps={{
name: 'example',
id: 'example-simple',
}}
>
{
Array.from(ExamplesRepo.entries())
.map(([k, v]) => <MenuItem key={k} value={k}>{v.name}</MenuItem>)
}
</Select>
</FormControl>
</Box>
<Tabs
centered={true}
value={tab}
indicatorColor="primary"
textColor="primary"
onChange={handleChange}
aria-label="disabled tabs example"
>
<Tab label="View code" />
<Tab label="View demo" />
<Tab label="View both" />
</Tabs>
{exampleMeta.description &&
<Box
margin={2}
padding={2}
style={{
backgroundColor: 'rgba(0, 0, 200, 0.05)',
}}
>
<Typography variant="body1" align="left" >
{exampleMeta.description}
</Typography>
</Box>
}
{(tab === 1 || tab === 2) &&
<Box
margin={2}
padding={2}
style={{
backgroundColor: 'rgba(0, 200, 0, 0.05)',
}}
>
{exampleMeta.demo}
</Box>
}
{(tab === 0 || tab === 2) &&
<Box margin={2}>
<SourceCodeView url={ExampleCodeUrl(exampleId)} />
</Box>
}
<Box
paddingTop={2}
/>
</Paper>
</Box>
<VersionInfo />
</Container>
</>
}
const routes: HookRouter.RouteObject = {
'/:example': ({ example }: HookRouter.QueryParams) => <HomePage example={example} />,
};
export const App = () => {
const routeResult = useRoutes(routes);
return <>
<CssBaseline />
<ButtonAppBar />
{routeResult || <HomePage />}
</>;
}
export default App;