-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathNewPattern.tsx
84 lines (81 loc) · 2.22 KB
/
NewPattern.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
import React from 'react'
import { useNavigate } from 'react-router-dom'
import queryString from 'query-string'
import { useStore } from './StoreContext'
import { observer } from 'mobx-react'
import {
DialogContentText,
Dialog,
DialogTitle,
DialogContent,
FormControl,
TextField,
FormHelperText,
DialogActions,
Button
} from '@material-ui/core'
const Content = () => (
<DialogContentText>
To use regular expression match websites, please enter a valid regular
expression here. You can use{' '}
<a href='https://regex101.com/' target='_blank' rel='noopener noreferrer'>
Regex101
</a>{' '}
to validate your regular expression.
</DialogContentText>
)
const NewPatternDialog = observer(() => {
const navigate = useNavigate()
const { NewPatternStore } = useStore()
const { closeDialog, error, validPattern, pattern } = NewPatternStore
return (
<Dialog open onClose={closeDialog}>
<DialogTitle>New RegExp Pattern</DialogTitle>
<DialogContent>
<Content />
<FormControl fullWidth error={!validPattern}>
<TextField
autoFocus
margin='dense'
type='text'
placeholder='.*github.com'
label='RegExp Pattern'
error={!validPattern}
value={pattern}
onChange={(e) => {
NewPatternStore.setPattern(e.target.value)
}}
/>
<FormHelperText>{error}</FormHelperText>
</FormControl>
</DialogContent>
<DialogActions>
<Button onClick={closeDialog}>Cancel</Button>
<Button
color='primary'
onClick={() => {
const { addToHosts, host } = NewPatternStore
addToHosts()
navigate(`?${queryString.stringify(host)}`)
}}
disabled={!validPattern}
>
Add
</Button>
</DialogActions>
</Dialog>
)
})
export default observer(() => {
// TODO: support to use current domain
const { NewPatternStore } = useStore()
const { openDialog, open } = NewPatternStore
return (
<span>
<Button color='primary' onClick={openDialog}>
New RegExp
</Button>
{open && <NewPatternDialog />}
</span>
)
})