'use client' import { useState, useRef } from 'react' export interface AvatarUploadProps { currentAvatarUrl?: string onUpload: (file: File) => void | Promise isLoading?: boolean size?: 'sm' | 'md' | 'lg' } export function AvatarUpload({ currentAvatarUrl, onUpload, isLoading = false, size = 'lg' }: AvatarUploadProps) { const [preview, setPreview] = useState(null) const [isDragging, setIsDragging] = useState(false) const fileInputRef = useRef(null) const sizeClasses = { sm: 'w-24 h-24', md: 'w-32 h-32', lg: 'w-40 h-40' } const handleFileSelect = (file: File) => { if (file && file.type.startsWith('image/')) { const reader = new FileReader() reader.onload = (e) => { setPreview(e.target?.result as string) } reader.readAsDataURL(file) onUpload(file) } } const handleInputChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) handleFileSelect(file) } const handleDragOver = (e: React.DragEvent) => { e.preventDefault() setIsDragging(true) } const handleDragLeave = (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) } const handleDrop = (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) const file = e.dataTransfer.files?.[0] if (file) handleFileSelect(file) } const displayUrl = preview || currentAvatarUrl return (
{/* Avatar Preview */}
{displayUrl ? ( Avatar ) : (
)} {/* Upload Overlay */}
{isLoading ? (
) : ( )}
{/* Upload Button */}

Click or drag and drop an image

JPG, PNG, GIF up to 5MB

) }