/** * AvatarUploader — avatar upload/remove control for the ProfileSettingsModal. * * Shows current avatar (or initials fallback) with "Upload" and "Remove" actions. * Uses the PUT /v1/auth/avatar and DELETE /v1/auth/avatar endpoints. * * ADDITIVE ONLY — new component, does not modify existing code. */ import React, { useRef, useState } from 'react'; import UserAvatar from './UserAvatar'; import { uploadAvatar, deleteAvatar } from '../profileApi'; import { Camera, Trash2 } from 'lucide-react'; export default function AvatarUploader({ backendUrl, token, displayName, avatarUrl, onAvatarChange, }) { const inputRef = useRef(null); const [uploading, setUploading] = useState(false); const [error, setError] = useState(''); async function handleFileChange(e) { const file = e.target.files?.[0]; if (!file) return; setError(''); setUploading(true); try { const url = await uploadAvatar(backendUrl, token, file); onAvatarChange(url); } catch (err) { setError(err?.message || 'Upload failed'); } setUploading(false); // Reset input so the same file can be re-selected if (inputRef.current) inputRef.current.value = ''; } async function handleRemove() { setError(''); try { await deleteAvatar(backendUrl, token); onAvatarChange(''); } catch (err) { setError(err?.message || 'Remove failed'); } } return (
{avatarUrl && ()}
{error && {error}} PNG, JPG, or WebP. Max 5MB.
); }