'use client'; import React, { useRef, useState } from 'react'; import { Upload, Loader2, Image as ImageIcon } from 'lucide-react'; import { shrinkImage, uploadFile } from '@/lib/upload'; interface ImageUploadInputProps { label?: string; value: string; onChange: (url: string) => void; placeholder?: string; // Voliteľný ručný alt text; prázdny = použije sa automatický (altPlaceholder) altValue?: string; onAltChange?: (alt: string) => void; altPlaceholder?: string; } export const ImageUploadInput: React.FC = ({ label = 'Obrázok', value, onChange, placeholder = 'https://... alebo nahrajte súbor', altValue, onAltChange, altPlaceholder }) => { const fileInputRef = useRef(null); const [isUploading, setIsUploading] = useState(false); const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setIsUploading(true); try { const small = await shrinkImage(file); onChange(await uploadFile(small, small.name)); } catch (err: any) { alert(`Chyba pri nahrávaní obrázka: ${err.message}`); } finally { setIsUploading(false); if (fileInputRef.current) { fileInputRef.current.value = ''; } } }; return (
{label && }
{/* Náhľad miniatúry */}
{value ? ( Náhľad { (e.target as HTMLElement).style.display = 'none'; }} /> ) : ( )}
{/* Textové pole s URL */} onChange(e.target.value)} placeholder={placeholder} className="flex-1 min-w-0 text-xs font-mono border border-gray-300 rounded-lg p-2 bg-white" /> {/* Tlačidlo nahrávania */}
{onAltChange && ( onAltChange(e.target.value)} placeholder={altPlaceholder ? `Alt text (automaticky: ${altPlaceholder})` : 'Alt text'} title="Alt text obrázka. Ak ho nevyplníte, doplní sa automaticky." className="w-full text-[11px] border border-gray-200 rounded-lg px-2 py-1.5 bg-white placeholder:text-gray-400" /> )}
); };