import React, { useState, useEffect } from 'react'; import { applyFilters } from '../extensions/registry'; const resolveImageUrl = (imgSrc) => { if (!imgSrc) return ''; if (imgSrc.startsWith('http://') || imgSrc.startsWith('https://') || imgSrc.startsWith('data:')) { return imgSrc; } if (typeof wpvfbSettings !== 'undefined' && wpvfbSettings?.pluginUrl) { const cleanPlugin = wpvfbSettings.pluginUrl.replace(/\/$/, ''); const cleanPath = imgSrc.replace(/^\//, ''); return `${cleanPlugin}/dist/${cleanPath}`; } return imgSrc; }; const TemplatePreview = ({ template, className = "", priority = false }) => { const [showFullPreview, setShowFullPreview] = useState(false); useEffect(() => { if (showFullPreview) { document.body.classList.add('template-preview-open'); } else { document.body.classList.remove('template-preview-open'); } return () => { document.body.classList.remove('template-preview-open'); }; }, [showFullPreview]); if (!template) return null; // For blank template (classic), show clean gradient background if (template.name === "Blank Form" || template.name === "blank" || template.category === "blank" || template.key === "blank") { return (
📄 Blank Canvas
); } // Extensions draw their own thumbnail for some templates (`templates.renderPreview` filter). const customPreview = applyFilters('templates.renderPreview', null, { template, className }); if (customPreview) return customPreview; // For templates with preview images, show the image with a view button if (template.previewImage) { const rawSrc = resolveImageUrl(template.previewImage); const isLocalImage = typeof template.previewImage === 'string' && template.previewImage.startsWith('/images/template/'); const webpSrc = isLocalImage ? resolveImageUrl(template.previewImage.replace(/\.(png|jpe?g)$/i, '.webp')) : null; return (
{/* Preview Image */} {webpSrc ? ( {`${template.name} ) : ( {`${template.name} )} {/* Overlay with view button */}
{/* Enhanced Full Preview Lightbox Modal */} {showFullPreview && (
setShowFullPreview(false)} > {/* Close button */} {/* Image at full height */} {`${template.name} e.stopPropagation()} />
)}
); } // Fallback preview return (
📋
Template Preview
); }; export default TemplatePreview;