forked from TheArcus02/Better-GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-uploader.tsx
102 lines (94 loc) · 2.81 KB
/
image-uploader.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use client'
import { PlusSquare, Replace } from 'lucide-react'
import { CldImage, CldUploadWidget } from 'next-cloudinary'
import { toast } from './ui/use-toast'
import { cn } from '@/lib/utils'
interface ImageUploaderProps {
onValueChange: (value: string) => void
setImage: React.Dispatch<any>
publicId: string
image: any
disabled?: boolean
}
const ImageUploader = ({
image,
onValueChange,
publicId,
setImage,
disabled,
}: ImageUploaderProps) => {
const handleUploadSuccess = (result: any) => {
setImage((prev: any) => ({
...prev,
publicId: result?.info?.public_id,
width: result?.info?.width,
height: result?.info?.height,
secureURL: result?.info?.secure_url,
}))
onValueChange(result?.info?.public_id)
toast({
title: 'Image uploaded successfully',
})
}
const handleUploadError = () => {
toast({
title: 'Failed to upload image',
variant: 'destructive',
})
}
return (
<CldUploadWidget
uploadPreset='better_gpt'
options={{
multiple: false,
resourceType: 'image',
}}
onSuccess={handleUploadSuccess}
onError={handleUploadError}
>
{({ open }) =>
publicId ? (
<div
onClick={() => open()}
className={cn(
'aspect-square cursor-pointer relative mx-auto max-w-xs rounded-xl group',
disabled && 'pointer-events-none',
)}
>
<div className='opacity-0 group-hover:opacity-100 transition-opacity ease-in absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 flex items-center justify-center flex-col'>
<Replace className='text-foreground' />
<p className='text-xs text-foreground mt-2'>
Change photo
</p>
</div>
<CldImage
fill
alt='image'
src={publicId}
className='w-full h-64 object-cover rounded-lg mb-4 group-hover:opacity-30 transition-opacity ease-in'
/>
</div>
) : (
<div
onClick={() => open()}
className={cn(
'aspect-square cursor-pointer mx-auto max-w-xs rounded-xl border bg-secondary shadow-inner',
disabled && 'pointer-events-none',
)}
>
<div className='flex items-center group justify-center flex-col h-full rounded-xl shadow-sm shadow-secondary'>
<PlusSquare
size={48}
className='text-muted-foreground group-hover:animate-bounce'
/>
<p className='text-muted-foreground text-sm mt-2'>
Click here to upload image
</p>
</div>
</div>
)
}
</CldUploadWidget>
)
}
export default ImageUploader