Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Created July 26, 2023 14:31
Show Gist options
  • Save MonteLogic/9a1b143bea8ff6c6955fd4684f0a038d to your computer and use it in GitHub Desktop.
Save MonteLogic/9a1b143bea8ff6c6955fd4684f0a038d to your computer and use it in GitHub Desktop.
This works as an uploader for uploadthing
import { useDropzone } from "react-dropzone";
import type { FileWithPath } from "react-dropzone";
import { generateReactHelpers } from "@uploadthing/react/hooks";
import { useState, useCallback } from "react";
import { OurFileRouter } from "~/server/uploadthing";
export const { useUploadThing, uploadFiles } =
generateReactHelpers<OurFileRouter>();
export function MultiUploader() {
const [files, setFiles] = useState<File[]>([]);
const onDrop = useCallback((acceptedFiles: FileWithPath[]) => {
setFiles(acceptedFiles);
}, []);
const { getRootProps, getInputProps } = useDropzone({
onDrop,
});
console.log(files);
const { startUpload, isUploading } = useUploadThing("imageUploader", {
onClientUploadComplete: () => {
alert("uploaded successfully!");
},
onUploadError: (e) => {
console.log(e);
console.log(isUploading);
alert("error occurred while uploading");
},
});
return (
<div {...getRootProps()}>
<input {...getInputProps()} />
<div>
{files.length > 0 && (
<button onClick={() => startUpload(files)}>
Upload {files.length} files
</button>
)}
</div>
Drop files here!
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment