Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

add new drag and drop component

Marc Seitz4a034e732f07
diff --git a/components/document-upload.tsx b/components/document-upload.tsx
new file mode 100644
index 00000000..abb68de5
--- /dev/null
+++ b/components/document-upload.tsx
@@ -0,0 +1,103 @@
+import { useMemo } from "react";
+import { useDropzone } from "react-dropzone";
+import { ArrowUpTrayIcon, DocumentIcon, PhotoIcon } from "@heroicons/react/24/outline";
+import { DocumentTextIcon, PresentationChartBarIcon } from "@heroicons/react/20/solid";
+import { bytesToSize } from "@/lib/utils";
+
+function fileIcon(fileType: string) {
+ switch (fileType) {
+ case "application/pdf":
+ return <DocumentTextIcon className="h-6 w-6 mx-auto" />;
+ case "image/png":
+ case "image/jpeg":
+ case "image/gif":
+ case "image/jpg":
+ return <PhotoIcon className="h-6 w-6 mx-auto" />;
+ case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
+ case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
+ case "application/vnd.ms-powerpoint":
+ case "application/msword":
+ return <PresentationChartBarIcon className="h-6 w-6 mx-auto" />;
+ default:
+ return <DocumentIcon className="h-6 w-6 mx-auto" />;
+ }
+}
+
+export default function DocumentUpload({
+ currentFile,
+ setCurrentFile,
+}: {
+ currentFile: File | null;
+ setCurrentFile: React.Dispatch<React.SetStateAction<File | null>>;
+}) {
+ const { getRootProps, getInputProps } = useDropzone({
+ accept: {
+ "image/*": [".png", ".gif", ".jpeg", ".jpg"],
+ "application/pdf": [], // ".pdf"
+ "application/msword": [], // ".doc"
+ "application/vnd.ms-powerpoint": [], // ".ppt"
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
+ [], // ".docx"
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation":
+ [], // ".pptx"
+ },
+ multiple: false,
+ onDrop: (acceptedFiles) => {
+ setCurrentFile(acceptedFiles[0]);
+ console.log(acceptedFiles[0]);
+ },
+ });
+
+ const imageBlobUrl = useMemo(
+ () => (currentFile ? URL.createObjectURL(currentFile) : ""),
+ [currentFile]
+ );
+
+ return (
+ <div className="col-span-full">
+ <div
+ {...getRootProps()}
+ className="relative cursor-pointer font-semibold text-white hover:text-gray-400 hover:bg-gray-900 block group"
+ >
+ <input {...getInputProps()} name="file" className="sr-only" />
+ <div className="flex justify-center rounded-lg border border-dashed border-white/25 px-6 py-10 min-h-[200px] md:min-w-[500px] items-center">
+ {currentFile ? (
+ <div
+ className="absolute inset-0 opacity-10 pointer-events-none group-hover:opacity-5 transition-opacity"
+ style={{
+ backgroundImage: `url(${imageBlobUrl})`,
+ backgroundPosition: "center",
+ backgroundSize: "cover",
+ }}
+ />
+ ) : null}
+ <div className="text-center">
+ {currentFile ? (
+ <div className="flex flex-col sm:flex-row items-center space-y-1 sm:space-x-2 text-white">
+ <div>{fileIcon(currentFile.type)}</div>
+ <p>{currentFile.name}</p>
+ <p className="text-gray-500">{bytesToSize(currentFile.size)}</p>
+ </div>
+ ) : (
+ <ArrowUpTrayIcon
+ className="mx-auto h-12 w-12 text-gray-500"
+ aria-hidden="true"
+ />
+ )}
+
+ <div className="mt-4 flex text-sm leading-6 text-gray-400">
+ <span className="mx-auto">
+ {currentFile ? "" : "Choose file to upload or drag and drop"}
+ </span>
+ </div>
+ <p className="text-xs leading-5 text-gray-400">
+ {currentFile
+ ? "Replace file?"
+ : ".pdf, .docx, .pptx, image files and more"}
+ </p>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/lib/utils.ts b/lib/utils.ts
index b2bd8f67..a6ccf0da 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -28,10 +28,7 @@ export async function fetcher<JSON = any>(
return res.json();
}
-
-export const log = async (
- message: string,
-) => {
+export const log = async (message: string) => {
/* Log a message to the console */
try {
return await fetch(`${process.env.PPMK_SLACK_WEBHOOK_URL}`, {
@@ -54,4 +51,16 @@ export const log = async (
} catch (e) {
console.log(`Failed to log to Dub Slack. Error: ${e}`);
}
-};
\ No newline at end of file
+};
+
+export function bytesToSize(bytes: number) {
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
+ if (bytes === 0) return "n/a";
+ const i = Math.floor(Math.log(bytes) / Math.log(1000));
+ if (i === 0) return `${bytes} ${sizes[i]}`;
+ const sizeInCurrentUnit = bytes / Math.pow(1000, i);
+ if (sizeInCurrentUnit >= 1000 && i < sizes.length - 1) {
+ return `1 ${sizes[i + 1]}`;
+ }
+ return `${Math.round(sizeInCurrentUnit)} ${sizes[i]}`;
+}
diff --git a/package-lock.json b/package-lock.json
index f35cb438..3121cb6c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -27,6 +27,7 @@
"postcss": "8.4.23",
"react": "18.2.0",
"react-dom": "18.2.0",
+ "react-dropzone": "^14.2.3",
"react-hot-toast": "^2.4.1",
"react-pdf": "^7.0.1",
"swr": "^2.1.5",
@@ -876,6 +877,14 @@
"resolved": "https://registry.npmjs.org/ast-types-flow/-/ast-types-flow-0.0.7.tgz",
"integrity": "sha512-eBvWn1lvIApYMhzQMsu9ciLfkBY499mFZlNqG+/9WR7PVlroQw0vG30cOQQbaKz3sCEc44TAOu2ykzqXSNnwag=="
},
+ "node_modules/attr-accept": {
+ "version": "2.2.2",
+ "resolved": "https://registry.npmjs.org/attr-accept/-/attr-accept-2.2.2.tgz",
+ "integrity": "sha512-7prDjvt9HmqiZ0cl5CRjtS84sEyhsHP2coDkaZKRKVfCDo9s7iw7ChVmar78Gu9pC4SoR/28wFu/G5JJhTnqEg==",
+ "engines": {
+ "node": ">=4"
+ }
+ },
"node_modules/autoprefixer": {
"version": "10.4.14",
"resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.14.tgz",
@@ -2094,6 +2103,17 @@
"node": "^10.12.0 || >=12.0.0"
}
},
+ "node_modules/file-selector": {
+ "version": "0.6.0",
+ "resolved": "https://registry.npmjs.org/file-selector/-/file-selector-0.6.0.tgz",
+ "integrity": "sha512-QlZ5yJC0VxHxQQsQhXvBaC7VRJ2uaxTf+Tfpu4Z/OcVQJVpZO+DGU0rkoVW5ce2SccxugvpBJoMvUs59iILYdw==",
+ "dependencies": {
+ "tslib": "^2.4.0"
+ },
+ "engines": {
+ "node": ">= 12"
+ }
+ },
"node_modules/fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@@ -4051,6 +4071,22 @@
"react": "^18.2.0"
}
},
+ "node_modules/react-dropzone": {
+ "version": "14.2.3",
+ "resolved": "https://registry.npmjs.org/react-dropzone/-/react-dropzone-14.2.3.tgz",
+ "integrity": "sha512-O3om8I+PkFKbxCukfIR3QAGftYXDZfOE2N1mr/7qebQJHs7U+/RSL/9xomJNpRg9kM5h9soQSdf0Gc7OHF5Fug==",
+ "dependencies": {
+ "attr-accept": "^2.2.2",
+ "file-selector": "^0.6.0",
+ "prop-types": "^15.8.1"
+ },
+ "engines": {
+ "node": ">= 10.13"
+ },
+ "peerDependencies": {
+ "react": ">= 16.8 || 18.0.0"
+ }
+ },
"node_modules/react-hot-toast": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/react-hot-toast/-/react-hot-toast-2.4.1.tgz",
@@ -5732,6 +5768,11 @@
"resolved": "https://registry.npmjs.org/ast-types-flow/-/ast-types-flow-0.0.7.tgz",
"integrity": "sha512-eBvWn1lvIApYMhzQMsu9ciLfkBY499mFZlNqG+/9WR7PVlroQw0vG30cOQQbaKz3sCEc44TAOu2ykzqXSNnwag=="
},
+ "attr-accept": {
+ "version": "2.2.2",
+ "resolved": "https://registry.npmjs.org/attr-accept/-/attr-accept-2.2.2.tgz",
+ "integrity": "sha512-7prDjvt9HmqiZ0cl5CRjtS84sEyhsHP2coDkaZKRKVfCDo9s7iw7ChVmar78Gu9pC4SoR/28wFu/G5JJhTnqEg=="
+ },
"autoprefixer": {
"version": "10.4.14",
"resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.14.tgz",
@@ -6602,6 +6643,14 @@
"flat-cache": "^3.0.4"
}
},
+ "file-selector": {
+ "version": "0.6.0",
+ "resolved": "https://registry.npmjs.org/file-selector/-/file-selector-0.6.0.tgz",
+ "integrity": "sha512-QlZ5yJC0VxHxQQsQhXvBaC7VRJ2uaxTf+Tfpu4Z/OcVQJVpZO+DGU0rkoVW5ce2SccxugvpBJoMvUs59iILYdw==",
+ "requires": {
+ "tslib": "^2.4.0"
+ }
+ },
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@@ -7879,6 +7928,16 @@
"scheduler": "^0.23.0"
}
},
+ "react-dropzone": {
+ "version": "14.2.3",
+ "resolved": "https://registry.npmjs.org/react-dropzone/-/react-dropzone-14.2.3.tgz",
+ "integrity": "sha512-O3om8I+PkFKbxCukfIR3QAGftYXDZfOE2N1mr/7qebQJHs7U+/RSL/9xomJNpRg9kM5h9soQSdf0Gc7OHF5Fug==",
+ "requires": {
+ "attr-accept": "^2.2.2",
+ "file-selector": "^0.6.0",
+ "prop-types": "^15.8.1"
+ }
+ },
"react-hot-toast": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/react-hot-toast/-/react-hot-toast-2.4.1.tgz",
diff --git a/package.json b/package.json
index d6e3f34b..6567e143 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
"postcss": "8.4.23",
"react": "18.2.0",
"react-dom": "18.2.0",
+ "react-dropzone": "^14.2.3",
"react-hot-toast": "^2.4.1",
"react-pdf": "^7.0.1",
"swr": "^2.1.5",