-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathupload.tsx
102 lines (99 loc) · 2.73 KB
/
upload.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
import { useState } from 'react'
import exampleImg from '../assets/crop-example.jpg'
export const Upload = ({
handleFile,
toCameraMode,
}: {
handleFile(file: File): Promise<void>
toCameraMode(): void
}) => {
const [isDragOn, setIsDragOn] = useState(false)
const [err, setErr] = useState('')
const [exampleOn, setExampleOn] = useState(false)
return (
<>
<label
style={{
margin: 16,
flexBasis: 300,
cursor: 'pointer',
display: 'flex',
flexDirection: 'column',
alignItems: 'start',
justifyContent: 'center',
padding: '0 48px',
...(isDragOn && { backgroundColor: '#ff606020' }),
}}
className="dropzone"
onDragOver={e => {
e.preventDefault()
}}
onDragEnter={e => {
e.target === e.currentTarget && setIsDragOn(true)
}}
onDragLeave={e => {
e.target === e.currentTarget && setIsDragOn(false)
}}
onDrop={async e => {
setIsDragOn(false)
e.preventDefault()
const item = e.dataTransfer?.items?.[0]
const file = item.kind === 'file' ? item.getAsFile() : null
file &&
handleFile(file).catch(() => {
setErr('Invalid image')
})
}}
>
<div style={{ pointerEvents: 'none' }}>
<div>To start, you can:</div>
<div>- Drag an image into the box</div>
<div>- or click the box to upload an image</div>
<div>
- or make an in-game screenshot using WIN + SHIFT + S, then CTRL + V
here
</div>
</div>
<input
type="file"
accept="image/*"
style={{ display: 'none' }}
onChange={e => {
const file = e.target.files?.[0]
file &&
handleFile(file).catch(() => {
setErr('Invalid image')
})
}}
/>
</label>
{err && <div style={{ marginLeft: 16 }}>Error: {err}</div>}
<div style={{ margin: '0 16px' }}>
Please crop the screenshot before uploading.
<a
style={{ marginLeft: 4 }}
href="#"
onClick={() => {
setExampleOn(!exampleOn)
}}
>
{exampleOn ? 'Hide' : 'Show'} the example
</a>
{exampleOn && (
<div>
<img
style={{ maxHeight: 240, maxWidth: '100%' }}
src={exampleImg}
/>
</div>
)}
</div>
<div style={{ margin: '8px 16px' }}>
Alternatively, you can{' '}
<a href="#" onClick={toCameraMode}>
use the camera
</a>
</div>
</>
)
}