arkitel / frontend /src /components /TemplatePreviewModal.tsx
CBV01
feat: standardize modals to dark grey palette, add telegram simulator scroll, and add admin coupon editing
1d80adf
Raw History Blame Contribute Delete
17.8 kB
"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<string>('');
const [sampleVariations, setSampleVariations] = useState<string[]>([]);
const [copiedIndex, setCopiedIndex] = useState<number | null>(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<string>();
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 (
<div className="fixed inset-0 z-50 flex items-center justify-center p-3 sm:p-5">
{/* Backdrop */}
<div
className="fixed inset-0 bg-black/75 backdrop-blur-sm transition-opacity"
onClick={onClose}
/>
{/* Modal Dialog Card */}
<div className="relative my-auto w-full max-w-2xl bg-card border border-border/80 rounded-2xl shadow-2xl p-5 sm:p-6 text-foreground space-y-4 animate-in fade-in zoom-in-95 duration-150 z-50 overflow-hidden max-h-[85vh] flex flex-col">
<div className="absolute top-0 left-1/2 -translate-x-1/2 w-1/2 h-[2px] bg-gradient-to-r from-transparent via-indigo-500 to-transparent"></div>
{/* Header */}
<div className="flex items-start justify-between gap-3 pb-3 border-b border-border/60 shrink-0">
<div className="flex items-center gap-3">
<div className="w-9 h-9 rounded-xl bg-indigo-500/10 border border-indigo-500/20 flex items-center justify-center text-indigo-400 shrink-0">
<Eye size={18} />
</div>
<div>
<div className="flex items-center gap-2">
<h3 className="text-sm font-bold text-foreground truncate max-w-[280px] sm:max-w-md">
{template.name}
</h3>
<span className="text-[10px] font-mono text-indigo-400 bg-indigo-500/10 px-2 py-0.5 rounded-md border border-indigo-500/20 shrink-0">
#{template.id}
</span>
</div>
<p className="text-[11px] text-foreground/50 font-medium">
Template Preview & Spintax Variations Inspector
</p>
</div>
</div>
<button
type="button"
onClick={onClose}
className="p-1 rounded-lg text-foreground/40 hover:text-foreground hover:bg-foreground/5 transition-colors cursor-pointer"
>
<X size={16} />
</button>
</div>
{/* Tab Switcher */}
<div className="flex items-center gap-1.5 p-1 bg-input/50 border border-border/70 rounded-xl text-xs font-semibold shrink-0">
<button
type="button"
onClick={() => setActiveTab('preview')}
className={`flex-1 py-1.5 px-3 rounded-lg flex items-center justify-center gap-1.5 transition-all cursor-pointer ${
activeTab === 'preview'
? 'bg-indigo-500 text-white shadow-xs'
: 'text-foreground/60 hover:text-foreground'
}`}
>
<FileText size={13} />
<span>Content & Media</span>
</button>
<button
type="button"
onClick={() => setActiveTab('variations')}
className={`flex-1 py-1.5 px-3 rounded-lg flex items-center justify-center gap-1.5 transition-all cursor-pointer ${
activeTab === 'variations'
? 'bg-indigo-500 text-white shadow-xs'
: 'text-foreground/60 hover:text-foreground'
}`}
>
<Sparkles size={13} />
<span>Spintax Variations {isSpintax && `(${sampleVariations.length})`}</span>
</button>
<button
type="button"
onClick={() => setActiveTab('telegram')}
className={`flex-1 py-1.5 px-3 rounded-lg flex items-center justify-center gap-1.5 transition-all cursor-pointer ${
activeTab === 'telegram'
? 'bg-indigo-500 text-white shadow-xs'
: 'text-foreground/60 hover:text-foreground'
}`}
>
<MessageSquare size={13} />
<span>Telegram Simulator</span>
</button>
</div>
{/* Tab Bodies Container */}
<div className="overflow-y-auto custom-scrollbar flex-1 pr-1 space-y-4">
{/* Tab 1: Content & Media */}
{activeTab === 'preview' && (
<div className="space-y-4">
{/* Attached Media Gallery */}
{mediaUrls.length > 0 && (
<div className="space-y-2">
<div className="text-[11px] font-bold text-foreground/50 uppercase tracking-wider flex items-center gap-1.5">
<Paperclip size={13} className="text-indigo-400" />
<span>Attached Media Payloads ({mediaUrls.length})</span>
</div>
<div className="grid grid-cols-2 sm:grid-cols-3 gap-2.5">
{mediaUrls.map((url, i) => {
const isVideo = url.includes('.mp4') || url.includes('video');
return (
<div
key={i}
className="relative rounded-xl overflow-hidden border border-border/80 bg-black/40 group aspect-video flex items-center justify-center"
>
{isVideo ? (
<div className="flex flex-col items-center gap-1 text-indigo-400">
<Film size={24} />
<span className="text-[10px] font-mono text-foreground/60">Video Payload</span>
</div>
) : (
<img
src={url}
alt={`Media ${i + 1}`}
className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300"
/>
)}
<span className="absolute bottom-1 right-1 px-1.5 py-0.5 rounded bg-black/70 text-[9px] font-mono text-white">
#{i + 1}
</span>
</div>
);
})}
</div>
</div>
)}
{/* Raw Template Content */}
<div className="space-y-1.5">
<div className="flex items-center justify-between">
<span className="text-[11px] font-bold text-foreground/50 uppercase tracking-wider">
Raw Template Snippet
</span>
<button
type="button"
onClick={() => copyToClipboard(template.content)}
className="text-[10px] font-bold text-indigo-400 hover:text-indigo-300 flex items-center gap-1 transition-colors cursor-pointer"
>
{copiedRaw ? <Check size={12} className="text-emerald-400" /> : <Copy size={12} />}
<span>{copiedRaw ? 'Copied' : 'Copy Payload'}</span>
</button>
</div>
<div className="p-3.5 rounded-xl bg-input/50 border border-border/70 font-mono text-xs leading-relaxed text-foreground/90 max-h-56 overflow-y-auto custom-scrollbar select-all whitespace-pre-wrap">
{template.content}
</div>
</div>
{/* Variable Pills Indicator */}
<div className="p-2.5 rounded-xl bg-indigo-500/10 border border-indigo-500/20 text-xs flex flex-wrap items-center gap-2">
<span className="text-[10px] font-bold text-indigo-300 uppercase tracking-wider">Variables:</span>
<span className="px-2 py-0.5 rounded-md bg-indigo-500/20 text-indigo-300 text-[10px] font-mono font-bold">
{"{{First Name}}"}
</span>
<span className="px-2 py-0.5 rounded-md bg-foreground/10 text-foreground/80 text-[10px] font-mono font-bold">
{"{{Group Name}}"}
</span>
<span className="px-2 py-0.5 rounded-md bg-foreground/10 text-foreground/80 text-[10px] font-mono font-bold">
{"{{Username}}"}
</span>
</div>
</div>
)}
{/* Tab 2: Spintax Variations */}
{activeTab === 'variations' && (
<div className="space-y-3">
{/* Top Toolbar */}
<div className="flex items-center justify-between pb-1">
<div>
<div className="text-xs font-bold text-foreground">
Resolved Message Variations
</div>
<p className="text-[11px] text-foreground/50">
{isSpintax
? "Paragraphs and line breaks are strictly obeyed across every resolved variation."
: "Static message template. Use {Hello|Hi|Hey} spintax to generate dynamic variations."}
</p>
</div>
{isSpintax && (
<button
type="button"
onClick={regenerateVariations}
className="px-3 py-1.5 rounded-xl bg-indigo-500 hover:bg-indigo-600 text-white text-xs font-bold transition-all shadow-sm flex items-center gap-1.5 active:scale-95 cursor-pointer"
>
<Shuffle size={13} />
<span>Re-roll Variations</span>
</button>
)}
</div>
{/* List of Variations */}
<div className="space-y-2.5 max-h-72 overflow-y-auto custom-scrollbar pr-1">
{sampleVariations.map((sample, idx) => {
const sampleWithVars = applySampleVariables(sample);
return (
<div
key={idx}
className="p-3.5 rounded-xl bg-input/40 hover:bg-input/60 border border-border/70 hover:border-indigo-500/40 transition-all text-xs space-y-2 group"
>
<div className="flex items-center justify-between pb-1.5 border-b border-border/40">
<div className="flex items-center gap-2">
<span className="text-[9px] font-black uppercase tracking-wider text-indigo-400 bg-indigo-500/10 px-2 py-0.5 rounded-md border border-indigo-500/20">
Variation {idx + 1}
</span>
<span className="text-[10px] text-foreground/40 font-mono">
{sampleWithVars.split(/\s+/).filter(Boolean).length} words
</span>
</div>
<button
type="button"
onClick={() => copyToClipboard(sampleWithVars, idx)}
className="text-[10px] font-semibold text-foreground/60 hover:text-foreground flex items-center gap-1 transition-colors cursor-pointer bg-foreground/5 hover:bg-foreground/10 px-2 py-0.5 rounded-md"
>
{copiedIndex === idx ? (
<span className="text-emerald-400 flex items-center gap-1">
<Check size={11} /> Copied
</span>
) : (
<span className="flex items-center gap-1">
<Copy size={11} /> Copy
</span>
)}
</button>
</div>
<div className="text-foreground/85 leading-relaxed font-sans text-xs whitespace-pre-wrap select-all">
{sampleWithVars}
</div>
</div>
);
})}
</div>
</div>
)}
{/* Tab 3: Telegram Simulator */}
{activeTab === 'telegram' && (
<div className="space-y-3">
<div className="flex items-center justify-between">
<span className="text-[10px] font-bold text-foreground/40 uppercase tracking-wider">
Telegram Client Bubble Simulator (Recipient View)
</span>
<button
type="button"
onClick={() => setCurrentSpun(spinText(template.content))}
className="text-[10px] font-bold text-indigo-400 hover:text-indigo-300 flex items-center gap-1 cursor-pointer"
>
<Shuffle size={11} />
<span>Reroll Message</span>
</button>
</div>
{/* Chat Box Container with internal scroll so modal stays fixed in size and position */}
<div className="p-4 sm:p-5 rounded-2xl bg-[#0e1621] border border-[#2b3a4a] shadow-inner flex flex-col h-[340px] max-h-[340px] overflow-y-auto custom-scrollbar">
{/* Telegram Message Bubble */}
<div className="max-w-[88%] self-start rounded-2xl bg-[#182533] border border-[#232e3c] p-3 text-white shadow-md space-y-2 shrink-0">
{/* Sender Title */}
<div className="text-[11px] font-bold text-[#56b0f5]">
ArkiTel Outreach Fleet
</div>
{/* Attached media preview if any */}
{mediaUrls.length > 0 && (
<div className="rounded-xl overflow-hidden border border-white/10 max-h-44 aspect-video bg-black/60 shrink-0">
{mediaUrls[0].includes('.mp4') ? (
<div className="h-full flex items-center justify-center text-indigo-400 gap-2">
<Film size={28} />
<span className="text-xs font-mono">Video Attached</span>
</div>
) : (
<img
src={mediaUrls[0]}
alt="Media"
className="w-full h-full object-cover"
/>
)}
</div>
)}
{/* Message Body with strictly preserved paragraphs */}
<div className="text-xs leading-relaxed text-slate-100 whitespace-pre-wrap break-words">
{applySampleVariables(currentSpun || spinText(template.content))}
</div>
{/* Time & Read Receipts */}
<div className="flex items-center justify-end gap-1 text-[10px] text-slate-400 pt-1">
<span>{new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</span>
<span className="text-[#56b0f5]">✓✓</span>
</div>
</div>
</div>
</div>
)}
</div>
</div>
</div>
);
}