MCPcopy Create free account
hub / github.com/triggerdotdev/jsonhero-web / DragAndDropForm

Function DragAndDropForm

app/components/DragAndDropForm.tsx:7–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import invariant from "tiny-invariant";
6
7export function DragAndDropForm() {
8 const formRef = useRef<HTMLFormElement>(null);
9 const filenameInputRef = useRef<HTMLInputElement>(null);
10 const rawJsonInputRef = useRef<HTMLInputElement>(null);
11
12 const submit = useSubmit();
13
14 const onDrop = useCallback(
15 (acceptedFiles: Array<File>) => {
16 if (!formRef.current || !filenameInputRef.current) {
17 return;
18 }
19
20 if (acceptedFiles.length === 0) {
21 return;
22 }
23
24 const firstFile = acceptedFiles[0];
25
26 const reader = new FileReader();
27
28 reader.onabort = () => console.log("file reading was aborted");
29 reader.onerror = () => console.log("file reading has failed");
30 reader.onload = () => {
31 if (reader.result == null) {
32 return;
33 }
34
35 let jsonValue: string | undefined = undefined;
36
37 if (typeof reader.result === "string") {
38 jsonValue = reader.result;
39 } else {
40 const decoder = new TextDecoder("utf-8");
41 jsonValue = decoder.decode(reader.result);
42 }
43
44 invariant(rawJsonInputRef.current, "rawJsonInputRef is null");
45 invariant(jsonValue, "jsonValue is undefined");
46
47 rawJsonInputRef.current.value = jsonValue;
48
49 submit(formRef.current);
50 };
51 reader.readAsArrayBuffer(firstFile);
52 filenameInputRef.current.value = firstFile.name;
53 },
54 [formRef.current, filenameInputRef.current, rawJsonInputRef.current]
55 );
56
57 const { getRootProps, getInputProps, isDragActive } = useDropzone({
58 onDropAccepted: onDrop,
59 maxFiles: 1,
60 maxSize: 1024 * 1024 * 1,
61 multiple: false,
62 accept: "application/json",
63 });
64

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected