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{fileName}
{defaultPlaceholder}
> )}