-
Notifications
You must be signed in to change notification settings - Fork 732
/
Copy pathcommon.tsx
539 lines (518 loc) · 14.3 KB
/
common.tsx
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
"use client";
import type React from "react";
import clsx from "clsx";
import Link from "next/link";
import useSWR from "swr";
import {
ArchiveIcon,
ArchiveXIcon,
BadgeCheckIcon,
ChevronDown,
ChevronDownIcon,
ChevronsUpDownIcon,
ExpandIcon,
ExternalLinkIcon,
MailMinusIcon,
MoreHorizontalIcon,
PlusCircle,
TagIcon,
TrashIcon,
} from "lucide-react";
import { type PostHog, usePostHog } from "posthog-js/react";
import { Button } from "@/components/ui/button";
import { ButtonLoader } from "@/components/Loading";
import { Tooltip } from "@/components/Tooltip";
import { Separator } from "@/components/ui/separator";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuPortal,
DropdownMenuSeparator,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
PremiumTooltip,
PremiumTooltipContent,
} from "@/components/PremiumAlert";
import { GroupItemType, NewsletterStatus } from "@/generated/prisma";
import type { GroupsResponse } from "@/app/api/user/group/route";
import { addGroupItemAction } from "@/utils/actions/group";
import { toastError, toastSuccess } from "@/components/Toast";
import { createFilterAction } from "@/utils/actions/mail";
import { isActionError } from "@/utils/error";
import { getGmailSearchUrl } from "@/utils/url";
import type { Row } from "@/app/(app)/bulk-unsubscribe/types";
import {
useUnsubscribe,
useAutoArchive,
useApproveButton,
useArchiveAll,
useDeleteAllFromSender,
} from "@/app/(app)/bulk-unsubscribe/hooks";
import { LabelsSubMenu } from "@/components/LabelsSubMenu";
import type { UserLabel } from "@/hooks/useLabels";
export function ActionCell<T extends Row>({
item,
hasUnsubscribeAccess,
mutate,
refetchPremium,
onOpenNewsletter,
labels,
openPremiumModal,
userEmail,
}: {
item: T;
hasUnsubscribeAccess: boolean;
mutate: () => Promise<void>;
refetchPremium: () => Promise<any>;
onOpenNewsletter: (row: T) => void;
selected: boolean;
labels: UserLabel[];
openPremiumModal: () => void;
userEmail: string;
}) {
const posthog = usePostHog();
return (
<>
<PremiumTooltip
showTooltip={!hasUnsubscribeAccess}
openModal={openPremiumModal}
>
<UnsubscribeButton
item={item}
hasUnsubscribeAccess={hasUnsubscribeAccess}
mutate={mutate}
posthog={posthog}
refetchPremium={refetchPremium}
/>
</PremiumTooltip>
<Tooltip
contentComponent={
!hasUnsubscribeAccess ? (
<PremiumTooltipContent openModal={openPremiumModal} />
) : undefined
}
content={
hasUnsubscribeAccess
? "Auto archive emails using Gmail filters."
: undefined
}
>
<AutoArchiveButton
item={item}
hasUnsubscribeAccess={hasUnsubscribeAccess}
mutate={mutate}
posthog={posthog}
refetchPremium={refetchPremium}
labels={labels}
/>
</Tooltip>
<Tooltip
contentComponent={
!hasUnsubscribeAccess ? (
<PremiumTooltipContent openModal={openPremiumModal} />
) : undefined
}
content={
hasUnsubscribeAccess
? "Approve to filter it from the list."
: undefined
}
>
<ApproveButton
item={item}
hasUnsubscribeAccess={hasUnsubscribeAccess}
mutate={mutate}
posthog={posthog}
/>
</Tooltip>
<MoreDropdown
onOpenNewsletter={onOpenNewsletter}
item={item}
userEmail={userEmail}
labels={labels}
posthog={posthog}
/>
</>
);
}
function UnsubscribeButton<T extends Row>({
item,
hasUnsubscribeAccess,
mutate,
posthog,
refetchPremium,
}: {
item: T;
hasUnsubscribeAccess: boolean;
mutate: () => Promise<void>;
refetchPremium: () => Promise<any>;
posthog: PostHog;
}) {
const { unsubscribeLoading, onUnsubscribe, unsubscribeLink } = useUnsubscribe(
{
item,
hasUnsubscribeAccess,
mutate,
posthog,
refetchPremium,
},
);
const hasUnsubscribeLink = unsubscribeLink !== "#";
return (
<Button
size="sm"
variant={
item.status === NewsletterStatus.UNSUBSCRIBED ? "red" : "secondary"
}
asChild
>
<Link
href={unsubscribeLink}
target={hasUnsubscribeLink ? "_blank" : undefined}
onClick={onUnsubscribe}
rel="noreferrer"
>
{unsubscribeLoading && <ButtonLoader />}
<span className="hidden xl:block">
{hasUnsubscribeLink ? "Unsubscribe" : "Block"}
</span>
<span className="block xl:hidden">
<Tooltip content={hasUnsubscribeLink ? "Unsubscribe" : "Block"}>
<MailMinusIcon className="size-4" />
</Tooltip>
</span>
</Link>
</Button>
);
}
function AutoArchiveButton<T extends Row>({
item,
hasUnsubscribeAccess,
mutate,
posthog,
refetchPremium,
labels,
}: {
item: T;
hasUnsubscribeAccess: boolean;
mutate: () => Promise<void>;
posthog: PostHog;
refetchPremium: () => Promise<any>;
labels: UserLabel[];
}) {
const {
autoArchiveLoading,
onAutoArchive,
onAutoArchiveAndLabel,
onDisableAutoArchive,
} = useAutoArchive({
item,
hasUnsubscribeAccess,
mutate,
posthog,
refetchPremium,
});
return (
<div
className={clsx(
"flex h-min items-center gap-1 rounded-md text-secondary-foreground",
item.autoArchived ? "bg-blue-100 dark:bg-blue-800" : "bg-secondary",
)}
>
<Button
variant={
item.status === NewsletterStatus.AUTO_ARCHIVED || item.autoArchived
? "blue"
: "secondary"
}
className="px-3 shadow-none"
size="sm"
onClick={onAutoArchive}
disabled={!hasUnsubscribeAccess}
>
{autoArchiveLoading && <ButtonLoader />}
<span className="hidden xl:block">Auto Archive</span>
<span className="block xl:hidden">
<Tooltip content="Auto Archive">
<ArchiveIcon className="size-4" />
</Tooltip>
</span>
</Button>
<Separator orientation="vertical" className="h-[20px]" />
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant={
item.status === NewsletterStatus.AUTO_ARCHIVED ||
item.autoArchived
? "blue"
: "secondary"
}
className="px-2 shadow-none"
size="sm"
disabled={!hasUnsubscribeAccess}
>
<ChevronDownIcon className="size-4 text-secondary-foreground" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
alignOffset={-5}
className="max-h-[415px] w-[220px] overflow-auto"
forceMount
onKeyDown={(e) => {
e.stopPropagation();
}}
>
{item.autoArchived?.id && (
<>
<DropdownMenuItem
onClick={async () => {
posthog.capture("Clicked Disable Auto Archive");
onDisableAutoArchive();
}}
>
<ArchiveXIcon className="mr-2 size-4" /> Disable Auto Archive
</DropdownMenuItem>
<DropdownMenuSeparator />
</>
)}
<DropdownMenuLabel>Auto Archive and Label</DropdownMenuLabel>
<DropdownMenuSeparator />
{labels.map((label) => {
return (
<DropdownMenuItem
key={label.id}
onClick={async () => {
posthog.capture("Clicked Auto Archive and Label");
await onAutoArchiveAndLabel(label.id!);
}}
>
{label.name}
</DropdownMenuItem>
);
})}
{!labels.length && (
<DropdownMenuItem>
You do not have any labels. Create one in Gmail first to auto
label emails.
</DropdownMenuItem>
)}
</DropdownMenuContent>
</DropdownMenu>
</div>
);
}
function ApproveButton<T extends Row>({
item,
hasUnsubscribeAccess,
mutate,
posthog,
}: {
item: T;
hasUnsubscribeAccess: boolean;
mutate: () => Promise<void>;
posthog: PostHog;
}) {
const { approveLoading, onApprove } = useApproveButton({
item,
mutate,
posthog,
});
return (
<Button
size="sm"
variant={
item.status === NewsletterStatus.APPROVED ? "green" : "secondary"
}
onClick={onApprove}
disabled={!hasUnsubscribeAccess}
loading={approveLoading}
>
<span className="hidden 2xl:block">Keep</span>
<span className="block 2xl:hidden">
<Tooltip content="Keep">
<BadgeCheckIcon className="size-4" />
</Tooltip>
</span>
</Button>
);
}
export function MoreDropdown<T extends Row>({
onOpenNewsletter,
item,
userEmail,
labels,
posthog,
}: {
onOpenNewsletter?: (row: T) => void;
item: T;
userEmail: string;
labels: UserLabel[];
posthog: PostHog;
}) {
const { archiveAllLoading, onArchiveAll } = useArchiveAll({
item,
posthog,
});
const { deleteAllLoading, onDeleteAll } = useDeleteAllFromSender({
item,
posthog,
});
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button aria-haspopup="true" size="icon" variant="ghost">
<MoreHorizontalIcon className="size-4" />
<span className="sr-only">Toggle menu</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{!!onOpenNewsletter && (
<DropdownMenuItem onClick={() => onOpenNewsletter(item)}>
<ExpandIcon className="mr-2 size-4" />
<span>View stats</span>
</DropdownMenuItem>
)}
<DropdownMenuItem asChild>
<Link href={getGmailSearchUrl(item.name, userEmail)} target="_blank">
<ExternalLinkIcon className="mr-2 size-4" />
<span>View in Gmail</span>
</Link>
</DropdownMenuItem>
{/* <DropdownMenuSub>
<DropdownMenuSubTrigger>
<UserPlus className="mr-2 size-4" />
<span>Add sender to rule</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<GroupsSubMenu sender={item.name} />
</DropdownMenuPortal>
</DropdownMenuSub> */}
<DropdownMenuSub>
<DropdownMenuSubTrigger>
<TagIcon className="mr-2 size-4" />
<span>Label future emails</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<LabelsSubMenu
labels={labels}
onClick={async (label) => {
const res = await createFilterAction(item.name, label.id);
if (isActionError(res)) {
toastError({
title: "Error",
description: `Failed to add ${item.name} to ${label.name}. ${res.error}`,
});
} else {
toastSuccess({
title: "Success!",
description: `Added ${item.name} to ${label.name}`,
});
}
}}
/>
</DropdownMenuPortal>
</DropdownMenuSub>
<DropdownMenuItem onClick={onArchiveAll}>
{archiveAllLoading ? (
<ButtonLoader />
) : (
<ArchiveIcon className="mr-2 size-4" />
)}
<span>Archive all</span>
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => {
const yes = confirm(
`Are you sure you want to delete all emails from ${item.name}?`,
);
if (!yes) return;
onDeleteAll();
}}
>
{deleteAllLoading ? (
<ButtonLoader />
) : (
<TrashIcon className="mr-2 size-4" />
)}
<span>Delete all</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
export function HeaderButton(props: {
children: React.ReactNode;
sorted: boolean;
onClick: () => void;
}) {
return (
<Button
variant="ghost"
size="sm"
className="-ml-3 h-8 data-[state=open]:bg-accent"
onClick={props.onClick}
>
<span>{props.children}</span>
{props.sorted ? (
<ChevronDown className="ml-2 size-4" />
) : (
<ChevronsUpDownIcon className="ml-2 size-4" />
)}
</Button>
);
}
function GroupsSubMenu({ sender }: { sender: string }) {
const { data, isLoading, error } = useSWR<GroupsResponse>("/api/user/group");
return (
<DropdownMenuSubContent>
{data &&
(data.groups.length ? (
data?.groups.map((group) => {
return (
<DropdownMenuItem
key={group.id}
onClick={async () => {
const result = await addGroupItemAction({
groupId: group.id,
type: GroupItemType.FROM,
value: sender,
});
if (isActionError(result)) {
toastError({
description: `Failed to add ${sender} to ${group.name}. ${result.error}`,
});
} else {
toastSuccess({
title: "Success!",
description: `Added ${sender} to ${group.name}`,
});
}
}}
>
{group.name}
</DropdownMenuItem>
);
})
) : (
<DropdownMenuItem>{`You don't have any groups yet.`}</DropdownMenuItem>
))}
{isLoading && <DropdownMenuItem>Loading...</DropdownMenuItem>}
{error && <DropdownMenuItem>Error loading groups</DropdownMenuItem>}
<DropdownMenuSeparator />
<DropdownMenuItem asChild>
<Link href="/automation?tab=groups" target="_blank">
<PlusCircle className="mr-2 size-4" />
<span>New Group</span>
</Link>
</DropdownMenuItem>
</DropdownMenuSubContent>
);
}