// PdfBuilder.jsx — modal flow to generate the custom-catalog PDF. // Steps: options -> generating -> done (download / share / create another). // No automatic download; the user picks the action. const { useState: usePdfState } = React; const PdfBuilder = ({ open, wines, defaultShowPrices, onClose }) => { const [step, setStep] = usePdfState('options'); const [title, setTitle] = usePdfState(''); const [subtitle, setSubtitle] = usePdfState(''); const [showPrices, setShowPrices] = usePdfState(false); const [showCata, setShowCata] = usePdfState(true); const [theme, setTheme] = usePdfState('light'); const [result, setResult] = usePdfState(null); const [error, setError] = usePdfState(''); const [shareMsg, setShareMsg] = usePdfState(''); // Reset to options each time it opens React.useEffect(() => { if (open) { setStep('options'); setTitle(''); setSubtitle(''); setResult(null); setError(''); setShareMsg(''); setShowPrices(!!defaultShowPrices); setShowCata(true); setTheme('light'); } }, [open]); React.useEffect(() => { const fn = (e) => { if (e.key === 'Escape' && step !== 'generating') onClose(); }; if (open) window.addEventListener('keydown', fn); return () => window.removeEventListener('keydown', fn); }, [open, step, onClose]); if (!open) return null; const effectiveTitle = title.trim() || 'Selección de vinos'; const doGenerate = async () => { setStep('generating'); setError(''); try { const res = await window.generateCatalogPdf({ title: effectiveTitle, subtitle: subtitle.trim(), showPrices, showCata, theme, wines, }); setResult(res); setStep('done'); } catch (e) { console.error(e); setError((e && e.message) || 'No se pudo generar el PDF.'); setStep('options'); } }; const download = () => { if (!result) return; const a = document.createElement('a'); a.href = result.url; a.download = result.filename; document.body.appendChild(a); a.click(); a.remove(); }; const share = async () => { if (!result) return; setShareMsg(''); const subject = effectiveTitle + ' · Comesa Canarias'; const outcome = await window.shareCatalogPdf(result.blob, result.filename, subject, subtitle.trim()); if (outcome === 'unsupported') { // Fallback: download the file and open the email client with a prefilled message. download(); const body = `Hola,%0D%0A%0D%0AAdjunto el catálogo "${encodeURIComponent(effectiveTitle)}".%0D%0A%0D%0AUn saludo.`; window.location.href = `mailto:?subject=${encodeURIComponent(subject)}&body=${body}`; setShareMsg('Tu navegador no permite adjuntar directamente: hemos descargado el PDF y abierto tu correo para que lo adjuntes.'); } }; const createAnother = () => { setStep('options'); setResult(null); setShareMsg(''); }; return ( <>
step !== 'generating' && onClose()} style={{ position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.72)', zIndex: 130, animation: 'fadeIn 200ms cubic-bezier(0.4,0,0.2,1)', }} />
{/* Header */}
Generar PDF · {wines.length} {wines.length === 1 ? 'producto' : 'productos'}
{step !== 'generating' && ( )}
{/* Body */} {step === 'options' && (

Opciones del catálogo

Formato horizontal · 2 productos por página · misma ficha que el catálogo.

setTitle(e.target.value)} placeholder="Selección de vinos" style={inputStyle} />