-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFileUpload.tsx
73 lines (65 loc) · 2.1 KB
/
FileUpload.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use client'
import { uploadToS3 } from '@/lib/s3'
import { Inbox, Loader2 } from 'lucide-react'
import { useState } from 'react'
import { useDropzone } from 'react-dropzone'
import { toast } from 'react-hot-toast'
import { createChat } from './utils'
import { useRouter } from 'next/navigation'
const FileUpload = () => {
const [isUploading, setIsUploading] = useState(false)
const [isCreatingChat, setIsCreatingChat] = useState(false)
const router = useRouter()
const { getRootProps, getInputProps } = useDropzone({
accept: {
'application/pdf': ['.pdf'],
},
maxFiles: 1,
onDrop: async (acceptedFiles) => {
const file = acceptedFiles[0]
if (file.size > 10 * 1024 * 1024) {
toast.error('File too large (limit: 10MB)')
return
}
try {
setIsUploading(true)
const fileKey = await uploadToS3(acceptedFiles[0])
toast.success('File uploaded successfully!')
setIsCreatingChat(true)
const chatId = await createChat(fileKey)
toast.success('Chat created successfully!')
router.push(`/chat/${chatId}`)
} catch (err) {
toast.error('Something went wrong ...')
console.log(err)
} finally {
setIsUploading(false)
setIsCreatingChat(false)
}
},
})
return (
<div className="p-2 bg-white rounded-2xl">
<div
{...getRootProps({
className:
'border-dashed border-2 rounded-xl cursor-pointer bg-gray-50 py-8 flex justify-center items-center flex-col hover:border-blue-300 focus:border-blue-300 focus:border-solid outline-none',
})}
>
<input {...getInputProps()} />
{isUploading || isCreatingChat ? (
<>
<Loader2 className="w-10 h-10 text-blue-500 animate-spin" />
<p className="mt-2 text-sm text-slate-400">Uploading...</p>
</>
) : (
<>
<Inbox className="w-10 h-10 text-blue-500" />
<p className="mt-2 text-sm text-slate-400">Drop PDF Here</p>
</>
)}
</div>
</div>
)
}
export default FileUpload