"use client"; import React, { useState, useEffect } from 'react'; import { X, Sparkles, Copy, Check, Shuffle, Eye, FileText, Paperclip, Film, MessageSquare } from 'lucide-react'; interface Template { id: number; name: string; content: string; media_url?: string | null; } interface TemplatePreviewModalProps { isOpen: boolean; onClose: () => void; template: Template | null; onEdit?: (template: Template) => void; } // Spintax spin parser helper (preserves newlines and paragraphs) function spinText(text: string): string { if (!text) return ''; const regex = /\{([^{}]+)\}/; let spun = text; while (regex.test(spun)) { spun = spun.replace(regex, (_, choices) => { const options = choices.split('|'); return options[Math.floor(Math.random() * options.length)].trim(); }); } return spun; } // Replace template variables with realistic sample data function applySampleVariables(text: string): string { if (!text) return ''; return text .replace(/\{\{First Name\}\}/gi, "Alex") .replace(/\{\{name\}\}/gi, "Alex") .replace(/\{\{Group Name\}\}/gi, "Web3 Founders Hub") .replace(/\{\{group\}\}/gi, "Web3 Founders Hub") .replace(/\{\{Username\}\}/gi, "@alex_growth"); } export function TemplatePreviewModal({ isOpen, onClose, template, onEdit }: TemplatePreviewModalProps) { const [activeTab, setActiveTab] = useState<'preview' | 'variations' | 'telegram'>('preview'); const [currentSpun, setCurrentSpun] = useState(''); const [sampleVariations, setSampleVariations] = useState([]); const [copiedIndex, setCopiedIndex] = useState(null); const [copiedRaw, setCopiedRaw] = useState(false); useEffect(() => { if (template) { regenerateVariations(); } }, [template]); const regenerateVariations = () => { if (!template) return; const isSpun = template.content.includes('{') && template.content.includes('|'); const spun = spinText(template.content); setCurrentSpun(spun); if (!isSpun) { setSampleVariations([template.content]); return; } // Generate up to 5 distinct variations const set = new Set(); let attempts = 0; while (set.size < 5 && attempts < 35) { set.add(spinText(template.content)); attempts++; } setSampleVariations(Array.from(set)); }; if (!isOpen || !template) return null; // Parse media files let mediaUrls: string[] = []; if (template.media_url) { try { if (template.media_url.startsWith('[') && template.media_url.endsWith(']')) { mediaUrls = JSON.parse(template.media_url); } else if (template.media_url.includes(',')) { mediaUrls = template.media_url.split(',').map(s => s.trim()).filter(Boolean); } else { mediaUrls = [template.media_url]; } } catch (e) { mediaUrls = [template.media_url]; } } const isSpintax = template.content.includes('{') && template.content.includes('|'); const copyToClipboard = (text: string, index?: number) => { navigator.clipboard.writeText(text); if (index !== undefined) { setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); } else { setCopiedRaw(true); setTimeout(() => setCopiedRaw(false), 2000); } }; return (
{/* Backdrop */}
{/* Modal Dialog Card */}
{/* Header */}

{template.name}

#{template.id}

Template Preview & Spintax Variations Inspector

{/* Tab Switcher */}
{/* Tab Bodies Container */}
{/* Tab 1: Content & Media */} {activeTab === 'preview' && (
{/* Attached Media Gallery */} {mediaUrls.length > 0 && (
Attached Media Payloads ({mediaUrls.length})
{mediaUrls.map((url, i) => { const isVideo = url.includes('.mp4') || url.includes('video'); return (
{isVideo ? (
Video Payload
) : ( {`Media )} #{i + 1}
); })}
)} {/* Raw Template Content */}
Raw Template Snippet
{template.content}
{/* Variable Pills Indicator */}
Variables: {"{{First Name}}"} {"{{Group Name}}"} {"{{Username}}"}
)} {/* Tab 2: Spintax Variations */} {activeTab === 'variations' && (
{/* Top Toolbar */}
Resolved Message Variations

{isSpintax ? "Paragraphs and line breaks are strictly obeyed across every resolved variation." : "Static message template. Use {Hello|Hi|Hey} spintax to generate dynamic variations."}

{isSpintax && ( )}
{/* List of Variations */}
{sampleVariations.map((sample, idx) => { const sampleWithVars = applySampleVariables(sample); return (
Variation {idx + 1} {sampleWithVars.split(/\s+/).filter(Boolean).length} words
{sampleWithVars}
); })}
)} {/* Tab 3: Telegram Simulator */} {activeTab === 'telegram' && (
Telegram Client Bubble Simulator (Recipient View)
{/* Chat Box Container with internal scroll so modal stays fixed in size and position */}
{/* Telegram Message Bubble */}
{/* Sender Title */}
ArkiTel Outreach Fleet
{/* Attached media preview if any */} {mediaUrls.length > 0 && (
{mediaUrls[0].includes('.mp4') ? (
Video Attached
) : ( Media )}
)} {/* Message Body with strictly preserved paragraphs */}
{applySampleVariables(currentSpun || spinText(template.content))}
{/* Time & Read Receipts */}
{new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} ✓✓
)}
); }