sitcod3 commited on
Commit
b677100
·
verified ·
1 Parent(s): f8dba25

Upload src\pages\CampaignDetail.tsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. src//pages//CampaignDetail.tsx +709 -0
src//pages//CampaignDetail.tsx ADDED
@@ -0,0 +1,709 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
2
+ import { apiClient } from "../lib/apiClient";
3
+ import { useParams, Link } from "react-router-dom";
4
+ import {
5
+ ArrowLeft,
6
+ Megaphone,
7
+ Layers,
8
+ Mail,
9
+ Users,
10
+ BarChart2,
11
+ Settings as SettingsIcon,
12
+ Plus,
13
+ Trash2,
14
+ Play,
15
+ Pause,
16
+ Clock,
17
+ CheckCircle2,
18
+ Sparkles,
19
+ Download,
20
+ Send,
21
+ Split
22
+ } from "lucide-react";
23
+ import { useState } from "react";
24
+ import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from "recharts";
25
+ import StatusBadge from "../components/StatusBadge";
26
+ import StatCard from "../components/StatCard";
27
+ import RichTextEditor from "../components/RichTextEditor";
28
+ import FormDrawer from "../components/FormDrawer";
29
+ import CsvExportButton from "../components/CsvExportButton";
30
+ import toast from "react-hot-toast";
31
+
32
+ export default function CampaignDetail() {
33
+ const { id } = useParams<{ id: string }>();
34
+ const queryClient = useQueryClient();
35
+ const [activeTab, setActiveTab] = useState<"overview" | "sequences" | "accounts" | "leads" | "analytics" | "settings">("overview");
36
+ const [isAddLeadOpen, setIsAddLeadOpen] = useState(false);
37
+ const [selectedVariantTab, setSelectedVariantTab] = useState<Record<string, number>>({});
38
+
39
+ const { data: campaign, isLoading } = useQuery({
40
+ queryKey: ["campaign", id],
41
+ queryFn: async () => {
42
+ const res = await apiClient.get(`/campaigns/${id}`);
43
+ return res.data;
44
+ },
45
+ enabled: Boolean(id),
46
+ });
47
+
48
+ const { data: sequences = [], refetch: refetchSequences } = useQuery({
49
+ queryKey: ["campaign-sequences", id],
50
+ queryFn: async () => {
51
+ const res = await apiClient.get(`/campaigns/${id}/sequences`);
52
+ return res.data;
53
+ },
54
+ enabled: Boolean(id),
55
+ });
56
+
57
+ const { data: campaignLeads = [], refetch: refetchLeads } = useQuery({
58
+ queryKey: ["campaign-leads", id],
59
+ queryFn: async () => {
60
+ const res = await apiClient.get(`/campaigns/${id}/leads`);
61
+ return res.data;
62
+ },
63
+ enabled: Boolean(id),
64
+ });
65
+
66
+ const { data: assignedAccounts = [], refetch: refetchAccounts } = useQuery({
67
+ queryKey: ["campaign-accounts", id],
68
+ queryFn: async () => {
69
+ const res = await apiClient.get(`/campaigns/${id}/email-accounts`);
70
+ return res.data;
71
+ },
72
+ enabled: Boolean(id),
73
+ });
74
+
75
+ const { data: allEmailAccounts = [] } = useQuery({
76
+ queryKey: ["email-accounts"],
77
+ queryFn: async () => {
78
+ const res = await apiClient.get("/email-accounts");
79
+ return res.data;
80
+ },
81
+ });
82
+
83
+ const { data: analyticsHistory = [] } = useQuery({
84
+ queryKey: ["campaign-analytics-date", id],
85
+ queryFn: async () => {
86
+ const res = await apiClient.get(`/campaigns/${id}/analytics-by-date`);
87
+ return res.data;
88
+ },
89
+ enabled: Boolean(id),
90
+ });
91
+
92
+ // Save sequences mutation
93
+ const saveSequencesMutation = useMutation({
94
+ mutationFn: async (updatedSequences: any[]) => {
95
+ const res = await apiClient.post(`/campaigns/${id}/sequences`, { sequences: updatedSequences });
96
+ return res.data;
97
+ },
98
+ onSuccess: () => {
99
+ toast.success("Sequence saved successfully");
100
+ refetchSequences();
101
+ queryClient.invalidateQueries({ queryKey: ["campaign", id] });
102
+ },
103
+ onError: (err: any) => {
104
+ toast.error(err.response?.data?.error || "Failed to save sequences");
105
+ },
106
+ });
107
+
108
+ const addSequenceStep = () => {
109
+ const nextSeqNum = (sequences.length || 0) + 1;
110
+ const newStep = {
111
+ id: "seq_" + Math.random().toString(36).substring(2, 7),
112
+ seq_number: nextSeqNum,
113
+ delay_days: nextSeqNum === 1 ? 0 : 3,
114
+ subject: `Follow-up #${nextSeqNum - 1}: Re: {{company_name}}`,
115
+ email_body: `<p>Hi {{first_name}},</p><p>Wanted to quickly circle back on my earlier note. Any thoughts?</p>`,
116
+ variant_distribution_percentage: 100,
117
+ variants: [],
118
+ };
119
+ saveSequencesMutation.mutate([...sequences, newStep]);
120
+ };
121
+
122
+ const removeSequenceStep = (stepIndex: number) => {
123
+ const filtered = sequences.filter((_: any, idx: number) => idx !== stepIndex);
124
+ saveSequencesMutation.mutate(filtered);
125
+ };
126
+
127
+ const addVariantToStep = (stepIndex: number) => {
128
+ const updated = [...sequences];
129
+ const target = updated[stepIndex];
130
+ if (!target.variants) target.variants = [];
131
+ target.variant_distribution_percentage = 50;
132
+ target.variants.push({
133
+ id: "var_" + Math.random().toString(36).substring(2, 7),
134
+ variant_label: `Variant ${String.fromCharCode(66 + target.variants.length)}`,
135
+ subject: `Alternative: ${target.subject}`,
136
+ email_body: `<p>Hey {{first_name}}, short follow up regarding {{company_name}}.</p>`,
137
+ });
138
+ saveSequencesMutation.mutate(updated);
139
+ };
140
+
141
+ const addAccountToCampaign = async (accountId: string) => {
142
+ try {
143
+ await apiClient.post(`/campaigns/${id}/email-accounts`, { email_account_id: accountId });
144
+ toast.success("Mailbox assigned to campaign");
145
+ refetchAccounts();
146
+ } catch (err: any) {
147
+ toast.error("Failed to assign account");
148
+ }
149
+ };
150
+
151
+ const removeAccountFromCampaign = async (accountId: string) => {
152
+ try {
153
+ await apiClient.delete(`/campaigns/${id}/email-accounts/${accountId}`);
154
+ toast.success("Mailbox removed from campaign");
155
+ refetchAccounts();
156
+ } catch (err: any) {
157
+ toast.error("Failed to remove account");
158
+ }
159
+ };
160
+
161
+ if (isLoading || !campaign) {
162
+ return (
163
+ <div className="p-12 flex justify-center items-center">
164
+ <div className="w-8 h-8 rounded-full border-2 border-indigo-500 border-t-transparent animate-spin" />
165
+ </div>
166
+ );
167
+ }
168
+
169
+ return (
170
+ <div className="p-6 max-w-7xl mx-auto space-y-6">
171
+ {/* Back Navigation and Header */}
172
+ <div className="space-y-3">
173
+ <Link
174
+ to="/campaigns"
175
+ className="inline-flex items-center gap-1.5 text-xs text-zinc-400 hover:text-zinc-200 transition-colors"
176
+ >
177
+ <ArrowLeft className="w-3.5 h-3.5" /> Back to all campaigns
178
+ </Link>
179
+
180
+ <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
181
+ <div>
182
+ <div className="flex items-center gap-3">
183
+ <h1 className="text-2xl font-bold text-zinc-100 tracking-tight">{campaign.name}</h1>
184
+ <StatusBadge status={campaign.status} />
185
+ </div>
186
+ <p className="text-xs text-zinc-400 mt-1">
187
+ Timezone: {campaign.timezone} • Schedule: {campaign.start_schedule} - {campaign.end_schedule}
188
+ </p>
189
+ </div>
190
+
191
+ <div className="flex items-center gap-2">
192
+ <button
193
+ onClick={() => setIsAddLeadOpen(true)}
194
+ className="px-3.5 py-1.5 bg-indigo-600 hover:bg-indigo-500 text-white text-xs font-medium rounded-lg transition-colors flex items-center gap-1.5"
195
+ >
196
+ <Plus className="w-3.5 h-3.5" /> Add Leads
197
+ </button>
198
+ </div>
199
+ </div>
200
+ </div>
201
+
202
+ {/* Tabs Bar */}
203
+ <div className="border-b border-zinc-800 flex items-center gap-1 text-sm font-medium overflow-x-auto">
204
+ {[
205
+ { key: "overview", label: "Overview", icon: BarChart2 },
206
+ { key: "sequences", label: `Sequences (${sequences.length})`, icon: Layers },
207
+ { key: "accounts", label: `Email Accounts (${assignedAccounts.length})`, icon: Mail },
208
+ { key: "leads", label: `Leads (${campaignLeads.length})`, icon: Users },
209
+ { key: "analytics", label: "Analytics", icon: BarChart2 },
210
+ { key: "settings", label: "Settings", icon: SettingsIcon },
211
+ ].map((tab) => (
212
+ <button
213
+ key={tab.key}
214
+ onClick={() => setActiveTab(tab.key as any)}
215
+ className={`px-4 py-2.5 border-b-2 font-medium text-xs flex items-center gap-2 transition-colors whitespace-nowrap ${
216
+ activeTab === tab.key
217
+ ? "border-indigo-500 text-indigo-400 bg-indigo-500/5"
218
+ : "border-transparent text-zinc-400 hover:text-zinc-200 hover:border-zinc-700"
219
+ }`}
220
+ >
221
+ <tab.icon className="w-4 h-4" />
222
+ {tab.label}
223
+ </button>
224
+ ))}
225
+ </div>
226
+
227
+ {/* TAB 1: OVERVIEW */}
228
+ {activeTab === "overview" && (
229
+ <div className="space-y-6">
230
+ <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
231
+ <StatCard
232
+ title="Leads in Sequence"
233
+ value={campaign.lead_count || 0}
234
+ icon={Users}
235
+ iconColor="teal"
236
+ />
237
+ <StatCard
238
+ title="Emails Sent"
239
+ value={campaign.sent_count || 0}
240
+ icon={Send}
241
+ iconColor="blue"
242
+ />
243
+ <StatCard
244
+ title="Unique Opens"
245
+ value={campaign.open_count || 0}
246
+ subtitle={`${((campaign.open_count / (campaign.sent_count || 1)) * 100).toFixed(1)}% Open Rate`}
247
+ icon={Mail}
248
+ iconColor="emerald"
249
+ />
250
+ <StatCard
251
+ title="Replies Received"
252
+ value={campaign.reply_count || 0}
253
+ subtitle={`${((campaign.reply_count / (campaign.sent_count || 1)) * 100).toFixed(1)}% Reply Rate`}
254
+ icon={CheckCircle2}
255
+ iconColor="purple"
256
+ />
257
+ </div>
258
+
259
+ {/* 14-Day Trajectory */}
260
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-5 shadow-sm">
261
+ <h3 className="text-sm font-semibold text-zinc-100 mb-4">
262
+ Engagement Trajectory (Last 14 Days)
263
+ </h3>
264
+ <div className="h-[260px] w-full">
265
+ <ResponsiveContainer width="100%" height="100%">
266
+ <LineChart data={analyticsHistory} margin={{ top: 5, right: 10, bottom: 5, left: -15 }}>
267
+ <CartesianGrid strokeDasharray="3 3" stroke="#27272a" vertical={false} />
268
+ <XAxis dataKey="date" stroke="#71717a" fontSize={11} tickLine={false} axisLine={false} />
269
+ <YAxis stroke="#71717a" fontSize={11} tickLine={false} axisLine={false} />
270
+ <Tooltip
271
+ contentStyle={{ backgroundColor: "#09090b", borderColor: "#27272a", borderRadius: "8px", fontSize: "12px" }}
272
+ />
273
+ <Line type="monotone" dataKey="sent" stroke="#3b82f6" strokeWidth={2} name="Sent" dot={false} />
274
+ <Line type="monotone" dataKey="opens" stroke="#10b981" strokeWidth={2} name="Opens" dot={false} />
275
+ <Line type="monotone" dataKey="replies" stroke="#a855f7" strokeWidth={2} name="Replies" dot={false} />
276
+ </LineChart>
277
+ </ResponsiveContainer>
278
+ </div>
279
+ </div>
280
+ </div>
281
+ )}
282
+
283
+ {/* TAB 2: SEQUENCES WITH A/B VARIANTS */}
284
+ {activeTab === "sequences" && (
285
+ <div className="space-y-6">
286
+ <div className="flex items-center justify-between">
287
+ <div>
288
+ <h3 className="text-base font-semibold text-zinc-100">Multi-Step Sequence Flow</h3>
289
+ <p className="text-xs text-zinc-400 mt-0.5">
290
+ Set delay days, customize subject lines, and configure A/B variants per email step.
291
+ </p>
292
+ </div>
293
+ <button
294
+ onClick={addSequenceStep}
295
+ className="px-3.5 py-1.5 bg-indigo-600 hover:bg-indigo-500 text-white text-xs font-medium rounded-lg transition-colors flex items-center gap-1.5"
296
+ >
297
+ <Plus className="w-3.5 h-3.5" /> Add Email Step
298
+ </button>
299
+ </div>
300
+
301
+ <div className="space-y-4">
302
+ {sequences.map((step: any, stepIndex: number) => {
303
+ const currentVariantTab = selectedVariantTab[step.id] || 0;
304
+ const hasVariants = step.variants && step.variants.length > 0;
305
+
306
+ return (
307
+ <div key={step.id || stepIndex} className="bg-zinc-900 border border-zinc-800 rounded-xl overflow-hidden shadow-sm">
308
+ {/* Step Header */}
309
+ <div className="px-5 py-3.5 bg-zinc-950 border-b border-zinc-800 flex items-center justify-between">
310
+ <div className="flex items-center gap-3">
311
+ <span className="w-6 h-6 rounded-full bg-indigo-500/20 text-indigo-400 font-bold text-xs flex items-center justify-center border border-indigo-500/30">
312
+ {step.seq_number}
313
+ </span>
314
+ <div>
315
+ <span className="text-xs font-semibold text-zinc-200">
316
+ {step.seq_number === 1 ? "Initial Outreach (Step 1)" : `Follow-up #${step.seq_number - 1}`}
317
+ </span>
318
+ <div className="flex items-center gap-2 text-[11px] text-zinc-400 mt-0.5">
319
+ <Clock className="w-3 h-3 text-zinc-500" />
320
+ <span>Delay:</span>
321
+ <input
322
+ type="number"
323
+ min="0"
324
+ value={step.delay_days}
325
+ onChange={(e) => {
326
+ const updated = [...sequences];
327
+ updated[stepIndex].delay_days = Number(e.target.value);
328
+ saveSequencesMutation.mutate(updated);
329
+ }}
330
+ className="w-12 bg-zinc-900 border border-zinc-700 rounded px-1.5 py-0.5 text-xs text-zinc-100"
331
+ />
332
+ <span>days after previous email</span>
333
+ </div>
334
+ </div>
335
+ </div>
336
+
337
+ <div className="flex items-center gap-2">
338
+ <button
339
+ type="button"
340
+ onClick={() => addVariantToStep(stepIndex)}
341
+ className="px-2.5 py-1 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 text-xs font-medium rounded-md transition-colors flex items-center gap-1"
342
+ >
343
+ <Split className="w-3.5 h-3.5 text-indigo-400" /> + A/B Variant
344
+ </button>
345
+ <button
346
+ type="button"
347
+ onClick={() => removeSequenceStep(stepIndex)}
348
+ className="p-1.5 text-zinc-500 hover:text-rose-400 hover:bg-rose-500/10 rounded-md transition-colors"
349
+ >
350
+ <Trash2 className="w-4 h-4" />
351
+ </button>
352
+ </div>
353
+ </div>
354
+
355
+ {/* Step Body & Variant Tabs */}
356
+ <div className="p-5 space-y-4">
357
+ {hasVariants && (
358
+ <div className="flex items-center justify-between border-b border-zinc-800 pb-3">
359
+ <div className="flex items-center gap-2">
360
+ <button
361
+ type="button"
362
+ onClick={() => setSelectedVariantTab((prev) => ({ ...prev, [step.id]: 0 }))}
363
+ className={`px-3 py-1 text-xs font-medium rounded-md transition-colors ${
364
+ currentVariantTab === 0 ? "bg-indigo-600 text-white" : "bg-zinc-800 text-zinc-400"
365
+ }`}
366
+ >
367
+ Variant A (Default)
368
+ </button>
369
+ {step.variants.map((v: any, vIdx: number) => (
370
+ <button
371
+ key={v.id || vIdx}
372
+ type="button"
373
+ onClick={() => setSelectedVariantTab((prev) => ({ ...prev, [step.id]: vIdx + 1 }))}
374
+ className={`px-3 py-1 text-xs font-medium rounded-md transition-colors ${
375
+ currentVariantTab === vIdx + 1 ? "bg-indigo-600 text-white" : "bg-zinc-800 text-zinc-400"
376
+ }`}
377
+ >
378
+ {v.variant_label || `Variant ${String.fromCharCode(66 + vIdx)}`}
379
+ </button>
380
+ ))}
381
+ </div>
382
+
383
+ <div className="text-xs text-zinc-400 font-mono">
384
+ Split Traffic: 50% / 50%
385
+ </div>
386
+ </div>
387
+ )}
388
+
389
+ {/* Active Variant Editor */}
390
+ {currentVariantTab === 0 ? (
391
+ <div className="space-y-3">
392
+ <div>
393
+ <label className="block text-xs font-medium text-zinc-400 mb-1">
394
+ Subject Line (Variant A)
395
+ </label>
396
+ <input
397
+ type="text"
398
+ value={step.subject}
399
+ onChange={(e) => {
400
+ const updated = [...sequences];
401
+ updated[stepIndex].subject = e.target.value;
402
+ saveSequencesMutation.mutate(updated);
403
+ }}
404
+ className="w-full bg-zinc-950 border border-zinc-800 rounded-lg p-2.5 text-xs text-zinc-100 focus:outline-none focus:border-indigo-500"
405
+ />
406
+ </div>
407
+
408
+ <div>
409
+ <label className="block text-xs font-medium text-zinc-400 mb-1">
410
+ Email Body (Variant A)
411
+ </label>
412
+ <RichTextEditor
413
+ content={step.email_body}
414
+ onChange={(html) => {
415
+ const updated = [...sequences];
416
+ updated[stepIndex].email_body = html;
417
+ saveSequencesMutation.mutate(updated);
418
+ }}
419
+ />
420
+ </div>
421
+ </div>
422
+ ) : (
423
+ <div className="space-y-3">
424
+ <div>
425
+ <label className="block text-xs font-medium text-zinc-400 mb-1">
426
+ Subject Line ({step.variants[currentVariantTab - 1]?.variant_label})
427
+ </label>
428
+ <input
429
+ type="text"
430
+ value={step.variants[currentVariantTab - 1]?.subject || ""}
431
+ onChange={(e) => {
432
+ const updated = [...sequences];
433
+ updated[stepIndex].variants[currentVariantTab - 1].subject = e.target.value;
434
+ saveSequencesMutation.mutate(updated);
435
+ }}
436
+ className="w-full bg-zinc-950 border border-zinc-800 rounded-lg p-2.5 text-xs text-zinc-100 focus:outline-none focus:border-indigo-500"
437
+ />
438
+ </div>
439
+
440
+ <div>
441
+ <label className="block text-xs font-medium text-zinc-400 mb-1">
442
+ Email Body ({step.variants[currentVariantTab - 1]?.variant_label})
443
+ </label>
444
+ <RichTextEditor
445
+ content={step.variants[currentVariantTab - 1]?.email_body || ""}
446
+ onChange={(html) => {
447
+ const updated = [...sequences];
448
+ updated[stepIndex].variants[currentVariantTab - 1].email_body = html;
449
+ saveSequencesMutation.mutate(updated);
450
+ }}
451
+ />
452
+ </div>
453
+ </div>
454
+ )}
455
+ </div>
456
+ </div>
457
+ );
458
+ })}
459
+ </div>
460
+ </div>
461
+ )}
462
+
463
+ {/* TAB 3: EMAIL ACCOUNTS ASSIGNMENT */}
464
+ {activeTab === "accounts" && (
465
+ <div className="space-y-6">
466
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-5">
467
+ <h3 className="text-base font-semibold text-zinc-100 mb-1">Assigned Sending Inboxes</h3>
468
+ <p className="text-xs text-zinc-400 mb-4">
469
+ Emails will be rotated across these connected mailboxes to preserve deliverability.
470
+ </p>
471
+
472
+ <div className="space-y-3">
473
+ {assignedAccounts.map((acc: any) => (
474
+ <div
475
+ key={acc.id}
476
+ className="flex items-center justify-between p-3 bg-zinc-950 border border-zinc-800 rounded-lg"
477
+ >
478
+ <div className="flex items-center gap-3">
479
+ <div className="p-2 bg-indigo-500/10 text-indigo-400 rounded-md">
480
+ <Mail className="w-4 h-4" />
481
+ </div>
482
+ <div>
483
+ <span className="text-xs font-semibold text-zinc-200">{acc.from_name}</span>
484
+ <div className="text-[11px] text-zinc-400">{acc.from_email}</div>
485
+ </div>
486
+ </div>
487
+
488
+ <div className="flex items-center gap-3">
489
+ <StatusBadge status={acc.status} size="sm" />
490
+ <button
491
+ onClick={() => removeAccountFromCampaign(acc.id)}
492
+ className="text-xs text-rose-400 hover:text-rose-300 font-medium"
493
+ >
494
+ Remove
495
+ </button>
496
+ </div>
497
+ </div>
498
+ ))}
499
+
500
+ {assignedAccounts.length === 0 && (
501
+ <div className="p-6 text-center text-xs text-zinc-500 bg-zinc-950/60 rounded-lg border border-dashed border-zinc-800">
502
+ No email accounts assigned yet. Select from available mailboxes below.
503
+ </div>
504
+ )}
505
+ </div>
506
+ </div>
507
+
508
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-5">
509
+ <h4 className="text-xs font-semibold uppercase tracking-wider text-zinc-400 mb-3">
510
+ Available Mailboxes
511
+ </h4>
512
+ <div className="space-y-2">
513
+ {allEmailAccounts
514
+ .filter((a: any) => !assignedAccounts.some((aa: any) => aa.id === a.id))
515
+ .map((acc: any) => (
516
+ <div
517
+ key={acc.id}
518
+ className="flex items-center justify-between p-3 bg-zinc-950/40 border border-zinc-800/80 rounded-lg"
519
+ >
520
+ <div>
521
+ <span className="text-xs font-medium text-zinc-200">{acc.from_email}</span>
522
+ <div className="text-[10px] text-zinc-500">
523
+ Provider: {acc.provider} • Daily limit: {acc.daily_limit} emails
524
+ </div>
525
+ </div>
526
+ <button
527
+ onClick={() => addAccountToCampaign(acc.id)}
528
+ className="px-3 py-1 bg-indigo-600/20 hover:bg-indigo-600 text-indigo-300 hover:text-white rounded text-xs font-medium transition-colors"
529
+ >
530
+ + Assign to Campaign
531
+ </button>
532
+ </div>
533
+ ))}
534
+ </div>
535
+ </div>
536
+ </div>
537
+ )}
538
+
539
+ {/* TAB 4: LEADS */}
540
+ {activeTab === "leads" && (
541
+ <div className="space-y-4">
542
+ <div className="flex items-center justify-between">
543
+ <div className="text-xs text-zinc-400">
544
+ Showing {campaignLeads.length} leads in this campaign.
545
+ </div>
546
+ <div className="flex items-center gap-2">
547
+ <CsvExportButton data={campaignLeads} filename={`campaign-${id}-leads.csv`} />
548
+ <button
549
+ onClick={() => setIsAddLeadOpen(true)}
550
+ className="px-3 py-1.5 bg-indigo-600 hover:bg-indigo-500 text-white text-xs font-medium rounded-lg transition-colors flex items-center gap-1.5"
551
+ >
552
+ <Plus className="w-3.5 h-3.5" /> Add Lead
553
+ </button>
554
+ </div>
555
+ </div>
556
+
557
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl overflow-hidden">
558
+ <table className="w-full text-sm text-left">
559
+ <thead className="bg-zinc-950 border-b border-zinc-800 text-zinc-400 text-xs uppercase">
560
+ <tr>
561
+ <th className="px-5 py-3 font-semibold">Lead</th>
562
+ <th className="px-5 py-3 font-semibold">Company</th>
563
+ <th className="px-5 py-3 font-semibold">Status</th>
564
+ <th className="px-5 py-3 font-semibold">Opens</th>
565
+ <th className="px-5 py-3 font-semibold">Clicks</th>
566
+ </tr>
567
+ </thead>
568
+ <tbody className="divide-y divide-zinc-800">
569
+ {campaignLeads.map((lead: any) => (
570
+ <tr key={lead.id} className="hover:bg-zinc-800/30">
571
+ <td className="px-5 py-3.5">
572
+ <div className="font-medium text-zinc-200">
573
+ {lead.first_name} {lead.last_name}
574
+ </div>
575
+ <div className="text-xs text-zinc-400 font-mono">{lead.email}</div>
576
+ </td>
577
+ <td className="px-5 py-3.5 text-zinc-300">{lead.company_name}</td>
578
+ <td className="px-5 py-3.5">
579
+ <StatusBadge status={lead.status} size="sm" />
580
+ </td>
581
+ <td className="px-5 py-3.5 text-zinc-300 font-mono">{lead.open_count || 0}</td>
582
+ <td className="px-5 py-3.5 text-zinc-300 font-mono">{lead.click_count || 0}</td>
583
+ </tr>
584
+ ))}
585
+ </tbody>
586
+ </table>
587
+ </div>
588
+ </div>
589
+ )}
590
+
591
+ {/* TAB 5: ANALYTICS */}
592
+ {activeTab === "analytics" && (
593
+ <div className="space-y-6">
594
+ <div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
595
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-4">
596
+ <span className="text-xs text-zinc-400">Open Rate</span>
597
+ <div className="text-2xl font-bold text-emerald-400 mt-1">56.2%</div>
598
+ <span className="text-[11px] text-zinc-500">Above industry avg (28%)</span>
599
+ </div>
600
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-4">
601
+ <span className="text-xs text-zinc-400">Reply Rate</span>
602
+ <div className="text-2xl font-bold text-purple-400 mt-1">6.4%</div>
603
+ <span className="text-[11px] text-zinc-500">Strong positive intent</span>
604
+ </div>
605
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-4">
606
+ <span className="text-xs text-zinc-400">Bounce Rate</span>
607
+ <div className="text-2xl font-bold text-rose-400 mt-1">0.6%</div>
608
+ <span className="text-[11px] text-zinc-500">Safe zone (&lt; 2%)</span>
609
+ </div>
610
+ </div>
611
+ </div>
612
+ )}
613
+
614
+ {/* TAB 6: SETTINGS */}
615
+ {activeTab === "settings" && (
616
+ <div className="bg-zinc-900 border border-zinc-800 rounded-xl p-6 max-w-2xl">
617
+ <h3 className="text-base font-semibold text-zinc-100 mb-4">Campaign Parameters</h3>
618
+ <div className="space-y-4 text-xs">
619
+ <div>
620
+ <label className="block text-zinc-400 font-medium mb-1">Timezone</label>
621
+ <input
622
+ disabled
623
+ value={campaign.timezone}
624
+ className="w-full bg-zinc-950 border border-zinc-800 rounded-lg p-2.5 text-zinc-300"
625
+ />
626
+ </div>
627
+ <div>
628
+ <label className="block text-zinc-400 font-medium mb-1">Unsubscribe Footer</label>
629
+ <input
630
+ disabled
631
+ value={campaign.unsubscribe_text}
632
+ className="w-full bg-zinc-950 border border-zinc-800 rounded-lg p-2.5 text-zinc-300"
633
+ />
634
+ </div>
635
+ </div>
636
+ </div>
637
+ )}
638
+
639
+ {/* Add Lead Slide-Over */}
640
+ <FormDrawer
641
+ isOpen={isAddLeadOpen}
642
+ onClose={() => setIsAddLeadOpen(false)}
643
+ title="Add Lead to Campaign"
644
+ subtitle="Upload prospect details to be enrolled into this sequence."
645
+ >
646
+ <form
647
+ onSubmit={async (e) => {
648
+ e.preventDefault();
649
+ const form = e.target as HTMLFormElement;
650
+ const newLead = {
651
+ first_name: (form.elements.namedItem("first_name") as HTMLInputElement).value,
652
+ last_name: (form.elements.namedItem("last_name") as HTMLInputElement).value,
653
+ email: (form.elements.namedItem("email") as HTMLInputElement).value,
654
+ company_name: (form.elements.namedItem("company_name") as HTMLInputElement).value,
655
+ website: (form.elements.namedItem("website") as HTMLInputElement).value,
656
+ status: "INTERESTED",
657
+ };
658
+
659
+ try {
660
+ await apiClient.post(`/campaigns/${id}/leads`, newLead);
661
+ toast.success("Lead enrolled in campaign!");
662
+ setIsAddLeadOpen(false);
663
+ refetchLeads();
664
+ } catch (err: any) {
665
+ toast.error("Failed to add lead");
666
+ }
667
+ }}
668
+ className="space-y-4 text-xs"
669
+ >
670
+ <div>
671
+ <label className="block font-medium text-zinc-300 mb-1">First Name *</label>
672
+ <input name="first_name" required className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100" />
673
+ </div>
674
+ <div>
675
+ <label className="block font-medium text-zinc-300 mb-1">Last Name</label>
676
+ <input name="last_name" className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100" />
677
+ </div>
678
+ <div>
679
+ <label className="block font-medium text-zinc-300 mb-1">Email *</label>
680
+ <input type="email" name="email" required className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100" />
681
+ </div>
682
+ <div>
683
+ <label className="block font-medium text-zinc-300 mb-1">Company Name</label>
684
+ <input name="company_name" className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100" />
685
+ </div>
686
+ <div>
687
+ <label className="block font-medium text-zinc-300 mb-1">Website</label>
688
+ <input name="website" placeholder="https://" className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100" />
689
+ </div>
690
+ <div className="pt-4 flex justify-end gap-2">
691
+ <button
692
+ type="button"
693
+ onClick={() => setIsAddLeadOpen(false)}
694
+ className="px-3 py-2 bg-zinc-800 text-zinc-300 rounded-lg"
695
+ >
696
+ Cancel
697
+ </button>
698
+ <button
699
+ type="submit"
700
+ className="px-4 py-2 bg-indigo-600 text-white rounded-lg font-medium"
701
+ >
702
+ Enroll Lead
703
+ </button>
704
+ </div>
705
+ </form>
706
+ </FormDrawer>
707
+ </div>
708
+ );
709
+ }