import { useRef } from "react"; import { toast } from "sonner"; import type { Attachment } from "../lib/types"; export interface FileListProps { files: Attachment[]; onChange: (files: Attachment[]) => void; // آپلود واقعی هر فایل به سرور (باید Attachment با id برگرداند) upload: (file: File) => Promise; // حذف سمت سرور (اختیاری — بعد از حذف محلی صدا زده می‌شود) onDelete?: (a: Attachment) => void; // دانلود احراز هویت‌شده (توکن Bearer + blob — لینک خام توکن نمی‌فرستد) onDownload?: (a: Attachment) => void | Promise; label?: string; } // مدیریت چند فاکتور: آپلود مستقیم به سرور (MinIO) + دانلود/حذف فایل‌های ذخیره‌شده export function FileList({ files, onChange, upload, onDelete, onDownload, label = "افزودن فاکتور" }: FileListProps) { const inputRef = useRef(null); const remove = (i: number) => { const removed = files[i]; onChange(files.filter((_, idx) => idx !== i)); if (removed) onDelete?.(removed); }; const handleChange = async (e: React.ChangeEvent) => { const picked = Array.from(e.target.files || []); e.target.value = ""; // اجازه‌ی انتخاب دوباره‌ی همان فایل if (!picked.length) return; const added: Attachment[] = []; for (const file of picked) { try { added.push(await upload(file)); } catch (err) { toast.error(err instanceof Error ? err.message : `آپلود «${file.name}» ناموفق بود`); } } if (added.length) onChange([...files, ...added]); }; return (
{!files.length &&
هنوز فایلی انتخاب نشده است.
} {files.map((inv, i) => (
📎 {inv.name} {onDownload && ( )}
))}
); }