import { useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { Upload, X, Loader2 } from "lucide-react"; import { cn } from "@/lib/utils"; import { postFile, buildFileUrl } from "@/features/shared/api/sharedApi"; // ─── Types ──────────────────────────────────────────────────────────────────── interface AvatarUploadProps { label?: string; value?: string; onChange?: (url: string) => void; error?: string; required?: boolean; disabled?: boolean; } // ─── FieldLabel ─────────────────────────────────────────────────────────────── function FieldLabel({ label, required }: { label?: string; required?: boolean }) { if (!label) return null; return ( ); } // ─── ErrorMsg ───────────────────────────────────────────────────────────────── function ErrorMsg({ error }: { error?: string }) { if (!error) return null; return (

{error}

); } // ─── AvatarUpload ───────────────────────────────────────────────────────────── export function AvatarUpload({ label, value, onChange, error, required, disabled }: AvatarUploadProps) { const { t } = useTranslation(); const inputRef = useRef(null); const [dragOver, setDragOver] = useState(false); const [uploading, setUploading] = useState(false); const [uploadError, setUploadError] = useState(""); const handleFile = async (file: File | null) => { if (!file || disabled) return; if (!file.type.startsWith("image/")) { setUploadError(t("users.fields.avatarInvalid", "Diňe surat faýllary kabul edilýär")); return; } setUploading(true); setUploadError(""); try { const filename = await postFile(file); onChange?.(buildFileUrl(filename)); } catch { setUploadError(t("users.fields.avatarUploadError", "Surat ýüklenip bilinmedi")); } finally { setUploading(false); if (inputRef.current) inputRef.current.value = ""; } }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setDragOver(false); handleFile(e.dataTransfer.files[0] ?? null); }; const effectiveError = error || uploadError; return (
{/* Preview — rounded, on top */}
{value ? ( avatar ) : ( )}
{/* Upload zone — full width, below */}
!disabled && !uploading && inputRef.current?.click()} onKeyDown={(e) => e.key === "Enter" && !disabled && !uploading && inputRef.current?.click()} onDragOver={(e) => { if (disabled) return; e.preventDefault(); setDragOver(true); }} onDragLeave={() => setDragOver(false)} onDrop={handleDrop} className={cn( "flex items-center justify-center gap-2", "min-h-20 rounded-md border-2 border-dashed cursor-pointer select-none px-4", "transition-all duration-200 text-sm", "focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2", dragOver && "border-primary bg-primary/5 scale-[1.01]", effectiveError ? "border-destructive/60" : "border-border", !dragOver && !effectiveError && "hover:border-primary/50 hover:bg-muted/20", (disabled || uploading) && "opacity-60 cursor-not-allowed pointer-events-none", )} > {uploading ? ( <> {t("users.fields.avatarUploading", "Ýüklenýär...")} ) : ( <> {t("users.fields.avatarUpload", "Surat saýlaň")} )}
{value && !disabled && ( )}
handleFile(e.target.files?.[0] ?? null)} className="hidden" />
); }