import { useState } from "react"; import { Avatar } from "./Avatar.tsx"; interface AvatarInputProps { currentAvatarUrl?: string; onChange: (file: File | null) => void; } export function AvatarInput( { currentAvatarUrl, onChange }: AvatarInputProps, ) { const [previewUrl, setPreviewUrl] = useState(null); const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { // Create preview URL const objectUrl = URL.createObjectURL(file); setPreviewUrl(objectUrl); onChange(file); // Clean up previous preview URL return () => { if (previewUrl) { URL.revokeObjectURL(previewUrl); } }; } else { setPreviewUrl(null); onChange(null); } }; const displayUrl = previewUrl || currentAvatarUrl; return (
); }