import { useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { Upload, X, Loader2, FileText, ExternalLink } from "lucide-react"; import { cn } from "@/lib/utils"; import { postFile, buildFileUrl } from "@/features/shared/api/sharedApi"; // ─── Types ──────────────────────────────────────────────────────────────────── interface DocumentUploadProps { label?: string; value?: string; onChange?: (url: string) => void; error?: string; required?: boolean; disabled?: boolean; accept?: string; placeholder?: string; } // ─── 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}

); } // ─── Helper: Extract Filename ──────────────────────────────────────────────── function getDisplayFilename(urlOrName: string): string { if (!urlOrName) return ""; try { const parts = urlOrName.split("/"); return decodeURIComponent(parts[parts.length - 1]); } catch { return urlOrName; } } const IMAGE_EXT_REGEX = /\.(png|jpe?g|gif|webp|bmp|svg|avif|heic|heif)$/i; function isImageFileName(urlOrName: string): boolean { const clean = urlOrName.split("?")[0].split("#")[0]; return IMAGE_EXT_REGEX.test(clean); } // ─── DocumentUpload ─────────────────────────────────────────────────────────── export function DocumentUpload({ label, value, onChange, error, required, disabled, accept = ".pdf,.doc,.docx,image/*", placeholder, }: DocumentUploadProps) { const { t } = useTranslation(); const inputRef = useRef(null); const [dragOver, setDragOver] = useState(false); const [uploading, setUploading] = useState(false); const [uploadError, setUploadError] = useState(""); const defaultPlaceholder = placeholder ?? t("common.fileUploadPlaceholder", "Faýl saýlaň ýa-da süýräp getiriň (PDF, DOCX, Surat)"); const handleFile = async (file: File | null) => { if (!file || disabled) return; setUploading(true); setUploadError(""); try { const filename = await postFile(file); const fileUrl = buildFileUrl(filename); onChange?.(fileUrl); } catch { setUploadError(t("common.fileUploadError", "Faýl ýü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; const fileName = value ? getDisplayFilename(value) : ""; return (
{value ? (
{isImageFileName(value) ? ( {fileName} ) : (

{fileName}

)}
{t("common.viewFile", "Faýla seret")} {!disabled && ( )}
) : (
!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 flex-col items-center justify-center gap-2 px-4 py-5", "rounded-md border-2 border-dashed cursor-pointer select-none text-center", "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("common.uploading", "Ýüklenýär...")}
) : ( <>

{defaultPlaceholder}

)}
)} handleFile(e.target.files?.[0] ?? null)} className="hidden" />
); }