-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.tsx
222 lines (215 loc) · 6.45 KB
/
index.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
import { faMarkdown } from "@fortawesome/free-brands-svg-icons";
import { faImage } from "@fortawesome/free-regular-svg-icons";
import {
Box,
Grid,
Paper,
TextField,
Typography,
useMediaQuery,
FormControlLabel,
FormControl,
Switch,
} from "@material-ui/core";
import Head from "next/head";
import { FC, useEffect, useRef, useState } from "react";
import { Subject } from "rxjs";
import BadgeContent from "../components/BadgeContent";
import BadgeStyle from "../components/BadgeStyle";
import CopyToClipboard from "../components/CopyToClipboard";
import Footer from "../components/Footer";
import { Badge, DEFAULT_BADGE, getMarkdown, getUrl } from "../utils/badge";
import toValidUsernameObservable from "../utils/observable";
import useKeepVisible from "../utils/use-keep-visible";
const Home: FC = () => {
const [username$] = useState(() => new Subject<string>());
const [usernameInput, setUsernameInput] = useState("");
const [error, setError] = useState("");
const [badge, setBadge] = useState<Badge>(DEFAULT_BADGE);
const badgeRef = useRef<HTMLImageElement | null>(null);
const screenBigEnough = useMediaQuery("(min-width:650px)");
useEffect(() => {
const subscription = toValidUsernameObservable(
username$,
setError
).subscribe((username) => setBadge((b) => ({ ...b, username })));
return () => subscription.unsubscribe();
}, []);
useKeepVisible(badgeRef, 20, 16);
return (
<>
<Head>
<meta
name="Description"
content="Create a badge showing your ranking on LeetCode.com"
/>
<title>LeetCode Badge Generator</title>
<link rel="icon" href="/favicon.ico" />
<link
rel="icon"
type="image/png"
sizes="32x32"
href="/favicon-32x32.png"
/>
<link
rel="icon"
type="image/png"
sizes="16x16"
href="/favicon-16x16.png"
/>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
</Head>
<Box
display="flex"
alignItems="center"
justifyContent="center"
minHeight="100vh"
>
<Paper
elevation={3}
style={{
margin: "24px",
padding: "24px 24px 8px 24px",
}}
>
<Box
display="flex"
flexDirection="column"
alignItems="center"
justifyContent="center"
mt={1}
>
<Typography variant="h4" align="center">
LeetCode Badge Generator
</Typography>
</Box>
<Box
display="flex"
flexDirection="column"
alignItems="center"
justifyContent="center"
>
<Box
mt={4}
width={"100%"}
minHeight={96}
display="flex"
flexDirection="row"
alignItems="center"
justifyContent="flex-start"
>
<TextField
autoFocus
id="username"
label="Your LeetCode username"
variant="outlined"
error={!!error}
helperText={error}
size="medium"
value={usernameInput}
onChange={({ target: { value } }) => {
setUsernameInput(value);
setError("");
username$.next(value);
}}
fullWidth
/>
<Box width="60%">
<FormControl>
<FormControlLabel
control={
<Grid
component="label"
container
alignItems="center"
style={{ fontFamily: "monospace" }}
>
<Grid item>.com</Grid>
<Grid item>
<Switch
checked={badge.cn}
onChange={(e) =>
setBadge((b) => ({ ...b, cn: e.target.checked }))
}
></Switch>
</Grid>
<Grid item>.cn</Grid>
</Grid>
}
label="Site"
labelPlacement="top"
/>
</FormControl>
</Box>
</Box>
<Box
display="flex"
flexDirection="row"
alignItems="flex-start"
justifyContent="space-around"
flexWrap="wrap"
>
<Box
minWidth={264}
mt={screenBigEnough ? 1 : 2}
ml={screenBigEnough ? 2 : 0}
>
<BadgeContent badge={badge} setBadge={setBadge} />
</Box>
<Box
minWidth={264}
mt={screenBigEnough ? 1 : 5}
mr={screenBigEnough ? -2 : 0}
>
<BadgeStyle badge={badge} setBadge={setBadge} />
</Box>
</Box>
<Box
mt={4}
minHeight={50}
display="flex"
justifyContent="center"
alignItems="center"
>
<img
ref={badgeRef}
src={getUrl(badge)}
alt={badge.username}
style={{ position: "relative" }}
/>
</Box>
<Grid
container
spacing={4}
justify="center"
alignItems="flex-end"
style={{ marginTop: "8px" }}
>
<Grid item md={4} xs={4}>
<CopyToClipboard
icon={faImage}
label="Copy Image URL"
textToCopy={getUrl(badge)}
/>
</Grid>
<Grid item md={4} xs={4}>
<CopyToClipboard
icon={faMarkdown}
label="Copy Markdown Code"
textToCopy={getMarkdown(badge)}
/>
</Grid>
</Grid>
<Box mt={4}>
<Footer />
</Box>
</Box>
</Paper>
</Box>
</>
);
};
export default Home;