import { useRef, useState } from 'react' import { ImagePlusIcon, Loader2Icon } from 'lucide-react' import { adminApi } from '../../admin/adminClient' import { useAdmin } from '../../admin/AdminContext' type ImageFieldProps = { label: string value: string onSave: (url: string) => Promise } /** * انتخاب و آپلود تصویر به MinIO — در کشوهای ویرایش استفاده می‌شود. * پس از آپلود موفق، URL نسبی /media/... ذخیره می‌شود. */ export function ImageField({ label, value, onSave }: ImageFieldProps) { const { runSave, isAuthenticated, isEditMode } = useAdmin() const inputRef = useRef(null) const [uploading, setUploading] = useState(false) if (!isAuthenticated || !isEditMode) return null const pick = async (file: File | undefined) => { if (!file) return setUploading(true) try { await runSave(async () => { const { url } = await adminApi.upload(file) const ok = await onSave(url) if (!ok) throw new Error('ذخیره نشد') }) } finally { setUploading(false) } } return (

{label}

{value ? ( {label} ) : (
—
)}
{value &&

{value}

}
{ void pick(e.target.files?.[0]) e.target.value = '' }} />
) }