face-recognition / frontend /src /components /ImageUpload.jsx
orik-ss's picture
Claude Opus 4.5
Initial deployment - Face verification demo
ccb7e46
Raw History Blame Contribute Delete
5.03 kB
import { useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
import { motion, AnimatePresence } from 'framer-motion'
import { Upload, X, Image as ImageIcon, User } from 'lucide-react'
function ImageUpload({ label, image, onImageSelect, onClear }) {
const onDrop = useCallback((acceptedFiles) => {
const file = acceptedFiles[0]
if (file) {
const preview = URL.createObjectURL(file)
onImageSelect({ file, preview })
}
}, [onImageSelect])
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: {
'image/*': ['.jpeg', '.jpg', '.png', '.webp']
},
maxFiles: 1,
maxSize: 10 * 1024 * 1024 // 10MB
})
return (
<div className="relative">
<label className="block text-sm font-medium text-gray-400 mb-3 flex items-center gap-2">
<User className="w-4 h-4" />
{label}
</label>
<AnimatePresence mode="wait">
{!image ? (
<motion.div
key="dropzone"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
{...getRootProps()}
className={`
upload-zone relative cursor-pointer
border-2 border-dashed rounded-2xl p-8
transition-all duration-300 min-h-[300px]
flex flex-col items-center justify-center
${isDragActive
? 'active border-primary-500 bg-primary-500/10'
: 'border-gray-700 hover:border-gray-600'
}
`}
>
<input {...getInputProps()} />
{/* Animated upload icon */}
<motion.div
animate={isDragActive ? { scale: 1.1, y: -5 } : { scale: 1, y: 0 }}
transition={{ type: 'spring', stiffness: 300 }}
className="relative mb-6"
>
<div className="w-20 h-20 rounded-2xl bg-gradient-to-br from-primary-500/20 to-purple-500/20 flex items-center justify-center">
<Upload className={`w-10 h-10 ${isDragActive ? 'text-primary-400' : 'text-gray-500'}`} />
</div>
{isDragActive && (
<motion.div
initial={{ scale: 0.8, opacity: 0 }}
animate={{ scale: 1.5, opacity: 0 }}
transition={{ repeat: Infinity, duration: 1 }}
className="absolute inset-0 rounded-2xl border-2 border-primary-500"
/>
)}
</motion.div>
<p className="text-gray-300 font-medium mb-2">
{isDragActive ? 'Drop image here' : 'Drag & drop an image'}
</p>
<p className="text-sm text-gray-500">
or <span className="text-primary-400 hover:text-primary-300">browse files</span>
</p>
<p className="text-xs text-gray-600 mt-4">
Supports: JPG, PNG, WebP (max 10MB)
</p>
</motion.div>
) : (
<motion.div
key="preview"
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="relative rounded-2xl overflow-hidden group min-h-[300px] bg-dark-200"
>
{/* Image preview */}
<img
src={image.preview}
alt="Preview"
className="w-full h-[300px] object-cover"
/>
{/* Overlay on hover */}
<div className="absolute inset-0 bg-gradient-to-t from-black/80 via-black/20 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300" />
{/* Clear button */}
<motion.button
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
onClick={(e) => {
e.stopPropagation()
onClear()
}}
className="absolute top-3 right-3 w-10 h-10 rounded-full bg-black/50 backdrop-blur-sm flex items-center justify-center text-white hover:bg-red-500/80 transition-colors"
>
<X className="w-5 h-5" />
</motion.button>
{/* File info */}
<div className="absolute bottom-0 left-0 right-0 p-4 opacity-0 group-hover:opacity-100 transition-opacity">
<div className="flex items-center gap-2 text-sm text-white">
<ImageIcon className="w-4 h-4" />
<span className="truncate">{image.file.name}</span>
</div>
</div>
{/* Success indicator */}
<div className="absolute top-3 left-3">
<div className="w-3 h-3 rounded-full bg-green-500 shadow-lg shadow-green-500/50" />
</div>
</motion.div>
)}
</AnimatePresence>
</div>
)
}
export default ImageUpload