-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathpage.tsx
More file actions
321 lines (301 loc) · 11.1 KB
/
page.tsx
File metadata and controls
321 lines (301 loc) · 11.1 KB
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
"use client";
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@usesend/ui/src/breadcrumb";
import Link from "next/link";
import { H2 } from "@usesend/ui";
import Spinner from "@usesend/ui/src/spinner";
import { api } from "~/trpc/react";
import { use } from "react";
import { CampaignStatus } from "@prisma/client";
import { formatDistanceToNow } from "date-fns";
import TogglePauseCampaign from "../toggle-pause-campaign";
import CampaignStatusBadge from "../../campaigns/campaign-status-badge";
import { Button } from "@usesend/ui/src/button";
import { Card, CardContent, CardHeader, CardTitle } from "@usesend/ui/src/card";
import { EmailStatusBadge } from "../../emails/email-status-badge";
import { AnimatePresence, motion } from "framer-motion";
export default function CampaignDetailsPage({
params,
}: {
params: Promise<{ campaignId: string }>;
}) {
const { campaignId } = use(params);
const { data: campaign, isLoading } = api.campaign.getCampaign.useQuery(
{ campaignId: campaignId },
{
refetchInterval: (query) => {
const c: any = query.state.data;
if (!c) return false;
if (
c.status === CampaignStatus.RUNNING ||
c.status === CampaignStatus.PAUSED
) {
return 5000;
}
return false;
},
}
);
const { data: latestEmails, isLoading: latestEmailsLoading } =
api.campaign.latestEmails.useQuery(
{ campaignId: campaignId },
{
refetchInterval: 5000,
}
);
const { data: audience } = api.campaign.getCampaignAudience.useQuery({
campaignId,
});
if (isLoading) {
return (
<div className="flex items-center justify-center h-screen">
<Spinner className="w-5 h-5 text-foreground" />
</div>
);
}
if (!campaign) {
return <div>Campaign not found</div>;
}
const deliveredCount = campaign.delivered ?? 0;
const openedCount = campaign.opened ?? 0;
const clickedCount = campaign.clicked ?? 0;
const unsubscribedCount = campaign.unsubscribed ?? 0;
const deliveredDenominator = deliveredCount > 0 ? deliveredCount : 0;
const percentageOfDelivered = (value: number) =>
deliveredDenominator > 0 ? (value / deliveredDenominator) * 100 : 0;
const statisticsRows = [
{
status: "delivered",
count: deliveredCount,
percentage: deliveredDenominator > 0 ? 100 : 0,
},
{
status: "unsubscribed",
count: unsubscribedCount,
percentage: percentageOfDelivered(unsubscribedCount),
},
{
status: "clicked",
count: clickedCount,
percentage: percentageOfDelivered(clickedCount),
},
{
status: "opened",
count: openedCount,
percentage: percentageOfDelivered(openedCount),
},
];
const total = campaign.total ?? 0;
const processed = campaign.sent ?? 0;
return (
<div className="container mx-auto">
<div className="flex justify-between items-center">
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink asChild>
<Link href="/campaigns" className="text-lg">
Campaigns
</Link>
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator className="text-lg" />
<BreadcrumbItem>
<BreadcrumbPage className="text-lg ">
<div className="flex items-center gap-2">
<div className="max-w-[300px] truncate">{campaign.name}</div>
<CampaignStatusBadge status={campaign.status} />
</div>
</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
{campaign.status === "SCHEDULED" ? (
<Link href={`/campaigns/${campaign.id}/edit`}>
<Button>Edit</Button>
</Link>
) : (
<TogglePauseCampaign campaign={campaign} mode="full" />
)}
</div>
<div className="mt-10">
<div className="grid gap-6 lg:grid-cols-2">
<Card>
<CardHeader className="space-y-4">
<div className="flex flex-col gap-1">
<CardTitle className="text-sm font-mono">Statistics</CardTitle>
{total > 0 ? (
<div className="text-sm text-muted-foreground font-mono">
{processed.toLocaleString()} of {total.toLocaleString()}{" "}
processed
</div>
) : (
<div className="text-sm text-muted-foreground">
No recipients processed yet
</div>
)}
</div>
</CardHeader>
<CardContent className="space-y-4">
{statisticsRows.map((row, index) => (
<div
key={row.status}
className={`flex items-center justify-between gap-4 px-0 pb-3 ${
index !== statisticsRows.length - 1
? "border-b border-dashed border-border"
: ""
}`}
>
<div className="flex items-center gap-3">
<CampaignStatusIndicator status={row.status} />
<div>
<div className="text-sm capitalize">{row.status}</div>
</div>
</div>
<div className="text-right">
<div className="text-xl font-mono">{row.count}</div>
{row.status !== "delivered" ? (
<div className="text-xs text-muted-foreground">
{row.percentage.toFixed(1)}% of delivered
</div>
) : null}
</div>
</div>
))}
</CardContent>
</Card>
<Card>
<CardHeader className="flex gap-2">
<CardTitle className="text-sm font-mono">Live activity</CardTitle>
</CardHeader>
<CardContent>
<div className="flex h-[300px] flex-col">
{latestEmailsLoading ? (
<div className="flex flex-1 items-center justify-center">
<Spinner className="h-5 w-5 text-foreground" />
</div>
) : !latestEmails || latestEmails.length === 0 ? (
<div className="flex flex-1 items-center justify-center">
<div className="rounded text-sm text-muted-foreground">
No recent user actions yet.
</div>
</div>
) : (
<div className="space-y-4 overflow-y-auto overscroll-y-contain pr-1 no-scrollbar">
<AnimatePresence initial={true}>
{latestEmails.map((email) => {
const recipients = email.to ?? [];
const primaryRecipient =
recipients.length > 0
? recipients[0]
: "Unknown recipient";
const timestamp =
email.latestStatus === "SCHEDULED" &&
email.scheduledAt
? new Date(email.scheduledAt)
: new Date(email.updatedAt ?? email.createdAt);
const relativeTime = formatDistanceToNow(timestamp, {
addSuffix: true,
});
return (
<motion.div
key={email.id}
layout
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -12 }}
transition={{ duration: 0.2, ease: "easeOut" }}
className="flex flex-col gap-2 border-b pb-4 last:border-b-0 last:pb-0"
>
<div className="text-sm font-mono">
{primaryRecipient}
</div>
<div className="flex items-center justify-between gap-3">
<EmailStatusBadge status={email.latestStatus} />
<span className="whitespace-nowrap text-xs text-muted-foreground font-mono">
{relativeTime}
</span>
</div>
</motion.div>
);
})}
</AnimatePresence>
</div>
)}
</div>
</CardContent>
</Card>
</div>
</div>
{campaign.html && (
<div className=" rounded-lg mt-16">
<H2 className="mb-4">Email</H2>
<div className="p-2 rounded-lg border shadow flex flex-col gap-4 w-full">
<div className="flex flex-col gap-3 px-4 py-1">
<div className=" flex text-sm">
<div className="w-[70px] text-muted-foreground">Subject</div>
<div> {campaign.subject}</div>
</div>
<div className="flex text-sm">
<div className="w-[70px] text-muted-foreground">From</div>
<div> {campaign.from}</div>
</div>
<div className="flex text-sm">
<div className="w-[70px] text-muted-foreground">Audience</div>
<div>
{campaign.contactSegment?.name ?? "All contacts"}
{audience ? ` (${audience.count.toLocaleString()})` : ""}
</div>
</div>
<div className="flex text-sm items-center">
<div className="w-[70px] text-muted-foreground">Contact</div>
<Link
href={`/contacts/${campaign.contactBookId}`}
target="_blank"
>
<div className="bg-secondary p-0.5 px-2 rounded-md ">
{campaign.contactBook?.emoji}
{campaign.contactBook?.name}
</div>
</Link>
</div>
</div>
<div className=" dark:bg-slate-50 overflow-auto text-black rounded py-8 border-t">
<div dangerouslySetInnerHTML={{ __html: campaign.html ?? "" }} />
</div>
</div>
</div>
)}
</div>
);
}
const CampaignStatusIndicator: React.FC<{ status: string }> = ({ status }) => {
let colorClass = "bg-gray";
switch (status) {
case "delivered":
colorClass = "bg-green";
break;
case "bounced":
case "unsubscribed":
colorClass = "bg-red";
break;
case "clicked":
colorClass = "bg-blue";
break;
case "opened":
colorClass = "bg-purple";
break;
case "complained":
colorClass = "bg-yellow";
break;
default:
colorClass = "bg-gray";
}
return <div className={`h-2.5 w-2.5 rounded-[2px] ${colorClass}`} />;
};