import { useFileUpload } from '@/lib/hooks/useFileUpload'; import { useAuth } from '@/components/context'; import { Avatar, AvatarFallback, AvatarImage, CardContent, } from '@/components/ui'; import { Loader2, Pencil, Upload, User } from 'lucide-react'; type AvatarUploadProps = { onAvatarUploaded: (path: string) => Promise; }; export const AvatarUpload = ({ onAvatarUploaded }: AvatarUploadProps) => { const { profile, avatarUrl } = useAuth(); const { isUploading, fileInputRef, uploadToStorage } = useFileUpload(); const handleAvatarClick = () => { fileInputRef.current?.click(); }; const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const result = await uploadToStorage({ file, bucket: 'avatars', resize: true, options: { maxWidth: 500, maxHeight: 500, quality: 0.8, }, replace: { replace: true, path: profile?.avatar_url ?? file.name }, }); if (result.success && result.data) { await onAvatarUploaded(result.data); } }; const getInitials = (name: string | null | undefined): string => { if (!name) return ''; return name .split(' ') .map((n) => n[0]) .join('') .toUpperCase(); }; return (
{avatarUrl ? ( ) : ( {profile?.full_name ? ( getInitials(profile.full_name) ) : ( )} )}
{isUploading && (
Uploading...
)}
); };