File size: 17,820 Bytes
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ab7243
b31cb3a
3ab7243
 
b31cb3a
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
3ab7243
 
 
 
 
 
 
 
 
 
 
b31cb3a
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d80adf
b31cb3a
 
1d80adf
b31cb3a
 
 
 
1d80adf
 
b31cb3a
 
1d80adf
b31cb3a
3ab7243
 
b31cb3a
 
 
3ab7243
b31cb3a
 
3ab7243
b31cb3a
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
3ab7243
b31cb3a
3ab7243
b31cb3a
 
 
 
1d80adf
b31cb3a
 
 
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
1d80adf
 
 
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d80adf
b31cb3a
 
 
 
 
3ab7243
 
b31cb3a
 
 
3ab7243
b31cb3a
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
3ab7243
 
 
b31cb3a
3ab7243
 
b31cb3a
3ab7243
 
 
 
b31cb3a
 
3ab7243
 
 
 
 
 
 
 
 
 
b31cb3a
 
3ab7243
 
b31cb3a
 
 
 
 
1d80adf
b31cb3a
3ab7243
 
 
 
 
 
 
 
 
b31cb3a
 
 
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
3ab7243
b31cb3a
3ab7243
b31cb3a
 
 
 
 
 
 
 
 
 
3ab7243
 
 
 
 
 
 
 
 
 
 
 
b31cb3a
 
1d80adf
 
b31cb3a
 
1d80adf
b31cb3a
 
 
 
 
 
 
 
1d80adf
b31cb3a
 
 
 
 
 
 
 
3ab7243
b31cb3a
 
 
 
 
 
3ab7243
1d80adf
3ab7243
 
b31cb3a
3ab7243
 
 
 
b31cb3a
 
 
 
 
 
 
1d80adf
b31cb3a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"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>
  );
}