@@ -8,22 +8,23 @@ import { LuUpload } from "react-icons/lu"
88import type { MapElement } from "@/types/utils"
99
1010const allowedTypes = [ "image/png" , "image/jpeg" , "image/jpg" ]
11- const maxFileSize = 10 * 1024 * 1024 // 10 MB
11+ const maxFileSize = 10 * 1024 * 1024 // 10MB
1212
13- export default function DropZone ( {
13+ export function DropZone ( {
1414 setData,
1515 className = "" ,
1616 value = "" ,
17- aspectRatio = "1"
17+ onSuccess
1818} : {
1919 setData : ( url : string ) => void
20+ onSuccess ?: ( ) => void
2021 className ?: string
2122 value ?: string
2223 aspectRatio ?: string
2324} ) {
2425 const [ isDragging , setIsDragging ] = useState ( false )
2526 const [ file , setFile ] = useState < File | null > ( null )
26- const [ imageUrl , setImageUrl ] = useState < string | null > ( null )
27+ const [ imageUrl , setImageUrl ] = useState < string | null > ( value || null )
2728 const [ uploading , setUploading ] = useState ( false )
2829 const [ error , setError ] = useState < string | null > ( null )
2930 const [ success , setSuccess ] = useState ( false )
@@ -34,12 +35,6 @@ export default function DropZone({
3435 if ( fileUploadRef . current ) fileUploadRef . current . value = ""
3536 } , [ file ] )
3637
37- useEffect ( ( ) => {
38- const handleDragOver = ( e : DragEvent ) => e . preventDefault ( )
39- window . addEventListener ( "dragover" , handleDragOver )
40- return ( ) => window . removeEventListener ( "dragover" , handleDragOver )
41- } , [ ] )
42-
4338 const handleDrag = ( e : React . DragEvent ) => {
4439 e . preventDefault ( )
4540 e . stopPropagation ( )
@@ -59,7 +54,7 @@ export default function DropZone({
5954
6055 const processFile = ( uploadedFile : File ) => {
6156 if ( ! allowedTypes . includes ( uploadedFile . type ) ) {
62- return setError ( "Invalid file type." )
57+ return setError ( "Invalid file type. Please upload a PNG or JPEG image. " )
6358 }
6459
6560 if ( uploadedFile . size > maxFileSize ) {
@@ -85,10 +80,7 @@ export default function DropZone({
8580 credentials : "include"
8681 } )
8782
88- if ( ! res . ok )
89- throw new Error (
90- res . status === 401 ? "Are you logged in?" : "Upload failed"
91- )
83+ if ( ! res . ok ) throw new Error ( "Upload failed. Please try again." )
9284
9385 const data = await res . json ( )
9486 setData ( data . url )
@@ -98,47 +90,38 @@ export default function DropZone({
9890 setError ( err instanceof Error ? err . message : "Unknown error occurred" )
9991 } finally {
10092 setUploading ( false )
93+ // onSuccess()
10194 }
10295 }
10396
10497 return (
10598 < div
10699 className = { cn (
107- "rounded-md border-2 border-dashed p-10 text-center transition-colors" ,
108- isDragging ? "bg-gray-300" : "bg-gray-100" ,
100+ "flex flex-col items-center justify-center rounded-lg text-center cursor-pointer z-10 bg-100 w-fit p-8" ,
109101 className
110102 ) }
111103 onDragEnter = { handleDrag }
112104 onDragOver = { handleDrag }
113105 onDragLeave = { handleDrag }
114106 onDrop = { handleDrop }
107+ onClick = { ( ) => fileUploadRef . current ?. click ( ) }
115108 >
116109 < input
117110 ref = { fileUploadRef }
118111 type = "file"
119112 className = "hidden"
120113 onChange = { handleFileInputChange }
114+ accept = { allowedTypes . join ( ", " ) }
121115 />
122116 { imageUrl ? (
123- < div className = "flex flex-col items-center" >
124- < Image width = { 200 } height = { 200 } alt = "Uploaded" src = { imageUrl } />
125- < span className = "text-lg font-bold" > Uploaded!</ span >
126- </ div >
117+ < Image width = { 200 } height = { 200 } alt = "Uploaded" src = { imageUrl } className = "rounded-md" />
127118 ) : uploading ? (
128119 < span className = "text-lg font-bold" > Uploading...</ span >
129120 ) : (
130- < div className = "flex flex-col items-center" >
131- < button
132- className = "mb-6 flex items-center justify-center rounded-full bg-gray-200 p-8"
133- onClick = { ( ) => fileUploadRef . current ?. click ( ) }
134- >
135- < LuUpload size = { 48 } />
136- </ button >
137- < span className = "text-lg font-bold" > Drag and drop files here</ span >
138- < span className = "mt-4" >
139- Max size: 10MB, Supported formats: .jpg, .png
140- </ span >
141- { error && < span className = "text-red-500" > { error } </ span > }
121+ < div className = "flex flex-col items-center justify-center text-sm cursor-pointer z-10 border-2 border-dashed aspect-square p-5 bg-100 rounded-lg" >
122+ < span > Drag and drop files</ span >
123+ < span > here or browse files</ span >
124+ { error && < span className = "text-red-500 mt-2" > { error } </ span > }
142125 </ div >
143126 ) }
144127 </ div >
0 commit comments