-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathpartner-info-cards.tsx
More file actions
605 lines (566 loc) · 20.4 KB
/
Copy pathpartner-info-cards.tsx
File metadata and controls
605 lines (566 loc) · 20.4 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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import { useAttributeReferringPartnerModal } from "@/lib/partner-referrals/components/attribute-referring-partner-modal";
import { usePartnerReferral } from "@/lib/partner-referrals/hooks/use-partner-referral";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import useGroup from "@/lib/swr/use-group";
import useWorkspace from "@/lib/swr/use-workspace";
import {
AdminNetworkPartner,
BountyListProps,
EnrolledPartnerExtendedProps,
NetworkPartnerProps,
RewardProps,
} from "@/lib/types";
import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";
import { INACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners";
import { usePartnerGroupHistorySheet } from "@/ui/activity-logs/partner-group-history-sheet";
import {
Button,
CalendarIcon,
CopyButton,
Globe,
Heart,
OfficeBuilding,
TimestampTooltip,
Trophy,
} from "@dub/ui";
import {
TriangleWarning,
UserArrowRight,
Users,
VerifiedBadge,
} from "@dub/ui/icons";
import {
COUNTRIES,
fetcher,
formatDate,
formatDateTimeSmart,
} from "@dub/utils";
import { CircleMinus } from "lucide-react";
import Link from "next/link";
import { Fragment, ReactNode, createElement } from "react";
import useSWR from "swr";
import { PartnerApplicationRiskSummary } from "./fraud-risks/partner-application-risk-summary";
import {
PartnerApplicationFraudBanner,
PartnerFraudBanner,
} from "./fraud-risks/partner-fraud-banner";
import { PartnerFraudIndicator } from "./fraud-risks/partner-fraud-indicator";
import { PartnerApplicationSource } from "./partner-application-source";
import { PartnerAvatar } from "./partner-avatar";
import { PartnerInfoGroup } from "./partner-info-group";
import { PartnerStarButton } from "./partner-star-button";
import { PartnerStatusBadgeWithTooltip } from "./partner-status-badge-with-tooltip";
import { PartnerTagsList } from "./partner-tags-list";
import {
getPayoutMethodIconConfig,
getPayoutMethodLabel,
} from "./payouts/payout-method-config";
import { ProgramRewardList } from "./program-reward-list";
import { TrustedPartnerBadge } from "./trusted-partner-badge";
import {
UpdatePartnerTagsModal,
useUpdatePartnerTagsModal,
} from "./update-partner-tags-modal";
type PartnerInfoCardsProps = {
showFraudIndicator?: boolean;
showApplicationRiskAnalysis?: boolean;
controls?: ReactNode;
/** Partner statuses to hide badges for */
hideStatuses?: EnrolledPartnerExtendedProps["status"][];
// Only used for a controlled group selector that doesn't persist the selection itself
selectedGroupId?: string | null;
setSelectedGroupId?: (groupId: string) => void;
} & (
| { type?: "enrolled"; partner?: EnrolledPartnerExtendedProps }
| { type: "network"; partner?: NetworkPartnerProps }
| { type: "admin"; partner?: AdminNetworkPartner }
);
type BasicField = {
id: string;
icon: React.ReactElement;
text: ReactNode | null | undefined;
/** When set, the row is wrapped in TimestampTooltip (local / UTC / unix). */
timestamp?: Date | string | number;
/** Optional outer wrapper (e.g. ConversionScoreTooltip) around the row content. */
wrapper?: React.ComponentType<{ children: React.ReactNode }>;
};
export function PartnerInfoCards({
type,
partner,
controls,
hideStatuses = [],
selectedGroupId,
setSelectedGroupId,
showFraudIndicator = true,
showApplicationRiskAnalysis = false,
}: PartnerInfoCardsProps) {
const { id: workspaceId, slug: workspaceSlug, plan } = useWorkspace();
const { canCreateReferralReward } = getPlanCapabilities(plan);
const isEnrolled = type === "enrolled" || type === undefined;
const isNetwork = type === "network";
const isAdmin = type === "admin";
const {
partnerGroupHistorySheet,
setIsOpen: setGroupHistoryOpen,
hasActivityLogs,
} = usePartnerGroupHistorySheet({ partner: partner || null });
const { group } = useGroup(
{
groupIdOrSlug: partner
? selectedGroupId ||
("groupId" in partner ? partner.groupId : null) ||
DEFAULT_PARTNER_GROUP.slug
: undefined,
},
{ keepPreviousData: false },
);
const { data: bounties, error: errorBounties } = useSWR<BountyListProps[]>(
workspaceId && partner && isEnrolled
? `/api/bounties?workspaceId=${workspaceId}&partnerId=${partner.id}`
: null,
fetcher,
);
let basicFields: BasicField[] = [
{
id: "country",
icon: partner?.country ? (
<img
alt={`Flag of ${COUNTRIES[partner.country]}`}
src={`https://flag.vercel.app/m/${partner.country}.svg`}
className="size-3.5 rounded-full"
/>
) : (
<Globe className="size-3.5 shrink-0" />
),
text: partner?.country ? COUNTRIES[partner.country] : "Planet Earth",
},
{
id: "companyName",
icon: <OfficeBuilding className="size-3.5" />,
text: partner ? partner.companyName || null : undefined,
},
];
if ((isEnrolled || isAdmin) && partner) {
const isPendingApplication =
"status" in partner && partner.status === "pending";
const applicationReferralSource =
isEnrolled && partner.applicationEvent?.referralSource;
basicFields = basicFields.concat([
{
id: "createdAt",
icon: isPendingApplication ? (
<CalendarIcon className="size-3.5" />
) : (
<Users className="size-3.5" />
),
text: isPendingApplication ? (
<span className="inline-flex flex-wrap items-center gap-1">
<TimestampTooltip
timestamp={partner.createdAt}
rows={["local", "utc", "unix"]}
side="left"
delayDuration={250}
>
<span>Applied {formatDate(partner.createdAt)}</span>
</TimestampTooltip>
{applicationReferralSource && (
<>
<span>via</span>
<PartnerApplicationSource
referralSource={applicationReferralSource}
variant="inline"
/>
</>
)}
</span>
) : (
`${isPendingApplication ? "Applied" : "Partner since"} ${formatDate(partner.createdAt)}`
),
timestamp:
isPendingApplication && applicationReferralSource
? undefined
: partner.createdAt,
},
{
id: "payoutMethod" as const,
icon: partner.defaultPayoutMethod ? (
createElement(
getPayoutMethodIconConfig(partner.defaultPayoutMethod).Icon,
{ className: "size-3.5 shrink-0" },
)
) : (
<CircleMinus className="size-3.5 shrink-0" />
),
text:
partner.defaultPayoutMethod && partner.payoutsEnabledAt
? `${getPayoutMethodLabel(partner.defaultPayoutMethod)} connected ${formatDateTimeSmart(partner.payoutsEnabledAt)}`
: "No payout method connected",
...(partner.payoutsEnabledAt
? { timestamp: partner.payoutsEnabledAt }
: {}),
},
// TODO: once more partners verify their identity, we can show this by default
...(partner.identityVerifiedAt
? [
{
id: "identityVerifiedAt",
icon: partner.identityVerifiedAt ? (
<VerifiedBadge className="size-3.5 shrink-0" />
) : (
<TriangleWarning className="size-3.5 shrink-0" />
),
text: partner.identityVerifiedAt
? `Identity verified ${formatDate(partner.identityVerifiedAt, { month: "short" })}`
: "Identity not verified",
...(partner.identityVerifiedAt
? { timestamp: partner.identityVerifiedAt }
: {}),
},
]
: []),
// Referred by
...(isEnrolled && canCreateReferralReward
? [
{
id: "referredBy",
icon: <UserArrowRight className="size-3.5 shrink-0" />,
text: <ReferredByPartner partner={partner} />,
},
]
: []),
]);
}
if (isNetwork) {
basicFields = basicFields.concat([
{
id: "joinedAt",
icon: <CalendarIcon className="size-3.5" />,
text: partner ? `Joined ${formatDate(partner.createdAt!)}` : undefined,
timestamp: partner?.createdAt,
},
]);
}
return (
<div className="flex flex-col gap-4">
<div className="overflow-hidden rounded-xl bg-red-100">
{partner &&
isEnrolled &&
(partner.status === "pending" ? (
<PartnerApplicationFraudBanner partner={partner} />
) : (
<PartnerFraudBanner partner={partner} />
))}
<div className="border-border-subtle flex flex-col divide-y divide-neutral-200 rounded-xl border bg-white">
<div className="p-4">
<div className="flex items-start justify-between gap-2">
<div className="relative w-fit shrink-0">
{partner ? (
<PartnerAvatar
partner={partner}
className="size-20 border border-neutral-100"
/>
) : (
<div className="size-20 animate-pulse rounded-full bg-neutral-200" />
)}
{partner?.networkStatus === "trusted" && (
<TrustedPartnerBadge />
)}
</div>
<div className="flex items-center gap-2">
{isEnrolled &&
partner &&
!hideStatuses.includes(partner.status) && (
<PartnerStatusBadgeWithTooltip partner={partner} />
)}
{isNetwork && partner && (
<PartnerStarButton partner={partner} className="size-9" />
)}
{controls}
</div>
</div>
<div className="mt-4">
{partner ? (
<div className="flex items-center gap-2">
<span className="text-content-emphasis text-lg font-semibold">
{partner.name}
</span>
{showFraudIndicator && (
<PartnerFraudIndicator partnerId={partner.id} />
)}
</div>
) : (
<div className="h-7 w-24 animate-pulse rounded bg-neutral-200" />
)}
</div>
{(isEnrolled || isAdmin) &&
(partner ? (
partner.email && (
<div className="mt-0.5 flex items-center gap-1">
<span className="text-sm font-medium text-neutral-500">
{partner.email}
</span>
<CopyButton
value={partner.email}
variant="neutral"
className="p-1 [&>*]:h-3 [&>*]:w-3"
successMessage="Copied email to clipboard!"
/>
</div>
)
) : (
<div className="mt-0.5 h-5 w-32 animate-pulse rounded bg-neutral-200" />
))}
</div>
<div className="flex flex-col gap-2 p-4">
{basicFields
.filter(({ text }) => text !== null)
.map(({ id, icon, text, timestamp, wrapper: RowWrapper }) => {
const rowInner = (
<div className="text-content-subtle flex items-center gap-1">
{text !== undefined ? (
<>
{icon}
{typeof text === "string" ? (
<span className="text-xs font-medium">{text}</span>
) : (
<div className="text-xs font-medium">{text}</div>
)}
</>
) : (
<div className="h-4 w-24 animate-pulse rounded bg-neutral-200" />
)}
</div>
);
const withTimestamp =
timestamp != null ? (
<TimestampTooltip
timestamp={timestamp}
rows={["local", "utc", "unix"]}
side="left"
delayDuration={250}
>
{rowInner}
</TimestampTooltip>
) : (
rowInner
);
return (
<Fragment key={id}>
{RowWrapper ? (
<RowWrapper>{withTimestamp}</RowWrapper>
) : (
withTimestamp
)}
</Fragment>
);
})}
</div>
{isEnrolled && partner && <TagsList partner={partner} />}
{partner && isEnrolled && showApplicationRiskAnalysis && (
<PartnerApplicationRiskSummary partner={partner} />
)}
</div>
</div>
{!isAdmin && (
<div className="border-border-subtle flex flex-col gap-4 rounded-xl border p-4">
{/* Group */}
<div className="flex flex-col gap-2">
{isEnrolled && (
<div className="flex min-h-7 items-center justify-between">
<h3 className="text-content-emphasis text-sm font-semibold">
Group
</h3>
{partner && partner.status !== "pending" && hasActivityLogs && (
<Button
variant="outline"
text="View history"
className="h-7 w-fit rounded-lg px-1.5 text-xs font-medium text-neutral-400"
onClick={() => setGroupHistoryOpen(true)}
/>
)}
</div>
)}
{partnerGroupHistorySheet}
{partner ? (
<PartnerInfoGroup
partner={partner}
changeButtonText="Change"
hideChangeButton={
"status" in partner &&
INACTIVE_ENROLLMENT_STATUSES.includes(partner.status)
}
className="rounded-lg bg-white shadow-sm"
selectedGroupId={selectedGroupId}
setSelectedGroupId={setSelectedGroupId}
/>
) : (
<div className="my-px h-11 w-full animate-pulse rounded-lg bg-neutral-200" />
)}
</div>
{isEnrolled && partner?.status === "approved" && (
<>
{/* Rewards */}
<div className="flex flex-col gap-2">
<h3 className="text-content-emphasis text-sm font-semibold">
Rewards
</h3>
{group ? (
group.clickReward ||
group.leadReward ||
group.saleReward ||
group.discount ? (
<ProgramRewardList
rewards={[
group.clickReward,
group.leadReward,
group.saleReward,
group.referralReward,
].filter((r): r is RewardProps => r !== null)}
discount={group.discount}
variant="plain"
className="text-content-subtle gap-2 text-xs leading-4"
iconClassName="size-3.5"
/>
) : (
<span className="text-content-subtle text-xs">
No rewards
</span>
)
) : (
<div className="h-4 w-32 animate-pulse rounded bg-neutral-200" />
)}
</div>
{/* Eligible bounties */}
<div className="flex flex-col gap-2">
<h3 className="text-content-emphasis text-sm font-semibold">
Eligible Bounties
</h3>
{bounties ? (
bounties.length ? (
<div className="flex flex-col gap-2">
{bounties.map((bounty) => {
const Icon =
bounty.type === "performance" ? Trophy : Heart;
return (
<Link
key={bounty.id}
target="_blank"
href={`/${workspaceSlug}/program/bounties/${bounty.id}`}
className="text-content-subtle flex cursor-alias items-center gap-2 decoration-dotted underline-offset-2 hover:underline"
>
<Icon className="size-3.5 shrink-0" />
<span className="text-xs font-medium">
{bounty.name}
</span>
</Link>
);
})}
</div>
) : (
<p className="text-content-subtle text-xs">
No eligible bounties
</p>
)
) : errorBounties ? (
<p className="text-content-subtle text-xs">
Failed to load bounties
</p>
) : (
<div className="h-4 w-24 animate-pulse rounded bg-neutral-200" />
)}
</div>
</>
)}
</div>
)}
</div>
);
}
function TagsList({ partner }: { partner: EnrolledPartnerExtendedProps }) {
const { showUpdatePartnerTagsModal, setShowUpdatePartnerTagsModal } =
useUpdatePartnerTagsModal();
return (
<div className="border-border-subtle flex flex-col border-t p-4">
<UpdatePartnerTagsModal
showUpdatePartnerTagsModal={showUpdatePartnerTagsModal}
setShowUpdatePartnerTagsModal={setShowUpdatePartnerTagsModal}
partners={[partner]}
/>
<div className="mb-2 flex justify-between gap-2">
<span className="text-content-emphasis block text-xs font-semibold">
Tags
</span>
<button
type="button"
onClick={() => setShowUpdatePartnerTagsModal(true)}
className="text-content-subtle hover:text-content-default text-xs font-medium"
>
Manage
</button>
</div>
<PartnerTagsList
tags={partner?.tags}
wrap
onAddTag={() => setShowUpdatePartnerTagsModal(true)}
mode="link"
/>
</div>
);
}
function ReferredByPartner({
partner,
}: {
partner: Pick<
EnrolledPartnerExtendedProps,
"id" | "name" | "image" | "email" | "groupId" | "totalCommissions"
>;
}) {
const { slug } = useWorkspace();
const { referral, loading, error } = usePartnerReferral({
partnerId: partner?.id,
});
const {
AttributeReferringPartnerModal,
setShowAttributeReferringPartnerModal,
} = useAttributeReferringPartnerModal({ partner });
if (error) {
return null;
}
if (loading) {
return (
<span className="flex min-w-0 items-center gap-1">
<div className="h-4 w-12 animate-pulse rounded bg-neutral-200" />
<div className="size-4 animate-pulse rounded-full bg-neutral-200" />
<div className="h-4 w-12 animate-pulse rounded bg-neutral-200" />
</span>
);
}
// Has a referring partner
if (referral && referral.referredBy) {
return (
<span className="flex min-w-0 items-center gap-1">
Referred by
<Link
href={`/${slug}/program/partners/${referral.referredBy.id}`}
className="inline-flex min-w-0 max-w-full cursor-alias items-center gap-1 rounded decoration-dotted underline-offset-2 hover:underline"
>
<PartnerAvatar partner={referral.referredBy} className="size-3.5" />
<span className="truncate">{referral.referredBy.name}</span>
</Link>
</span>
);
}
return (
<>
<AttributeReferringPartnerModal />
<button
type="button"
onClick={() => setShowAttributeReferringPartnerModal(true)}
aria-label="Attribute referring partner"
className="bg-bg-inverted/5 text-content-default hover:bg-bg-inverted/10 -my-0.5 inline-flex h-5 min-w-0 select-none items-center whitespace-nowrap rounded-md px-1.5 py-0.5 text-xs font-medium transition-all"
>
Attribute referring partner
</button>
</>
);
}