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

Upload src\pages\Campaigns.tsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. src//pages//Campaigns.tsx +440 -0
src//pages//Campaigns.tsx ADDED
@@ -0,0 +1,440 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
2
+ import { apiClient } from "../lib/apiClient";
3
+ import {
4
+ Plus,
5
+ Search,
6
+ Megaphone,
7
+ Play,
8
+ Pause,
9
+ Square,
10
+ Trash2,
11
+ ArrowUpRight,
12
+ Sliders,
13
+ Clock,
14
+ Globe,
15
+ CheckCircle2,
16
+ MailCheck
17
+ } from "lucide-react";
18
+ import { useState } from "react";
19
+ import { Link } from "react-router-dom";
20
+ import StatusBadge from "../components/StatusBadge";
21
+ import ConfirmModal from "../components/ConfirmModal";
22
+ import FormDrawer from "../components/FormDrawer";
23
+ import toast from "react-hot-toast";
24
+
25
+ export default function Campaigns() {
26
+ const queryClient = useQueryClient();
27
+ const [searchTerm, setSearchTerm] = useState("");
28
+ const [statusFilter, setStatusFilter] = useState("ALL");
29
+ const [isCreateOpen, setIsCreateOpen] = useState(false);
30
+ const [selectedForDelete, setSelectedForDelete] = useState<string | null>(null);
31
+
32
+ const { data: campaigns, isLoading } = useQuery({
33
+ queryKey: ["campaigns"],
34
+ queryFn: async () => {
35
+ const res = await apiClient.get("/campaigns");
36
+ return res.data;
37
+ },
38
+ });
39
+
40
+ const updateStatusMutation = useMutation({
41
+ mutationFn: async ({ id, status }: { id: string; status: "START" | "PAUSE" | "STOP" }) => {
42
+ const res = await apiClient.post(`/campaigns/${id}/status`, { status });
43
+ return res.data;
44
+ },
45
+ onSuccess: (_, variables) => {
46
+ toast.success(`Campaign ${variables.status.toLowerCase()}ed`);
47
+ queryClient.invalidateQueries({ queryKey: ["campaigns"] });
48
+ },
49
+ onError: (err: any) => {
50
+ toast.error(err.response?.data?.error || "Failed to update campaign status");
51
+ }
52
+ });
53
+
54
+ const deleteMutation = useMutation({
55
+ mutationFn: async (id: string) => {
56
+ const res = await apiClient.delete(`/campaigns/${id}`);
57
+ return res.data;
58
+ },
59
+ onSuccess: () => {
60
+ toast.success("Campaign removed");
61
+ setSelectedForDelete(null);
62
+ queryClient.invalidateQueries({ queryKey: ["campaigns"] });
63
+ },
64
+ onError: (err: any) => {
65
+ toast.error(err.response?.data?.error || "Failed to delete campaign");
66
+ }
67
+ });
68
+
69
+ const filteredCampaigns = (campaigns || []).filter((c: any) => {
70
+ const matchesSearch = c.name?.toLowerCase().includes(searchTerm.toLowerCase());
71
+ const matchesStatus = statusFilter === "ALL" || c.status === statusFilter;
72
+ return matchesSearch && matchesStatus;
73
+ });
74
+
75
+ return (
76
+ <div className="p-6 max-w-7xl mx-auto space-y-6">
77
+ {/* Header */}
78
+ <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
79
+ <div>
80
+ <h1 className="text-2xl font-bold tracking-tight text-zinc-100 flex items-center gap-2.5">
81
+ <Megaphone className="w-6 h-6 text-indigo-400" />
82
+ Outreach Campaigns
83
+ </h1>
84
+ <p className="text-zinc-400 text-sm mt-1">
85
+ Automated multi-step cold email sequences with A/B variant testing.
86
+ </p>
87
+ </div>
88
+
89
+ <button
90
+ onClick={() => setIsCreateOpen(true)}
91
+ className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white text-sm font-medium rounded-lg transition-all shadow-md shadow-indigo-600/20 flex items-center gap-2 self-start sm:self-auto"
92
+ >
93
+ <Plus className="w-4 h-4" />
94
+ Create Campaign
95
+ </button>
96
+ </div>
97
+
98
+ {/* Filter and Search Bar */}
99
+ <div className="bg-white/5 border border-white/10 backdrop-blur-md rounded-xl p-4 flex flex-col sm:flex-row items-stretch sm:items-center justify-between gap-3 shadow-[0_8px_32px_-8px_rgba(0,0,0,0.5)]">
100
+ <div className="relative flex-1 max-w-md">
101
+ <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-zinc-400" />
102
+ <input
103
+ type="text"
104
+ placeholder="Search campaigns by name..."
105
+ value={searchTerm}
106
+ onChange={(e) => setSearchTerm(e.target.value)}
107
+ className="w-full bg-black/40 border border-white/5 rounded-lg py-2 pl-9 pr-4 text-sm text-zinc-100 placeholder:text-zinc-500 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500 shadow-[inset_0_1px_1px_rgba(255,255,255,0.02)] transition-all"
108
+ />
109
+ </div>
110
+
111
+ <div className="flex items-center gap-2">
112
+ {["ALL", "ACTIVE", "PAUSED", "STOPPED"].map((status) => (
113
+ <button
114
+ key={status}
115
+ onClick={() => setStatusFilter(status)}
116
+ className={`px-3 py-1.5 rounded-lg text-xs font-medium transition-all duration-300 shadow-[inset_0_1px_1px_rgba(255,255,255,0.02)] ${
117
+ statusFilter === status
118
+ ? "bg-indigo-600 text-white shadow-[0_0_12px_rgba(79,70,229,0.5)] border border-indigo-500/50"
119
+ : "bg-black/40 text-zinc-400 hover:text-zinc-200 border border-white/5"
120
+ }`}
121
+ >
122
+ {status}
123
+ </button>
124
+ ))}
125
+ </div>
126
+ </div>
127
+
128
+ {/* Campaigns Table */}
129
+ <div className="bg-white/5 border border-white/10 backdrop-blur-md rounded-xl overflow-hidden shadow-[0_8px_32px_-8px_rgba(0,0,0,0.5)]">
130
+ {isLoading ? (
131
+ <div className="py-20 flex flex-col items-center justify-center gap-3">
132
+ <div className="w-8 h-8 rounded-full border-2 border-indigo-500 border-t-transparent animate-spin drop-shadow-[0_0_8px_rgba(99,102,241,0.5)]" />
133
+ <p className="text-sm text-zinc-400">Loading campaign records...</p>
134
+ </div>
135
+ ) : filteredCampaigns.length > 0 ? (
136
+ <div className="overflow-x-auto">
137
+ <table className="w-full text-sm text-left">
138
+ <thead className="bg-black/20 border-b border-white/5 text-zinc-400 text-xs uppercase font-sans tracking-wider">
139
+ <tr>
140
+ <th className="px-6 py-3.5 font-semibold">Campaign Name</th>
141
+ <th className="px-6 py-3.5 font-semibold">Status</th>
142
+ <th className="px-6 py-3.5 font-semibold">Leads</th>
143
+ <th className="px-6 py-3.5 font-semibold">Sent</th>
144
+ <th className="px-6 py-3.5 font-semibold">Opens</th>
145
+ <th className="px-6 py-3.5 font-semibold">Replies</th>
146
+ <th className="px-6 py-3.5 font-semibold">Bounces</th>
147
+ <th className="px-6 py-3.5 font-semibold text-right">Actions</th>
148
+ </tr>
149
+ </thead>
150
+ <tbody className="divide-y divide-white/5">
151
+ {filteredCampaigns.map((camp: any) => (
152
+ <tr key={camp.id} className="hover:bg-white/5 transition-colors">
153
+ <td className="px-6 py-4">
154
+ <Link
155
+ to={`/campaigns/${camp.id}`}
156
+ className="font-semibold text-zinc-100 hover:text-indigo-400 transition-colors flex items-center gap-1.5"
157
+ >
158
+ {camp.name}
159
+ <ArrowUpRight className="w-3.5 h-3.5 text-zinc-500" />
160
+ </Link>
161
+ <div className="text-[11px] text-zinc-400 mt-0.5 font-sans">
162
+ Schedule: <span className="font-mono">{camp.start_schedule || "09:00"} - {camp.end_schedule || "17:00"}</span> ({camp.timezone || "America/New_York"})
163
+ </div>
164
+ </td>
165
+ <td className="px-6 py-4">
166
+ <StatusBadge status={camp.status} />
167
+ </td>
168
+ <td className="px-6 py-4 font-mono text-zinc-300">
169
+ {camp.lead_count?.toLocaleString() || 0}
170
+ </td>
171
+ <td className="px-6 py-4 font-mono text-zinc-300">
172
+ {camp.sent_count?.toLocaleString() || 0}
173
+ </td>
174
+ <td className="px-6 py-4 font-mono text-emerald-400 drop-shadow-[0_0_8px_rgba(52,211,153,0.3)]">
175
+ {camp.open_count?.toLocaleString() || 0}
176
+ </td>
177
+ <td className="px-6 py-4 font-mono text-purple-400 font-semibold drop-shadow-[0_0_8px_rgba(168,85,247,0.3)]">
178
+ {camp.reply_count?.toLocaleString() || 0}
179
+ </td>
180
+ <td className="px-6 py-4 font-mono text-rose-400 drop-shadow-[0_0_8px_rgba(244,63,94,0.3)]">
181
+ {camp.bounce_count || 0}
182
+ </td>
183
+ <td className="px-6 py-4 text-right">
184
+ <div className="flex items-center justify-end gap-1.5">
185
+ {camp.status === "ACTIVE" ? (
186
+ <button
187
+ onClick={() => updateStatusMutation.mutate({ id: camp.id, status: "PAUSE" })}
188
+ title="Pause Campaign"
189
+ className="p-1.5 bg-black/40 hover:bg-amber-500/20 text-zinc-400 hover:text-amber-400 rounded-md transition-colors border border-white/5 hover:border-amber-500/30"
190
+ >
191
+ <Pause className="w-3.5 h-3.5" />
192
+ </button>
193
+ ) : (
194
+ <button
195
+ onClick={() => updateStatusMutation.mutate({ id: camp.id, status: "START" })}
196
+ title="Start Campaign"
197
+ className="p-1.5 bg-black/40 hover:bg-emerald-500/20 text-zinc-400 hover:text-emerald-400 rounded-md transition-colors border border-white/5 hover:border-emerald-500/30"
198
+ >
199
+ <Play className="w-3.5 h-3.5" />
200
+ </button>
201
+ )}
202
+ <button
203
+ onClick={() => updateStatusMutation.mutate({ id: camp.id, status: "STOP" })}
204
+ title="Stop Campaign"
205
+ className="p-1.5 bg-black/40 hover:bg-rose-500/20 text-zinc-400 hover:text-rose-400 rounded-md transition-colors border border-white/5 hover:border-rose-500/30"
206
+ >
207
+ <Square className="w-3.5 h-3.5" />
208
+ </button>
209
+ <button
210
+ onClick={() => setSelectedForDelete(camp.id)}
211
+ title="Delete Campaign"
212
+ className="p-1.5 bg-black/40 hover:bg-rose-500/20 text-zinc-400 hover:text-rose-400 rounded-md transition-colors border border-white/5 hover:border-rose-500/30"
213
+ >
214
+ <Trash2 className="w-3.5 h-3.5" />
215
+ </button>
216
+ </div>
217
+ </td>
218
+ </tr>
219
+ ))}
220
+ </tbody>
221
+ </table>
222
+ </div>
223
+ ) : (
224
+ <div className="py-16 text-center">
225
+ <Megaphone className="w-12 h-12 text-zinc-600 mx-auto mb-3" />
226
+ <h3 className="text-base font-medium text-zinc-300">No campaigns found</h3>
227
+ <p className="text-xs text-zinc-500 mt-1 max-w-sm mx-auto font-sans">
228
+ Create your first campaign to begin dispatching automated cold outreach sequences.
229
+ </p>
230
+ <button
231
+ onClick={() => setIsCreateOpen(true)}
232
+ className="mt-4 px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white text-xs font-medium rounded-lg transition-colors shadow-[0_0_12px_rgba(79,70,229,0.5)] border border-indigo-500/50"
233
+ >
234
+ Create Campaign
235
+ </button>
236
+ </div>
237
+ )}
238
+ </div>
239
+
240
+ {/* Delete Campaign Confirmation */}
241
+ <ConfirmModal
242
+ isOpen={Boolean(selectedForDelete)}
243
+ onClose={() => setSelectedForDelete(null)}
244
+ onConfirm={() => selectedForDelete && deleteMutation.mutate(selectedForDelete)}
245
+ title="Delete Campaign"
246
+ description="Are you sure you want to permanently delete this campaign and all associated sequences? This action cannot be undone."
247
+ confirmText="Delete Campaign"
248
+ isDestructive={true}
249
+ isLoading={deleteMutation.isPending}
250
+ />
251
+
252
+ {/* Full Multi-Step Create Campaign Drawer */}
253
+ <FormDrawer
254
+ isOpen={isCreateOpen}
255
+ onClose={() => setIsCreateOpen(false)}
256
+ title="Launch New Campaign"
257
+ subtitle="Configure schedule parameters, tracking domain, and initial sequence step."
258
+ >
259
+ <form
260
+ onSubmit={async (e) => {
261
+ e.preventDefault();
262
+ const form = e.target as HTMLFormElement;
263
+ const data = {
264
+ name: (form.elements.namedItem("name") as HTMLInputElement).value,
265
+ timezone: (form.elements.namedItem("timezone") as HTMLSelectElement).value,
266
+ start_schedule: (form.elements.namedItem("start_schedule") as HTMLInputElement).value,
267
+ end_schedule: (form.elements.namedItem("end_schedule") as HTMLInputElement).value,
268
+ track_settings: (form.elements.namedItem("track_settings") as HTMLSelectElement).value,
269
+ send_as_plain_text: (form.elements.namedItem("send_as_plain_text") as HTMLInputElement).checked,
270
+ follow_up_percentage: Number((form.elements.namedItem("follow_up_percentage") as HTMLInputElement).value),
271
+ unsubscribe_text: (form.elements.namedItem("unsubscribe_text") as HTMLInputElement).value,
272
+ stop_lead_settings: (form.elements.namedItem("stop_lead_settings") as HTMLSelectElement).value,
273
+ initial_subject: (form.elements.namedItem("subject") as HTMLInputElement).value,
274
+ initial_body: (form.elements.namedItem("body") as HTMLTextAreaElement).value,
275
+ };
276
+
277
+ try {
278
+ await apiClient.post("/campaigns", data);
279
+ toast.success("Campaign launched successfully!");
280
+ setIsCreateOpen(false);
281
+ queryClient.invalidateQueries({ queryKey: ["campaigns"] });
282
+ } catch (err: any) {
283
+ toast.error(err.response?.data?.error || "Failed to create campaign");
284
+ }
285
+ }}
286
+ className="space-y-5 text-sm"
287
+ >
288
+ <div>
289
+ <label className="block font-medium text-zinc-300 mb-1">Campaign Name *</label>
290
+ <input
291
+ name="name"
292
+ required
293
+ placeholder="e.g. Enterprise Founders Outbound"
294
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500"
295
+ />
296
+ </div>
297
+
298
+ <div className="grid grid-cols-2 gap-3">
299
+ <div>
300
+ <label className="block font-medium text-zinc-300 mb-1">Start Time</label>
301
+ <input
302
+ type="time"
303
+ name="start_schedule"
304
+ defaultValue="09:00"
305
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500"
306
+ />
307
+ </div>
308
+ <div>
309
+ <label className="block font-medium text-zinc-300 mb-1">End Time</label>
310
+ <input
311
+ type="time"
312
+ name="end_schedule"
313
+ defaultValue="17:00"
314
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500"
315
+ />
316
+ </div>
317
+ </div>
318
+
319
+ <div>
320
+ <label className="block font-medium text-zinc-300 mb-1">Timezone</label>
321
+ <select
322
+ name="timezone"
323
+ defaultValue="America/New_York"
324
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500"
325
+ >
326
+ <option value="America/New_York">America/New_York (EST)</option>
327
+ <option value="America/Chicago">America/Chicago (CST)</option>
328
+ <option value="America/Los_Angeles">America/Los_Angeles (PST)</option>
329
+ <option value="Europe/London">Europe/London (GMT)</option>
330
+ <option value="Europe/Paris">Europe/Paris (CET)</option>
331
+ <option value="Asia/Singapore">Asia/Singapore (SGT)</option>
332
+ </select>
333
+ </div>
334
+
335
+ <div>
336
+ <label className="block font-medium text-zinc-300 mb-1">Tracking Settings</label>
337
+ <select
338
+ name="track_settings"
339
+ defaultValue="TRACK_EMAIL_OPEN_AND_CLICK"
340
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500"
341
+ >
342
+ <option value="TRACK_EMAIL_OPEN_AND_CLICK">Track Opens and Clicks</option>
343
+ <option value="TRACK_EMAIL_OPEN">Track Email Opens Only</option>
344
+ <option value="TRACK_EMAIL_CLICK">Track Email Clicks Only</option>
345
+ <option value="DONT_TRACK_EMAIL_OPEN">Do Not Track Opens or Clicks</option>
346
+ </select>
347
+ </div>
348
+
349
+ <div className="flex items-center justify-between p-3 bg-zinc-900 border border-zinc-800 rounded-lg">
350
+ <div>
351
+ <span className="font-medium text-zinc-200 block text-xs">Send as Plain Text</span>
352
+ <span className="text-[11px] text-zinc-500">Improves deliverability in strict enterprise inboxes</span>
353
+ </div>
354
+ <input
355
+ type="checkbox"
356
+ name="send_as_plain_text"
357
+ className="w-4 h-4 rounded text-indigo-600 focus:ring-indigo-500 bg-zinc-950 border-zinc-700"
358
+ />
359
+ </div>
360
+
361
+ <div>
362
+ <div className="flex justify-between text-xs font-medium text-zinc-300 mb-1">
363
+ <span>Follow-up Distribution %</span>
364
+ <span>85%</span>
365
+ </div>
366
+ <input
367
+ type="range"
368
+ name="follow_up_percentage"
369
+ min="10"
370
+ max="100"
371
+ defaultValue="85"
372
+ className="w-full accent-indigo-500"
373
+ />
374
+ </div>
375
+
376
+ <div>
377
+ <label className="block font-medium text-zinc-300 mb-1">Unsubscribe Message</label>
378
+ <input
379
+ name="unsubscribe_text"
380
+ defaultValue="Reply unsubscribe if you prefer not to receive updates."
381
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500 text-xs"
382
+ />
383
+ </div>
384
+
385
+ <div>
386
+ <label className="block font-medium text-zinc-300 mb-1">Stop Lead Condition</label>
387
+ <select
388
+ name="stop_lead_settings"
389
+ defaultValue="REPLIED"
390
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500 text-xs"
391
+ >
392
+ <option value="REPLIED">Stop sequence once lead replies</option>
393
+ <option value="OPENED">Stop sequence once lead opens email</option>
394
+ <option value="CLICKED">Stop sequence once lead clicks a link</option>
395
+ </select>
396
+ </div>
397
+
398
+ <div className="pt-2 border-t border-zinc-800 space-y-3">
399
+ <h4 className="text-xs font-semibold uppercase text-zinc-400">Step 1 Sequence Setup</h4>
400
+ <div>
401
+ <label className="block font-medium text-zinc-300 mb-1">Subject Line</label>
402
+ <input
403
+ name="subject"
404
+ required
405
+ defaultValue="Quick sync for {{first_name}}"
406
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500 text-xs"
407
+ />
408
+ </div>
409
+ <div>
410
+ <label className="block font-medium text-zinc-300 mb-1">Email Body</label>
411
+ <textarea
412
+ name="body"
413
+ rows={3}
414
+ required
415
+ defaultValue="<p>Hi {{first_name}},</p><p>Wanted to see how {{company_name}} is optimizing outbound deliverability?</p>"
416
+ className="w-full bg-zinc-900 border border-zinc-800 rounded-lg p-2.5 text-zinc-100 focus:outline-none focus:border-indigo-500 font-mono text-xs"
417
+ />
418
+ </div>
419
+ </div>
420
+
421
+ <div className="pt-4 flex justify-end gap-3 border-t border-zinc-800">
422
+ <button
423
+ type="button"
424
+ onClick={() => setIsCreateOpen(false)}
425
+ className="px-4 py-2 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 rounded-lg text-xs"
426
+ >
427
+ Cancel
428
+ </button>
429
+ <button
430
+ type="submit"
431
+ className="px-4 py-2 bg-indigo-600 hover:bg-indigo-500 text-white font-medium rounded-lg text-xs shadow-md"
432
+ >
433
+ Launch Campaign
434
+ </button>
435
+ </div>
436
+ </form>
437
+ </FormDrawer>
438
+ </div>
439
+ );
440
+ }