-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathsubscriber-row.tsx
More file actions
286 lines (268 loc) · 10.6 KB
/
subscriber-row.tsx
File metadata and controls
286 lines (268 loc) · 10.6 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
import { ISubscriberResponseDto, PermissionsEnum } from '@novu/shared';
import { useQueryClient } from '@tanstack/react-query';
import { ComponentProps, useState } from 'react';
import { RiDeleteBin2Line, RiFileCopyLine, RiMore2Fill, RiPulseFill } from 'react-icons/ri';
import { Link, useLocation } from 'react-router-dom';
import { ExternalToast } from 'sonner';
import { ConfirmationModal } from '@/components/confirmation-modal';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/primitives/avatar';
import { CompactButton } from '@/components/primitives/button-compact';
import { CopyButton } from '@/components/primitives/copy-button';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/primitives/dropdown-menu';
import { Skeleton } from '@/components/primitives/skeleton';
import { ToastIcon } from '@/components/primitives/sonner';
import { showToast } from '@/components/primitives/sonner-helpers';
import { TableCell, TableRow } from '@/components/primitives/table';
import { useSubscribersNavigate } from '@/components/subscribers/hooks/use-subscribers-navigate';
import { getSubscriberTitle } from '@/components/subscribers/utils';
import { TimeDisplayHoverCard } from '@/components/time-display-hover-card';
import TruncatedText from '@/components/truncated-text';
import { useEnvironment } from '@/context/environment/hooks';
import { useDeleteSubscriber } from '@/hooks/use-delete-subscriber';
import { formatDateSimple } from '@/utils/format-date';
import { Protect } from '@/utils/protect';
import { QueryKeys } from '@/utils/query-keys';
import { buildRoute, ROUTES } from '@/utils/routes';
import { cn } from '@/utils/ui';
import { useSubscribersUrlState } from './hooks/use-subscribers-url-state';
const toastOptions: ExternalToast = {
position: 'bottom-right',
classNames: {
toast: 'mb-4 right-0',
},
};
type SubscriberRowProps = {
subscriber: ISubscriberResponseDto;
subscribersCount: number;
firstTwoSubscribersInternalIds: string[];
};
type SubscriberLinkTableCellProps = ComponentProps<typeof TableCell> & {
to?: string;
};
const SubscriberTableCell = (props: SubscriberLinkTableCellProps) => {
const { children, className, to, ...rest } = props;
return (
<TableCell className={cn('group-hover:bg-neutral-alpha-50 text-text-sub relative', className)} {...rest}>
{to && (
<Link to={to} className="absolute inset-0" tabIndex={-1}>
<span className="sr-only">Edit subscriber</span>
</Link>
)}
{children}
</TableCell>
);
};
export const SubscriberRow = ({ subscriber, subscribersCount, firstTwoSubscribersInternalIds }: SubscriberRowProps) => {
const { currentEnvironment } = useEnvironment();
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const subscriberTitle = getSubscriberTitle(subscriber);
const queryClient = useQueryClient();
const location = useLocation();
const { navigateToSubscribersFirstPage } = useSubscribersNavigate();
const { handleNavigationAfterDelete } = useSubscribersUrlState();
const subscriberLink = `${buildRoute(ROUTES.EDIT_SUBSCRIBER, {
environmentSlug: currentEnvironment?.slug ?? '',
subscriberId: encodeURIComponent(subscriber.subscriberId),
})}${location.search}`;
const { deleteSubscriber, isPending: isDeleteSubscriberPending } = useDeleteSubscriber({
onSuccess: () => {
showToast({
children: () => (
<>
<ToastIcon variant="success" />
<span className="text-sm">
Deleted subscriber <span className="font-bold">{subscriberTitle}</span>.
</span>
</>
),
options: toastOptions,
});
},
onError: () => {
showToast({
children: () => (
<>
<ToastIcon variant="error" />
<span className="text-sm">
Failed to delete subscriber <span className="font-bold">{subscriberTitle}</span>.
</span>
</>
),
options: toastOptions,
});
},
});
const stopPropagation = (e: React.MouseEvent) => {
// don't propagate the click event to the row
e.stopPropagation();
};
const handleDeletion = async () => {
try {
await deleteSubscriber({ subscriberId: subscriber.subscriberId });
} catch {
// errors are handled by the mutation's onError callback
return;
}
setIsDeleteModalOpen(false);
const hasSingleSubscriber = subscribersCount === 1;
if (hasSingleSubscriber) {
queryClient.invalidateQueries({
queryKey: [QueryKeys.fetchSubscribers],
});
navigateToSubscribersFirstPage();
return;
}
const hasTwoSubscribersInternalIds = firstTwoSubscribersInternalIds.length === 2 && !hasSingleSubscriber;
const firstSubscriberInternalId = firstTwoSubscribersInternalIds[0];
const isFirstSubscriberBeingDeleted = subscriber._id === firstSubscriberInternalId;
let afterCursor = firstSubscriberInternalId;
/**
* If the first subscriber is being deleted and there are more than one subscribers on the list then
* fetch the list from the second subscriber onwards.
*/
if (isFirstSubscriberBeingDeleted && hasTwoSubscribersInternalIds) {
afterCursor = firstTwoSubscribersInternalIds[1];
}
handleNavigationAfterDelete(afterCursor);
};
return (
<>
<TableRow
key={subscriber.subscriberId}
className="group relative isolate cursor-pointer"
>
<SubscriberTableCell to={subscriberLink}>
<div className="flex items-center gap-3">
<Avatar>
<AvatarImage src={subscriber.avatar || undefined} />
<AvatarFallback>{subscriberTitle[0]}</AvatarFallback>
</Avatar>
<div className="flex flex-col">
<TruncatedText className="text-text-strong max-w-[36ch] font-medium">{subscriberTitle}</TruncatedText>
<div className="flex items-center gap-1 transition-opacity duration-200">
<TruncatedText className="text-text-soft font-code block max-w-[40ch] text-xs">
{subscriber.subscriberId}
</TruncatedText>
<CopyButton
className="z-10 flex size-2 p-0 px-1 opacity-0 group-hover:opacity-100"
valueToCopy={subscriber.subscriberId}
size="2xs"
/>
</div>
</div>
</div>
</SubscriberTableCell>
<SubscriberTableCell to={subscriberLink}>
<TruncatedText className="relative z-10 max-w-[28ch]">{subscriber.email || '-'}</TruncatedText>
</SubscriberTableCell>
<SubscriberTableCell to={subscriberLink}>{subscriber.phone || '-'}</SubscriberTableCell>
<SubscriberTableCell to={subscriberLink}>
<TimeDisplayHoverCard date={new Date(subscriber.createdAt)}>
{formatDateSimple(subscriber.createdAt)}
</TimeDisplayHoverCard>
</SubscriberTableCell>
<SubscriberTableCell to={subscriberLink}>
<TimeDisplayHoverCard date={new Date(subscriber.updatedAt)}>
{formatDateSimple(subscriber.updatedAt)}
</TimeDisplayHoverCard>
</SubscriberTableCell>
<SubscriberTableCell className="w-1">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<CompactButton icon={RiMore2Fill} variant="ghost" className="z-10 h-8 w-8 p-0" />
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56" onClick={stopPropagation}>
<DropdownMenuGroup>
<DropdownMenuItem
className="cursor-pointer"
onClick={() => {
navigator.clipboard.writeText(subscriber.subscriberId);
}}
>
<RiFileCopyLine />
Copy identifier
</DropdownMenuItem>
<Protect permission={PermissionsEnum.NOTIFICATION_READ}>
<DropdownMenuItem asChild className="cursor-pointer">
<Link
to={
buildRoute(ROUTES.ACTIVITY_FEED, {
environmentSlug: currentEnvironment?.slug ?? '',
}) +
'?' +
new URLSearchParams({ subscriberId: subscriber.subscriberId }).toString()
}
>
<RiPulseFill />
View activity
</Link>
</DropdownMenuItem>
</Protect>
<Protect permission={PermissionsEnum.SUBSCRIBER_WRITE}>
<DropdownMenuItem
className="text-destructive cursor-pointer"
onClick={() => {
setTimeout(() => setIsDeleteModalOpen(true), 0);
}}
>
<RiDeleteBin2Line />
Delete subscriber
</DropdownMenuItem>
</Protect>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</SubscriberTableCell>
</TableRow>
<ConfirmationModal
open={isDeleteModalOpen}
onOpenChange={setIsDeleteModalOpen}
onConfirm={handleDeletion}
title={`Delete subscriber`}
description={
<span>
Are you sure you want to delete subscriber{' '}
<TruncatedText className="max-w-[20ch] font-bold">{subscriberTitle}</TruncatedText>? This action cannot be
undone.
</span>
}
confirmButtonText="Delete subscriber"
isLoading={isDeleteSubscriberPending}
/>
</>
);
};
export const SubscriberRowSkeleton = () => {
return (
<TableRow className="group relative isolate">
<TableCell className="flex items-center gap-3">
<Skeleton className="size-10 rounded-full" />
<div className="space-y-1">
<Skeleton className="h-5 w-[20ch]" />
<Skeleton className="h-3 w-[15ch] rounded-full" />
</div>
</TableCell>
<TableCell>
<Skeleton className="h-5 w-[6ch] rounded-full" />
</TableCell>
<TableCell>
<Skeleton className="h-5 w-[8ch] rounded-full" />
</TableCell>
<TableCell>
<Skeleton className="h-5 w-[7ch] rounded-full" />
</TableCell>
<TableCell>
<Skeleton className="h-5 w-[14ch] rounded-full" />
</TableCell>
<TableCell className="w-1">
<RiMore2Fill className="size-4 opacity-50" />
</TableCell>
</TableRow>
);
};