1
1
import { makeStyles } from "@material-ui/core/styles"
2
2
import { Stack } from "components/Stack/Stack"
3
- import { FC , useRef } from "react"
3
+ import { FC , DragEvent , useRef } from "react"
4
4
import UploadIcon from "@material-ui/icons/CloudUploadOutlined"
5
5
import { useClickable } from "hooks/useClickable"
6
6
import CircularProgress from "@material-ui/core/CircularProgress"
@@ -9,6 +9,32 @@ import IconButton from "@material-ui/core/IconButton"
9
9
import RemoveIcon from "@material-ui/icons/DeleteOutline"
10
10
import FileIcon from "@material-ui/icons/FolderOutlined"
11
11
12
+ const useTarDrop = (
13
+ callback : ( file : File ) => void ,
14
+ ) : {
15
+ onDragOver : ( e : DragEvent < HTMLDivElement > ) => void
16
+ onDrop : ( e : DragEvent < HTMLDivElement > ) => void
17
+ } => {
18
+ const onDragOver = ( e : DragEvent < HTMLDivElement > ) => {
19
+ e . preventDefault ( )
20
+ }
21
+
22
+ const onDrop = ( e : DragEvent < HTMLDivElement > ) => {
23
+ e . preventDefault ( )
24
+ const file = e . dataTransfer . files [ 0 ]
25
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- file can be undefined
26
+ if ( ! file || file . type !== "application/x-tar" ) {
27
+ return
28
+ }
29
+ callback ( file )
30
+ }
31
+
32
+ return {
33
+ onDragOver,
34
+ onDrop,
35
+ }
36
+ }
37
+
12
38
export interface TemplateUploadProps {
13
39
isUploading : boolean
14
40
onUpload : ( file : File ) => void
@@ -24,6 +50,7 @@ export const TemplateUpload: FC<TemplateUploadProps> = ({
24
50
} ) => {
25
51
const styles = useStyles ( )
26
52
const inputRef = useRef < HTMLInputElement > ( null )
53
+ const tarDrop = useTarDrop ( onUpload )
27
54
const clickable = useClickable ( ( ) => {
28
55
if ( inputRef . current ) {
29
56
inputRef . current . click ( )
@@ -58,6 +85,7 @@ export const TemplateUpload: FC<TemplateUploadProps> = ({
58
85
[ styles . disabled ] : isUploading ,
59
86
} ) }
60
87
{ ...clickable }
88
+ { ...tarDrop }
61
89
>
62
90
< Stack alignItems = "center" spacing = { 1 } >
63
91
{ isUploading ? (
0 commit comments