-
Notifications
You must be signed in to change notification settings - Fork 568
/
Copy pathDragAndDropForm.tsx
102 lines (89 loc) · 3.2 KB
/
DragAndDropForm.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 { ArrowCircleDownIcon } from "@heroicons/react/outline";
import { useCallback, useRef, useState } from "react";
import { useDropzone } from "react-dropzone";
import { Form, useSubmit } from "remix";
import invariant from "tiny-invariant";
import ToastPopover from "./UI/ToastPopover";
export function DragAndDropForm() {
const formRef = useRef<HTMLFormElement>(null);
const filenameInputRef = useRef<HTMLInputElement>(null);
const rawJsonInputRef = useRef<HTMLInputElement>(null);
const [error, setError] = useState<string | null>(null);
const submit = useSubmit();
const onDrop = useCallback(
(acceptedFiles: Array<File>) => {
if (!formRef.current || !filenameInputRef.current) {
return;
}
if (acceptedFiles.length === 0) {
return;
}
const firstFile = acceptedFiles[0];
const reader = new FileReader();
reader.onabort = () => console.log("file reading was aborted");
reader.onerror = () => console.log("file reading has failed");
reader.onload = () => {
if (reader.result == null) {
return;
}
let jsonValue: string | undefined = undefined;
if (typeof reader.result === "string") {
jsonValue = reader.result;
} else {
const decoder = new TextDecoder("utf-8");
jsonValue = decoder.decode(reader.result);
}
invariant(rawJsonInputRef.current, "rawJsonInputRef is null");
invariant(jsonValue, "jsonValue is undefined");
rawJsonInputRef.current.value = jsonValue;
submit(formRef.current);
};
reader.readAsArrayBuffer(firstFile);
filenameInputRef.current.value = firstFile.name;
},
[formRef.current, filenameInputRef.current, rawJsonInputRef.current]
);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDropAccepted: onDrop,
maxFiles: 1,
maxSize: 1024 * 1024 * 1,
multiple: false,
accept: "application/json",
onDropRejected: (fileRejections) => {
setError(fileRejections[0].errors[0].message);
setTimeout(() => setError(null), 4000);
},
});
return (
<Form method="post" action="/actions/createFromFile" ref={formRef}>
<div
{...getRootProps()}
className="block min-w-[300px] cursor-pointer rounded-md border-2 border-dashed border-slate-600 bg-slate-900/40 p-4 text-base text-slate-300 focus:border-indigo-500 focus:ring-indigo-500"
>
<input {...getInputProps()} />
<div className="flex items-center">
<ArrowCircleDownIcon
className={`mr-3 inline h-6 w-6 ${
isDragActive ? "text-lime-500" : ""
}`}
/>
<p className={`${isDragActive ? "text-lime-500" : ""}`}>
{isDragActive
? "Now drop to open it…"
: "Drop a JSON file here, or click to select"}
</p>
</div>
<input type="hidden" name="filename" ref={filenameInputRef} />
<input type="hidden" name="rawJson" ref={rawJsonInputRef} />
</div>
{error && (
<ToastPopover
message={error}
title="Error"
type="error"
key={Date.now()}
/>
)}
</Form>
);
}