2022-11-18 01:36:53 -05:00
|
|
|
import { useMediaQuery, useTheme, useToasts } from "@geist-ui/core/dist"
|
2022-04-09 20:48:19 -04:00
|
|
|
import { useDropzone } from "react-dropzone"
|
|
|
|
import styles from "./drag-and-drop.module.css"
|
|
|
|
import generateUUID from "@lib/generate-uuid"
|
|
|
|
import {
|
|
|
|
allowedFileTypes,
|
|
|
|
allowedFileNames,
|
|
|
|
allowedFileExtensions
|
|
|
|
} from "@lib/constants"
|
2022-04-13 00:14:10 -04:00
|
|
|
import byteToMB from "@lib/byte-to-mb"
|
2022-11-14 20:24:35 -05:00
|
|
|
import type { Document } from "../new"
|
2022-03-11 18:38:37 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
function FileDropzone({ setDocs }: { setDocs: (docs: Document[]) => void }) {
|
|
|
|
const { palette } = useTheme()
|
|
|
|
const { setToast } = useToasts()
|
2022-04-22 18:42:54 -04:00
|
|
|
const isMobile = useMediaQuery("xs", {
|
|
|
|
match: "down"
|
|
|
|
})
|
2022-04-09 20:48:19 -04:00
|
|
|
const onDrop = async (acceptedFiles: File[]) => {
|
|
|
|
const newDocs = await Promise.all(
|
|
|
|
acceptedFiles.map((file) => {
|
|
|
|
return new Promise<Document>((resolve) => {
|
|
|
|
const reader = new FileReader()
|
2022-03-11 18:38:37 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
reader.onabort = () =>
|
|
|
|
setToast({ text: "File reading was aborted", type: "error" })
|
|
|
|
reader.onerror = () =>
|
|
|
|
setToast({ text: "File reading failed", type: "error" })
|
|
|
|
reader.onload = () => {
|
|
|
|
const content = reader.result as string
|
|
|
|
resolve({
|
|
|
|
title: file.name,
|
|
|
|
content,
|
|
|
|
id: generateUUID()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
reader.readAsText(file)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
2022-03-11 18:38:37 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
setDocs(newDocs)
|
|
|
|
}
|
2022-03-11 21:48:40 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const validator = (file: File) => {
|
|
|
|
// TODO: make this configurable
|
|
|
|
const maxFileSize = 50000000
|
|
|
|
if (file.size > maxFileSize) {
|
|
|
|
return {
|
|
|
|
code: "file-too-big",
|
|
|
|
message:
|
|
|
|
"File is too big. Maximum file size is " +
|
|
|
|
byteToMB(maxFileSize) +
|
|
|
|
" MB."
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 03:11:02 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
// We initially try to use the browser provided mime type, and then fall back to file names and finally extensions
|
|
|
|
if (
|
|
|
|
allowedFileTypes.includes(file.type) ||
|
|
|
|
allowedFileNames.includes(file.name) ||
|
|
|
|
allowedFileExtensions.includes(file.name?.split(".").pop() || "")
|
|
|
|
) {
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
code: "not-plain-text",
|
|
|
|
message: `Only plain text files are allowed.`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 15:04:29 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const { getRootProps, getInputProps, isDragActive, fileRejections } =
|
|
|
|
useDropzone({ onDrop, validator })
|
2022-03-11 18:38:37 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const fileRejectionItems = fileRejections.map(({ file, errors }) => (
|
|
|
|
<li key={file.name}>
|
|
|
|
{file.name}:
|
|
|
|
<ul>
|
|
|
|
{errors.map((e) => (
|
2022-11-18 01:36:53 -05:00
|
|
|
<li key={e.code}>{e.message}</li>
|
2022-04-09 20:48:19 -04:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
))
|
2022-03-11 18:38:37 -05:00
|
|
|
|
2022-04-22 18:42:54 -04:00
|
|
|
const verb = isMobile ? "tap" : "click"
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
|
|
|
<div
|
|
|
|
{...getRootProps()}
|
|
|
|
className={styles.dropzone}
|
|
|
|
style={{
|
|
|
|
borderColor: palette.accents_3
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<input {...getInputProps()} />
|
|
|
|
{!isDragActive && (
|
2022-11-18 01:36:53 -05:00
|
|
|
<p style={{ color: "var(--gray)" }}>
|
|
|
|
Drag some files here, or {verb} to select files
|
|
|
|
</p>
|
2022-04-09 20:48:19 -04:00
|
|
|
)}
|
2022-11-16 01:52:25 -05:00
|
|
|
{isDragActive && <p>Release to drop the files here</p>}
|
2022-04-09 20:48:19 -04:00
|
|
|
</div>
|
|
|
|
{fileRejections.length > 0 && (
|
|
|
|
<ul className={styles.error}>
|
|
|
|
{/* <Button style={{ float: 'right' }} type="abort" onClick={() => fileRejections.splice(0, fileRejections.length)} auto iconRight={<XCircle />}></Button> */}
|
2022-11-16 01:52:25 -05:00
|
|
|
<p>There was a problem with one or more of your files.</p>
|
2022-04-09 20:48:19 -04:00
|
|
|
{fileRejectionItems}
|
|
|
|
</ul>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-11 18:38:37 -05:00
|
|
|
}
|
|
|
|
|
2022-11-12 04:28:06 -05:00
|
|
|
export default FileDropzone
|