214 lines
7.9 KiB
TypeScript
214 lines
7.9 KiB
TypeScript
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 (
|
|
<label className="block text-xs font-semibold text-muted-foreground uppercase tracking-wider mb-1.5 select-none">
|
|
{label}
|
|
{required && <span className="text-destructive ml-0.5">*</span>}
|
|
</label>
|
|
);
|
|
}
|
|
|
|
// ─── ErrorMsg ─────────────────────────────────────────────────────────────────
|
|
|
|
function ErrorMsg({ error }: { error?: string }) {
|
|
if (!error) return null;
|
|
return (
|
|
<p className="mt-1.5 text-xs text-destructive font-medium flex items-center gap-1.5">
|
|
<span className="inline-block w-1 h-1 rounded-full bg-destructive shrink-0" />
|
|
{error}
|
|
</p>
|
|
);
|
|
}
|
|
|
|
// ─── 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<HTMLInputElement>(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 (
|
|
<div>
|
|
<FieldLabel label={label} required={required} />
|
|
|
|
{value ? (
|
|
<div className="p-3 rounded-md border border-border bg-muted/30 space-y-2">
|
|
{isImageFileName(value) ? (
|
|
<a
|
|
href={value.startsWith("http") ? value : buildFileUrl(value)}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="block overflow-hidden rounded-md border border-border bg-background"
|
|
>
|
|
<img
|
|
src={value.startsWith("http") ? value : buildFileUrl(value)}
|
|
alt={fileName}
|
|
loading="lazy"
|
|
className="w-full h-36 object-cover"
|
|
/>
|
|
</a>
|
|
) : (
|
|
<div className="flex items-center gap-2.5 min-w-0">
|
|
<div className="w-8 h-8 rounded-md bg-primary/10 flex items-center justify-center shrink-0">
|
|
<FileText size={16} className="text-primary" />
|
|
</div>
|
|
<p className="text-sm font-medium text-foreground truncate">{fileName}</p>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex items-center justify-between gap-3">
|
|
<a
|
|
href={value.startsWith("http") ? value : buildFileUrl(value)}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="text-xs text-primary hover:underline inline-flex items-center gap-1 shrink-0"
|
|
>
|
|
<ExternalLink size={11} />
|
|
{t("common.viewFile", "Faýla seret")}
|
|
</a>
|
|
|
|
{!disabled && (
|
|
<button
|
|
type="button"
|
|
onClick={() => onChange?.("")}
|
|
className="p-1.5 rounded-md text-muted-foreground hover:text-destructive hover:bg-destructive/10 transition-colors shrink-0"
|
|
title={t("common.remove", "Aýyr")}
|
|
>
|
|
<X size={16} />
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div
|
|
role="button"
|
|
tabIndex={disabled ? -1 : 0}
|
|
onClick={() => !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 ? (
|
|
<div className="flex items-center gap-2 py-1">
|
|
<Loader2 size={18} className="animate-spin text-primary" />
|
|
<span className="text-sm text-muted-foreground">{t("common.uploading", "Ýüklenýär...")}</span>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div className="w-9 h-9 rounded-full bg-primary/10 flex items-center justify-center">
|
|
<Upload size={18} className="text-primary" />
|
|
</div>
|
|
<p className="text-xs text-muted-foreground max-w-xs">{defaultPlaceholder}</p>
|
|
</>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
<input
|
|
ref={inputRef}
|
|
type="file"
|
|
accept={accept}
|
|
disabled={disabled || uploading}
|
|
onChange={(e) => handleFile(e.target.files?.[0] ?? null)}
|
|
className="hidden"
|
|
/>
|
|
|
|
<ErrorMsg error={effectiveError} />
|
|
</div>
|
|
);
|
|
}
|