-
-
Save MonteLogic/9a1b143bea8ff6c6955fd4684f0a038d to your computer and use it in GitHub Desktop.
This works as an uploader for uploadthing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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