@mblaney/holster-browser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,184 @@
1
+ import {useState} from "react"
2
+ import Button from "@mui/material/Button"
3
+ import Card from "@mui/material/Card"
4
+ import CardContent from "@mui/material/CardContent"
5
+ import Container from "@mui/material/Container"
6
+ import FormControl from "@mui/material/FormControl"
7
+ import Grid from "@mui/material/Grid"
8
+ import IconButton from "@mui/material/IconButton"
9
+ import InputAdornment from "@mui/material/InputAdornment"
10
+ import InputLabel from "@mui/material/InputLabel"
11
+ import OutlinedInput from "@mui/material/OutlinedInput"
12
+ import TextField from "@mui/material/TextField"
13
+ import Typography from "@mui/material/Typography"
14
+ import Visibility from "@mui/icons-material/Visibility"
15
+ import VisibilityOff from "@mui/icons-material/VisibilityOff"
16
+ import SearchAppBar from "./SearchAppBar.jsx"
17
+
18
+ const Register = ({user, mode, setMode, appBar}) => {
19
+ const [username, setUsername] = useState("")
20
+ const [password, setPassword] = useState("")
21
+ const [showPassword, setShowPassword] = useState(false)
22
+ const [email, setEmail] = useState("")
23
+ const [code, setCode] = useState("")
24
+ const [message, setMessage] = useState(user.is ? "Already logged in" : "")
25
+ const [disabledButton, setDisabledButton] = useState(user.is)
26
+
27
+ const register = () => {
28
+ if (!username) {
29
+ setMessage("Please choose a username")
30
+ return
31
+ }
32
+ if (!/^\w+$/.test(username)) {
33
+ setMessage("Username must contain only numbers, letters and underscore")
34
+ return
35
+ }
36
+ if (!email) {
37
+ setMessage("Please provide your email")
38
+ return
39
+ }
40
+
41
+ setDisabledButton(true)
42
+ setMessage("Checking login code...")
43
+ fetch(`${window.location.origin}/check-login-code`, {
44
+ method: "POST",
45
+ headers: {"Content-Type": "application/json;charset=utf-8"},
46
+ body: JSON.stringify({code: code}),
47
+ })
48
+ .then(res => res.text().then(text => ({ok: res.ok, text: text})))
49
+ .then(res => {
50
+ if (!res.ok) {
51
+ setDisabledButton(false)
52
+ setMessage(res.text)
53
+ return
54
+ }
55
+
56
+ user.create(username, password, err => {
57
+ if (err) {
58
+ setDisabledButton(false)
59
+ setMessage(err)
60
+ return
61
+ }
62
+
63
+ user.auth(username, password, err => {
64
+ if (err) {
65
+ setDisabledButton(false)
66
+ setMessage(err)
67
+ return
68
+ }
69
+
70
+ fetch(`${window.location.origin}/verify-login-code`, {
71
+ method: "POST",
72
+ headers: {"Content-Type": "application/json;charset=utf-8"},
73
+ body: JSON.stringify({
74
+ code: code,
75
+ pub: user.is.pub,
76
+ epub: user.is.epub,
77
+ username: username,
78
+ email: email,
79
+ }),
80
+ })
81
+ .then(res => res.text().then(text => ({ok: res.ok, text: text})))
82
+ .then(res => {
83
+ setDisabledButton(false)
84
+ if (!res.ok) {
85
+ user.delete(username, password, console.log)
86
+ setMessage(res.text)
87
+ return
88
+ }
89
+
90
+ setMessage("Account created")
91
+ window.location = "/login"
92
+ })
93
+ })
94
+ })
95
+ })
96
+ }
97
+
98
+ return (
99
+ <>
100
+ {user.is && (
101
+ <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
102
+ )}
103
+ <Container maxWidth="sm">
104
+ <Grid container>
105
+ <Grid item xs={12}>
106
+ <Card sx={{mt: 2}}>
107
+ <CardContent>
108
+ <Typography variant="h5">Register</Typography>
109
+ <TextField
110
+ id="register-username"
111
+ label="Username"
112
+ variant="outlined"
113
+ fullWidth={true}
114
+ margin="normal"
115
+ value={username}
116
+ onChange={event => setUsername(event.target.value)}
117
+ InputLabelProps={{shrink: true}}
118
+ />
119
+ <FormControl
120
+ variant="outlined"
121
+ fullWidth={true}
122
+ margin="normal"
123
+ value={password}
124
+ onChange={event => setPassword(event.target.value)}
125
+ >
126
+ <InputLabel htmlFor="register-password">Password</InputLabel>
127
+ <OutlinedInput
128
+ id="register-password"
129
+ type={showPassword ? "text" : "password"}
130
+ endAdornment={
131
+ <InputAdornment position="end">
132
+ <IconButton
133
+ aria-label="toggle password visibility"
134
+ onClick={() => setShowPassword(show => !show)}
135
+ edge="end"
136
+ >
137
+ {showPassword ? <VisibilityOff /> : <Visibility />}
138
+ </IconButton>
139
+ </InputAdornment>
140
+ }
141
+ label="Password"
142
+ />
143
+ </FormControl>
144
+ <TextField
145
+ id="register-email"
146
+ label="Email"
147
+ variant="outlined"
148
+ fullWidth={true}
149
+ margin="normal"
150
+ value={email}
151
+ onChange={event => setEmail(event.target.value)}
152
+ />
153
+ <TextField
154
+ id="register-code"
155
+ label="Login code"
156
+ variant="outlined"
157
+ fullWidth={true}
158
+ margin="normal"
159
+ value={code}
160
+ onChange={event => setCode(event.target.value)}
161
+ />
162
+ <Button
163
+ sx={{mt: 1}}
164
+ variant="contained"
165
+ disabled={disabledButton}
166
+ onClick={register}
167
+ >
168
+ Submit
169
+ </Button>
170
+ {message && (
171
+ <Typography sx={{m: 1}} variant="string">
172
+ {message}
173
+ </Typography>
174
+ )}
175
+ </CardContent>
176
+ </Card>
177
+ </Grid>
178
+ </Grid>
179
+ </Container>
180
+ </>
181
+ )
182
+ }
183
+
184
+ export default Register
@@ -0,0 +1,79 @@
1
+ import {useState} from "react"
2
+ import Button from "@mui/material/Button"
3
+ import Card from "@mui/material/Card"
4
+ import CardContent from "@mui/material/CardContent"
5
+ import Container from "@mui/material/Container"
6
+ import Grid from "@mui/material/Grid"
7
+ import TextField from "@mui/material/TextField"
8
+ import Typography from "@mui/material/Typography"
9
+ import SearchAppBar from "./SearchAppBar.jsx"
10
+
11
+ const RequestCode = ({loggedIn, mode, setMode, appBar}) => {
12
+ const [email, setEmail] = useState("")
13
+ const [message, setMessage] = useState("")
14
+ const [disabledButton, setDisabledButton] = useState(false)
15
+
16
+ const request = () => {
17
+ if (!email) {
18
+ setMessage("Please provide your email")
19
+ return
20
+ }
21
+
22
+ setDisabledButton(true)
23
+ setMessage("Requesting login code...")
24
+ fetch(`${window.location.origin}/request-login-code`, {
25
+ method: "POST",
26
+ headers: {"Content-Type": "application/json;charset=utf-8"},
27
+ body: JSON.stringify({email: email}),
28
+ })
29
+ .then(res => res.text().then(text => ({ok: res.ok, text: text})))
30
+ .then(res => {
31
+ setDisabledButton(false)
32
+ setEmail("")
33
+ setMessage(res.text)
34
+ })
35
+ }
36
+
37
+ return (
38
+ <>
39
+ {loggedIn && (
40
+ <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
41
+ )}
42
+ <Container maxWidth="sm">
43
+ <Grid container>
44
+ <Grid item xs={12}>
45
+ <Card sx={{mt: 2}}>
46
+ <CardContent>
47
+ <Typography variant="h5">Request login code</Typography>
48
+ <TextField
49
+ id="request-email"
50
+ label="Email"
51
+ variant="outlined"
52
+ fullWidth={true}
53
+ margin="normal"
54
+ value={email}
55
+ onChange={event => setEmail(event.target.value)}
56
+ />
57
+ <Button
58
+ sx={{mt: 1}}
59
+ variant="contained"
60
+ disabled={disabledButton}
61
+ onClick={request}
62
+ >
63
+ Submit
64
+ </Button>
65
+ {message && (
66
+ <Typography sx={{m: 1}} variant="string">
67
+ {message}
68
+ </Typography>
69
+ )}
70
+ </CardContent>
71
+ </Card>
72
+ </Grid>
73
+ </Grid>
74
+ </Container>
75
+ </>
76
+ )
77
+ }
78
+
79
+ export default RequestCode
@@ -0,0 +1,88 @@
1
+ import {useState} from "react"
2
+ import Button from "@mui/material/Button"
3
+ import Card from "@mui/material/Card"
4
+ import CardContent from "@mui/material/CardContent"
5
+ import Container from "@mui/material/Container"
6
+ import Grid from "@mui/material/Grid"
7
+ import TextField from "@mui/material/TextField"
8
+ import Typography from "@mui/material/Typography"
9
+ import SearchAppBar from "./SearchAppBar.jsx"
10
+
11
+ const ResetPassword = ({loggedIn, mode, setMode, appBar}) => {
12
+ const [email, setEmail] = useState("")
13
+ const [code, setCode] = useState("")
14
+ const [message, setMessage] = useState(loggedIn ? "Already logged in" : "")
15
+ const [disabledButton, setDisabledButton] = useState(loggedIn)
16
+
17
+ const reset = () => {
18
+ if (!email) {
19
+ setMessage("Please provide your email")
20
+ return
21
+ }
22
+
23
+ setDisabledButton(true)
24
+ setMessage("Checking account...")
25
+ fetch(`${window.location.origin}/reset-password`, {
26
+ method: "POST",
27
+ headers: {"Content-Type": "application/json;charset=utf-8"},
28
+ body: JSON.stringify({email: email, code: code}),
29
+ })
30
+ .then(res => res.text())
31
+ .then(res => {
32
+ setDisabledButton(false)
33
+ setMessage(res)
34
+ })
35
+ }
36
+
37
+ return (
38
+ <>
39
+ {loggedIn && (
40
+ <SearchAppBar mode={mode} setMode={setMode} {...appBar} />
41
+ )}
42
+ <Container maxWidth="sm">
43
+ <Grid container>
44
+ <Grid item xs={12}>
45
+ <Card sx={{mt: 2}}>
46
+ <CardContent>
47
+ <Typography variant="h5">Reset Password</Typography>
48
+ <TextField
49
+ id="reset-password-email"
50
+ label="Email"
51
+ variant="outlined"
52
+ fullWidth={true}
53
+ margin="normal"
54
+ value={email}
55
+ onChange={event => setEmail(event.target.value)}
56
+ />
57
+ <TextField
58
+ id="reset-password-code"
59
+ label="Login code"
60
+ variant="outlined"
61
+ fullWidth={true}
62
+ margin="normal"
63
+ value={code}
64
+ onChange={event => setCode(event.target.value)}
65
+ />
66
+ <Button
67
+ sx={{mt: 1}}
68
+ variant="contained"
69
+ disabled={disabledButton}
70
+ onClick={reset}
71
+ >
72
+ Submit
73
+ </Button>
74
+ {message && (
75
+ <Typography sx={{m: 1}} variant="string">
76
+ {message}
77
+ </Typography>
78
+ )}
79
+ </CardContent>
80
+ </Card>
81
+ </Grid>
82
+ </Grid>
83
+ </Container>
84
+ </>
85
+ )
86
+ }
87
+
88
+ export default ResetPassword
@@ -0,0 +1,290 @@
1
+ import {useRef, useState} from "react"
2
+ import {styled, alpha, useTheme} from "@mui/material/styles"
3
+ import useMediaQuery from "@mui/material/useMediaQuery"
4
+ import AppBar from "@mui/material/AppBar"
5
+ import Box from "@mui/material/Box"
6
+ import Button from "@mui/material/Button"
7
+ import IconButton from "@mui/material/IconButton"
8
+ import InputBase from "@mui/material/InputBase"
9
+ import MenuItem from "@mui/material/MenuItem"
10
+ import Menu from "@mui/material/Menu"
11
+ import Switch from "@mui/material/Switch"
12
+ import Toolbar from "@mui/material/Toolbar"
13
+ import Typography from "@mui/material/Typography"
14
+ import AccountCircleIcon from "@mui/icons-material/AccountCircle"
15
+ import DarkModeIcon from "@mui/icons-material/DarkMode"
16
+ import LightModeIcon from "@mui/icons-material/LightMode"
17
+ import MoreIcon from "@mui/icons-material/MoreVert"
18
+ import SearchIcon from "@mui/icons-material/Search"
19
+
20
+ const Search = styled("div", {
21
+ shouldForwardProp: prop => prop !== "collapsed",
22
+ })(({theme, collapsed}) => ({
23
+ position: "relative",
24
+ borderRadius: theme.shape.borderRadius,
25
+ backgroundColor: alpha(theme.palette.common.white, 0.15),
26
+ "&:hover": {
27
+ backgroundColor: alpha(theme.palette.common.white, 0.25),
28
+ },
29
+ marginRight: theme.spacing(2),
30
+ marginLeft: 0,
31
+ width: collapsed ? "auto" : "100%",
32
+ ...(collapsed && {
33
+ maxWidth: theme.spacing(7),
34
+ overflow: "hidden",
35
+ transition: theme.transitions.create("max-width"),
36
+ "&:focus-within": {maxWidth: "200px"},
37
+ }),
38
+ [theme.breakpoints.up("sm")]: {
39
+ marginLeft: theme.spacing(3),
40
+ width: "auto",
41
+ },
42
+ }))
43
+
44
+ const SearchIconWrapper = styled("div")(({theme}) => ({
45
+ padding: theme.spacing(0, 2),
46
+ height: "100%",
47
+ position: "absolute",
48
+ pointerEvents: "none",
49
+ display: "flex",
50
+ alignItems: "center",
51
+ justifyContent: "center",
52
+ }))
53
+
54
+ const StyledInputBase = styled(InputBase, {
55
+ shouldForwardProp: prop => prop !== "collapsed",
56
+ })(({theme, collapsed}) => ({
57
+ color: "inherit",
58
+ "& .MuiInputBase-input": {
59
+ padding: theme.spacing(1, 1, 1, 0),
60
+ paddingLeft: `calc(1em + ${theme.spacing(4)})`,
61
+ transition: theme.transitions.create("width"),
62
+ width: collapsed ? 0 : "100%",
63
+ ...(collapsed && {
64
+ "&:focus": {width: "8ch"},
65
+ }),
66
+ [theme.breakpoints.up("sm")]: {
67
+ width: "12ch",
68
+ },
69
+ [theme.breakpoints.up("md")]: {
70
+ width: "20ch",
71
+ },
72
+ },
73
+ }))
74
+
75
+ const SearchAppBar = ({
76
+ name,
77
+ icon: AppIcon,
78
+ iconSx,
79
+ onHomeClick,
80
+ title,
81
+ onTitleClick,
82
+ onSearch,
83
+ searchQuery,
84
+ mode,
85
+ setMode,
86
+ menuItems,
87
+ }) => {
88
+ const theme = useTheme()
89
+ const isSmall = useMediaQuery(theme.breakpoints.down("sm"))
90
+ const collapsed = !!title && isSmall
91
+ const searchInputRef = useRef(null)
92
+ const [searchValue, setSearchValue] = useState(searchQuery || "")
93
+ const [anchorEl, setAnchorEl] = useState(null)
94
+ const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = useState(null)
95
+
96
+ const isMenuOpen = Boolean(anchorEl)
97
+ const isMobileMenuOpen = Boolean(mobileMoreAnchorEl)
98
+
99
+ const handleProfileMenuOpen = event => {
100
+ setAnchorEl(event.currentTarget)
101
+ }
102
+
103
+ const handleMobileMenuClose = () => {
104
+ setMobileMoreAnchorEl(null)
105
+ }
106
+
107
+ const handleMenuClose = () => {
108
+ setAnchorEl(null)
109
+ handleMobileMenuClose()
110
+ }
111
+
112
+ const handleMobileMenuOpen = event => {
113
+ setMobileMoreAnchorEl(event.currentTarget)
114
+ }
115
+
116
+ const changeMode = () => {
117
+ sessionStorage.setItem("mode", mode === "light" ? "dark" : "light")
118
+ setMode(mode === "light" ? "dark" : "light")
119
+ }
120
+
121
+ const menuId = "search-account-menu"
122
+ const renderMenu = (
123
+ <Menu
124
+ anchorEl={anchorEl}
125
+ anchorOrigin={{vertical: "top", horizontal: "right"}}
126
+ id={menuId}
127
+ keepMounted
128
+ transformOrigin={{vertical: "top", horizontal: "right"}}
129
+ open={isMenuOpen}
130
+ onClose={handleMenuClose}
131
+ >
132
+ {(menuItems ?? []).map(item => (
133
+ <MenuItem
134
+ key={item.label}
135
+ onClick={() => {
136
+ handleMenuClose()
137
+ item.onClick()
138
+ }}
139
+ >
140
+ {item.label}
141
+ </MenuItem>
142
+ ))}
143
+ </Menu>
144
+ )
145
+
146
+ const mobileMenuId = "search-account-menu-mobile"
147
+ const renderMobileMenu = (
148
+ <Menu
149
+ anchorEl={mobileMoreAnchorEl}
150
+ anchorOrigin={{vertical: "top", horizontal: "right"}}
151
+ id={mobileMenuId}
152
+ keepMounted
153
+ transformOrigin={{vertical: "top", horizontal: "right"}}
154
+ open={isMobileMenuOpen}
155
+ onClose={handleMobileMenuClose}
156
+ >
157
+ <MenuItem onClick={handleProfileMenuOpen}>
158
+ <IconButton
159
+ size="large"
160
+ aria-label="account of current user"
161
+ aria-controls="search-account-menu"
162
+ aria-haspopup="true"
163
+ color="inherit"
164
+ >
165
+ <AccountCircleIcon />
166
+ </IconButton>
167
+ <p>Account</p>
168
+ </MenuItem>
169
+ <MenuItem onClick={changeMode}>
170
+ <IconButton size="large" color="inherit">
171
+ {mode === "light" ? <DarkModeIcon /> : <LightModeIcon />}
172
+ </IconButton>
173
+ <p>{mode === "light" ? "Dark" : "Light"} mode</p>
174
+ </MenuItem>
175
+ </Menu>
176
+ )
177
+
178
+ return (
179
+ <Box sx={{flexGrow: 1}}>
180
+ <AppBar position="fixed">
181
+ <Toolbar>
182
+ {AppIcon && (
183
+ <IconButton
184
+ size="large"
185
+ edge="start"
186
+ color="inherit"
187
+ aria-label="home"
188
+ onClick={onHomeClick ?? (() => (window.location = "/"))}
189
+ >
190
+ <AppIcon sx={iconSx} />
191
+ </IconButton>
192
+ )}
193
+ {title ? (
194
+ <Button
195
+ sx={{
196
+ color: "white",
197
+ fontSize: "1.25em",
198
+ textTransform: "none",
199
+ whiteSpace: "nowrap",
200
+ textOverflow: "ellipsis",
201
+ overflow: "hidden",
202
+ display: "block",
203
+ }}
204
+ onClick={onTitleClick}
205
+ >
206
+ {title}
207
+ </Button>
208
+ ) : (
209
+ <Typography
210
+ variant="h6"
211
+ noWrap
212
+ component="div"
213
+ sx={{display: {xs: "none", sm: "block"}}}
214
+ >
215
+ {name}
216
+ </Typography>
217
+ )}
218
+ <Box sx={{flexGrow: 1}} />
219
+ <Search
220
+ collapsed={collapsed}
221
+ onClick={() => searchInputRef.current?.focus()}
222
+ >
223
+ <SearchIconWrapper>
224
+ <SearchIcon />
225
+ </SearchIconWrapper>
226
+ <StyledInputBase
227
+ collapsed={collapsed}
228
+ inputRef={searchInputRef}
229
+ placeholder="Search…"
230
+ inputProps={{"aria-label": "search"}}
231
+ value={searchValue}
232
+ onChange={e => setSearchValue(e.target.value)}
233
+ onKeyDown={e => {
234
+ if (e.key === "Enter") {
235
+ const trimmed = searchValue.trim()
236
+ if (trimmed.length >= 3) {
237
+ setSearchValue(trimmed)
238
+ if (onSearch) {
239
+ onSearch(trimmed)
240
+ } else {
241
+ window.location = `/?search=${encodeURIComponent(trimmed)}`
242
+ }
243
+ searchInputRef.current?.blur()
244
+ }
245
+ } else if (e.key === "Escape") {
246
+ setSearchValue("")
247
+ if (onSearch) onSearch("")
248
+ searchInputRef.current?.blur()
249
+ }
250
+ }}
251
+ />
252
+ </Search>
253
+ <Box sx={{display: {xs: "none", md: "flex"}}}>
254
+ <Switch checked={mode === "dark"} onChange={changeMode} />
255
+ </Box>
256
+ <Box sx={{display: {xs: "none", md: "flex"}}}>
257
+ <IconButton
258
+ size="large"
259
+ edge="end"
260
+ aria-label="account of current user"
261
+ aria-controls={menuId}
262
+ aria-haspopup="true"
263
+ onClick={handleProfileMenuOpen}
264
+ color="inherit"
265
+ >
266
+ <AccountCircleIcon />
267
+ </IconButton>
268
+ </Box>
269
+ <Box sx={{display: {xs: "flex", md: "none"}}}>
270
+ <IconButton
271
+ size="large"
272
+ aria-label="show more"
273
+ aria-controls={mobileMenuId}
274
+ aria-haspopup="true"
275
+ onClick={handleMobileMenuOpen}
276
+ color="inherit"
277
+ >
278
+ <MoreIcon />
279
+ </IconButton>
280
+ </Box>
281
+ </Toolbar>
282
+ </AppBar>
283
+ <Toolbar />
284
+ {renderMobileMenu}
285
+ {renderMenu}
286
+ </Box>
287
+ )
288
+ }
289
+
290
+ export default SearchAppBar