Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

improve UX, simplify CustomerDetailsColumn

Steven Tey9bcdff02789e
diff --git a/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts b/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts
index 780820f25c..eac163f77b 100644
--- a/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts
+++ b/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts
@@ -54,15 +54,14 @@ export const POST = withWorkspace(
if (isReattributedCustomerStub(customer)) {
throw new DubApiError({
code: "bad_request",
- message:
- "This customer was already reattributed. Use the new customer ID.",
+ message: `Customer "${customer.id}" was already reattributed. Use the new customer ID instead.`,
});
}
- if (customer.partnerId === partnerId && customer.linkId === linkId) {
+ if (customer.partnerId === partnerId) {
throw new DubApiError({
code: "bad_request",
- message: "Customer is already attributed to this partner and link.",
+ message: `Customer "${customer.id}" is already attributed to the partner "${partnerId}".`,
});
}
@@ -129,20 +128,19 @@ export const POST = withWorkspace(
linkId: link.id,
programId: link.programId,
},
- });
-
- const enrichedCustomer = await getCustomerOrThrow(
- {
- id: updatedCustomer.id,
- workspaceId: workspace.id,
- },
- {
- includeExpandedFields: true,
+ include: {
+ link: true,
+ programEnrollment: {
+ include: {
+ partner: true,
+ discount: true,
+ },
+ },
},
- );
+ });
return NextResponse.json(
- CustomerEnrichedSchema.parse(transformCustomer(enrichedCustomer)),
+ CustomerEnrichedSchema.parse(transformCustomer(updatedCustomer)),
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/links/customers/[customerId]/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/links/customers/[customerId]/layout.tsx
index f1a9b51d06..6222f01b4a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/links/customers/[customerId]/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/links/customers/[customerId]/layout.tsx
@@ -83,7 +83,6 @@ export default function CustomerLayout({ children }: { children: ReactNode }) {
customer={customer}
customerActivity={customerActivity}
isCustomerActivityLoading={!customer || isCustomerActivityLoading}
- workspaceSlug={workspaceSlug}
/>
</div>
<div className="@3xl/page:order-1">
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/customers/[customerId]/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/customers/[customerId]/layout.tsx
index 4d78a8c80e..4e5d47aa4a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/customers/[customerId]/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/customers/[customerId]/layout.tsx
@@ -83,7 +83,6 @@ export default function ProgramCustomerLayout({
customer={customer}
customerActivity={customerActivity}
isCustomerActivityLoading={!customer || isCustomerActivityLoading}
- workspaceSlug={workspaceSlug}
/>
</div>
<div className="@3xl/page:order-1">
diff --git a/apps/web/lib/api/customers/reattribute-customer.ts b/apps/web/lib/api/customers/reattribute-customer.ts
index 699dadcb37..937420f3c0 100644
--- a/apps/web/lib/api/customers/reattribute-customer.ts
+++ b/apps/web/lib/api/customers/reattribute-customer.ts
@@ -136,6 +136,9 @@ export async function recreateCustomerForReattribution({
programId: null,
partnerId: null,
clickId: null,
+ saleAmount: 0,
+ firstSaleAt: null,
+ subscriptionCanceledAt: null,
},
});
diff --git a/apps/web/ui/customers/customer-details-column.tsx b/apps/web/ui/customers/customer-details-column.tsx
index 2bc55c4790..64bddf75bb 100644
--- a/apps/web/ui/customers/customer-details-column.tsx
+++ b/apps/web/ui/customers/customer-details-column.tsx
@@ -1,3 +1,4 @@
+import useWorkspace from "@/lib/swr/use-workspace";
import { CustomerActivityResponse, CustomerEnriched } from "@/lib/types";
import { PartnerAvatar } from "@/ui/partners/partner-avatar";
import {
@@ -8,6 +9,7 @@ import {
Globe,
Hyperlink,
MoneyBill2,
+ Shuffle,
TimestampTooltip,
Tooltip,
useCurrentProduct,
@@ -23,7 +25,7 @@ import {
} from "@dub/utils";
import { Pencil } from "lucide-react";
import { useParams, usePathname, useRouter } from "next/navigation";
-import { Fragment, HTMLProps, useMemo } from "react";
+import { Fragment, HTMLProps, useCallback, useMemo } from "react";
import { DeviceIcon } from "../analytics/device-icon";
import { useEditCustomerModal } from "../modals/edit-customer-modal";
import { useReattributeCustomerModal } from "../modals/reattribute-customer-modal";
@@ -34,15 +36,14 @@ export function CustomerDetailsColumn({
customer,
customerActivity,
isCustomerActivityLoading,
- workspaceSlug,
}: {
customer?: CustomerEnriched;
customerActivity?: CustomerActivityResponse;
isCustomerActivityLoading: boolean;
- workspaceSlug?: string;
}) {
- const { programSlug } = useParams<{ programSlug: string }>();
const { product } = useCurrentProduct();
+ const { programSlug } = useParams<{ programSlug: string }>();
+ const { slug: workspaceSlug, defaultProgramId } = useWorkspace();
const pathname = usePathname();
const router = useRouter();
@@ -50,6 +51,21 @@ export function CustomerDetailsColumn({
const { ReattributeCustomerModal, openReattributeCustomerModal } =
useReattributeCustomerModal();
+ const handleReattributeCustomer = useCallback(() => {
+ if (customer) {
+ openReattributeCustomerModal(customer, {
+ onSuccess: (updatedCustomer) => {
+ if (
+ updatedCustomer.id !== customer.id &&
+ pathname.includes(customer.id)
+ ) {
+ router.push(pathname.replace(customer.id, updatedCustomer.id));
+ }
+ },
+ });
+ }
+ }, [customer, openReattributeCustomerModal, pathname, router]);
+
const basicFields = [
customer?.email
? {
@@ -135,8 +151,6 @@ export function CustomerDetailsColumn({
})).filter(({ value }) => value);
}, [click?.url]);
- const showReferralCard = Boolean(workspaceSlug || link || !customer);
-
return (
<>
<EditCustomerModal />
@@ -325,86 +339,75 @@ export function CustomerDetailsColumn({
</div>
</div>
- {showReferralCard && (
- <div className="border-border-subtle rounded-lg border p-4">
- <div className="mb-2.5 flex items-center justify-between gap-2">
- <h2 className="text-content-emphasis text-sm font-semibold">
- Referral {partner || !link ? "partner" : "link"}
- </h2>
- {customer && workspaceSlug && (
- <Button
- variant="secondary"
- icon={<Pencil className="size-3.5" />}
- text={partner ? "Edit" : "Assign"}
- className="h-7 w-fit rounded-lg px-2"
- onClick={() =>
- openReattributeCustomerModal(customer, {
- onSuccess: (updatedCustomer) => {
- if (
- updatedCustomer.id !== customer.id &&
- pathname.includes(customer.id)
- ) {
- router.push(
- pathname.replace(customer.id, updatedCustomer.id),
- );
- }
- },
- })
- }
- />
- )}
- </div>
-
- {partner && (
- <div className="mb-4 flex items-center gap-2">
- <PartnerAvatar partner={partner} className="size-5" />
+ <div className="border-border-subtle rounded-lg border p-4">
+ <div className="flex flex-col gap-2">
+ <h2 className="text-content-emphasis text-sm font-semibold">
+ Referral link
+ </h2>
+ {!customer || isCustomerActivityLoading ? (
+ <div className="h-5 w-12 animate-pulse rounded-md bg-neutral-100" />
+ ) : link ? (
+ <div className="flex items-center gap-1.5 text-xs">
+ <Hyperlink className="size-3.5 shrink-0" />
<ConditionalLink
href={
workspaceSlug
- ? `/${workspaceSlug}/program/partners/${partner.id}`
- : undefined
+ ? `/${workspaceSlug}/links/${link.domain}/${link.key}`
+ : programSlug
+ ? `/programs/${programSlug}/analytics?linkId=${link.id}`
+ : undefined
}
target="_blank"
- className="min-w-0 overflow-hidden truncate text-xs font-semibold"
+ className="min-w-0 overflow-hidden truncate"
>
- {partner.name}
+ {getPrettyUrl(link.shortLink)}
</ConditionalLink>
</div>
+ ) : (
+ <span>-</span>
)}
+ </div>
- {!partner && customer && workspaceSlug && (
- <p className="mb-4 text-xs text-neutral-500">
- No partner assigned
- </p>
- )}
+ {customer && workspaceSlug && defaultProgramId && (
+ <div className="mt-4 flex flex-col gap-2">
+ <h2 className="text-content-emphasis text-sm font-semibold">
+ Referral partner
+ </h2>
- <div className="flex flex-col gap-2 text-xs">
- {partner && <DetailHeading>Referral link</DetailHeading>}
- {!customer || isCustomerActivityLoading ? (
- <div className="h-5 w-12 animate-pulse rounded-md bg-neutral-100" />
- ) : link ? (
- <div className="flex items-center gap-1.5">
- <Hyperlink className="size-3.5 shrink-0" />
- <ConditionalLink
- href={
- workspaceSlug
- ? `/${workspaceSlug}/links/${link.domain}/${link.key}`
- : programSlug
- ? `/programs/${programSlug}/analytics?linkId=${link.id}`
- : undefined
- }
- target="_blank"
- className="min-w-0 overflow-hidden truncate"
+ {partner ? (
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <PartnerAvatar partner={partner} className="size-5" />
+ <ConditionalLink
+ href={`/${workspaceSlug}/program/partners/${partner.id}`}
+ target="_blank"
+ className="min-w-0 overflow-hidden truncate text-xs font-semibold"
+ >
+ {partner.name}
+ </ConditionalLink>
+ </div>
+ <Button
+ variant="secondary"
+ icon={<Shuffle className="size-3.5" />}
+ text="Change"
+ className="h-7 w-fit rounded-lg px-2"
+ onClick={handleReattributeCustomer}
+ />
+ </div>
+ ) : (
+ <div className="text-xs text-neutral-500">
+ Not attributed to a partner.{" "}
+ <button
+ className="underline decoration-dotted underline-offset-2 hover:text-neutral-700"
+ onClick={handleReattributeCustomer}
>
- {getPrettyUrl(link.shortLink)}
- </ConditionalLink>
+ Attribute now.
+ </button>
</div>
- ) : customer && workspaceSlug ? null : (
- <span>-</span>
)}
</div>
- </div>
- )}
+ )}
+ </div>
</div>
</>
);
diff --git a/apps/web/ui/modals/reattribute-customer-modal.tsx b/apps/web/ui/modals/reattribute-customer-modal.tsx
index dca0236963..d4ad479f7c 100644
--- a/apps/web/ui/modals/reattribute-customer-modal.tsx
+++ b/apps/web/ui/modals/reattribute-customer-modal.tsx
@@ -6,7 +6,7 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { CommissionsCount, CustomerEnriched, CustomerProps } from "@/lib/types";
import { PartnerLinkSelector } from "@/ui/partners/partner-link-selector";
import { PartnerSelector } from "@/ui/partners/partner-selector";
-import { Button, Checkbox, Modal } from "@dub/ui";
+import { AnimatedSizeContainer, Button, Checkbox, Modal } from "@dub/ui";
import { currencyFormatter, fetcher } from "@dub/utils";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useForm } from "react-hook-form";
@@ -34,6 +34,7 @@ function ReattributeCustomerModal({
}: ReattributeCustomerModalProps) {
const { id: workspaceId } = useWorkspace();
const { makeRequest, isSubmitting } = useApiMutation<CustomerProps>();
+ const [verification, setVerification] = useState("");
const {
watch,
@@ -62,6 +63,7 @@ function ReattributeCustomerModal({
linkId: customer.link?.id ?? null,
createClawback: false,
});
+ setVerification("");
}
}, [showModal, customer, reset]);
@@ -83,12 +85,14 @@ function ReattributeCustomerModal({
const paidEarnings = commissionsCount?.paid?.earnings ?? 0;
const showClawback = !commissionsCountError && paidEarnings > 0;
- const isUnchanged =
- partnerId === (customer.partner?.id ?? null) &&
- linkId === (customer.link?.id ?? null);
+ const currentPartnerId = customer.partner?.id ?? null;
+ const isPartnerDifferent =
+ Boolean(partnerId) && partnerId !== currentPartnerId;
+ const confirmationText = "confirm attribute customer";
+ const isVerified = verification === confirmationText;
const onSubmit = async (data: FormData) => {
- if (!data.partnerId || !data.linkId) {
+ if (!isPartnerDifferent || !data.partnerId || !data.linkId || !isVerified) {
return;
}
@@ -112,118 +116,162 @@ function ReattributeCustomerModal({
};
return (
- <Modal
- showModal={showModal}
- setShowModal={setShowModal}
- className="sm:max-w-md"
- >
- <form
- className="px-5 py-4"
- onSubmit={(e) => {
- e.stopPropagation();
- handleSubmit(onSubmit)(e);
- }}
- >
- <h3 className="text-lg font-medium">Assign to partner</h3>
- <p className="mt-1 text-sm text-neutral-500">
- Move this customer to a different partner. Unpaid commissions will
- transfer automatically.
- </p>
+ <Modal showModal={showModal} setShowModal={setShowModal}>
+ <div className="flex items-center justify-between gap-2 border-b border-neutral-200 px-4 py-4 sm:px-6">
+ <h3 className="text-lg font-medium">Attribute to partner</h3>
+ </div>
+
+ <div className="bg-neutral-50">
+ <form
+ onSubmit={(e) => {
+ e.stopPropagation();
+ return handleSubmit(onSubmit)(e);
+ }}
+ >
+ <AnimatedSizeContainer
+ height
+ className="flex flex-col"
+ transition={{ type: "spring", stiffness: 400, damping: 35 }}
+ >
+ <div className="flex flex-col gap-6 px-4 py-6 text-left sm:px-6">
+ <div>
+ <label className="text-sm font-medium text-neutral-600">
+ Partner
+ </label>
+ <div className="mt-1.5">
+ <PartnerSelector
+ selectedPartnerId={partnerId}
+ setSelectedPartnerId={(id) => {
+ setValue("partnerId", id, { shouldDirty: true });
+ setValue(
+ "linkId",
+ id === currentPartnerId
+ ? customer.link?.id ?? null
+ : null,
+ { shouldDirty: true },
+ );
+ }}
+ />
+ </div>
+ {currentPartnerId && (
+ <p className="mt-1.5 text-xs text-neutral-500">
+ Attribute this customer to a different partner. Unpaid
+ commissions will be transferred to the new partner as well.
+ </p>
+ )}
+ </div>
+
+ {isPartnerDifferent && (
+ <>
+ <div>
+ <label className="text-sm font-medium text-neutral-600">
+ Referral link
+ </label>
+ <div className="mt-1.5">
+ <PartnerLinkSelector
+ selectedLinkId={linkId}
+ partnerId={partnerId}
+ showDestinationUrl={false}
+ setSelectedLinkId={(id) =>
+ setValue("linkId", id, { shouldDirty: true })
+ }
+ />
+ </div>
+ </div>
- <div className="mt-6 flex flex-col gap-2">
- <span className="block text-sm font-medium text-neutral-900">
- Partner
- </span>
- <PartnerSelector
- selectedPartnerId={partnerId}
- setSelectedPartnerId={(id) => {
- setValue("partnerId", id, { shouldDirty: true });
- setValue("linkId", null, { shouldDirty: true });
- }}
- />
- </div>
+ <div>
+ <label
+ htmlFor="verification"
+ className="text-sm font-medium text-neutral-600"
+ >
+ To verify, type{" "}
+ <span className="font-semibold text-neutral-800">
+ {confirmationText}
+ </span>{" "}
+ below
+ </label>
+ <input
+ type="text"
+ name="verification"
+ id="verification"
+ pattern={confirmationText}
+ required
+ autoComplete="off"
+ placeholder={confirmationText}
+ value={verification}
+ onChange={(e) => setVerification(e.target.value)}
+ className="mt-1.5 block w-full rounded-lg border border-neutral-200 bg-white px-3 py-2.5 text-sm text-neutral-900 placeholder-neutral-400 shadow-sm transition-colors focus:border-neutral-400 focus:outline-none focus:ring-2 focus:ring-neutral-200"
+ />
+ </div>
- <div className="mt-4 flex flex-col gap-2">
- <span className="block text-sm font-medium text-neutral-900">
- Referral link
- </span>
- <PartnerLinkSelector
- selectedLinkId={linkId}
- partnerId={partnerId}
- showDestinationUrl={false}
- setSelectedLinkId={(id) =>
- setValue("linkId", id, { shouldDirty: true })
- }
- disabledTooltip={
- !partnerId
- ? "You need to select a partner first before you can select a link"
- : undefined
- }
- />
- </div>
+ {showClawback && (
+ <label className="flex items-start gap-2.5 rounded-lg border border-neutral-200 bg-white p-4 shadow-sm">
+ <Checkbox
+ checked={createClawback}
+ onCheckedChange={(checked) =>
+ setValue("createClawback", checked === true, {
+ shouldDirty: true,
+ })
+ }
+ className="mt-0.5 data-[state=checked]:bg-black"
+ />
+ <span className="text-sm text-neutral-700">
+ Create a clawback for the{" "}
+ <span className="font-medium text-neutral-900">
+ {currencyFormatter(paidEarnings)}
+ </span>{" "}
+ in commissions that were already paid to the previous
+ partner.
+ </span>
+ </label>
+ )}
+ </>
+ )}
- {commissionsCountError && (
- <div className="mt-5 flex items-center justify-between gap-3 rounded-lg border border-neutral-200 bg-neutral-50 p-3">
- <p className="text-sm text-neutral-700">
- Failed to load paid commissions. Retry before saving.
- </p>
+ {commissionsCountError && (
+ <div className="flex items-center justify-between gap-3 rounded-lg border border-neutral-200 bg-white p-4 shadow-sm">
+ <p className="text-sm text-neutral-700">
+ Failed to load paid commissions. Retry before saving.
+ </p>
+ <Button
+ type="button"
+ variant="secondary"
+ text="Retry"
+ className="h-8 w-fit shrink-0"
+ loading={isCommissionsCountValidating}
+ onClick={() => retryCommissionsCount()}
+ />
+ </div>
+ )}
+ </div>
+ </AnimatedSizeContainer>
+
+ <div className="flex items-center justify-end gap-2 border-t border-neutral-200 px-4 py-4 sm:px-6">
<Button
type="button"
variant="secondary"
- text="Retry"
- className="h-8 w-fit shrink-0"
- loading={isCommissionsCountValidating}
- onClick={() => retryCommissionsCount()}
+ text="Cancel"
+ className="h-9 w-fit"
+ onClick={() => setShowModal(false)}
+ disabled={isSubmitting}
/>
- </div>
- )}
-
- {showClawback && (
- <label className="mt-5 flex items-start gap-2.5 rounded-lg border border-neutral-200 bg-neutral-50 p-3">
- <Checkbox
- checked={createClawback}
- onCheckedChange={(checked) =>
- setValue("createClawback", checked === true, {
- shouldDirty: true,
- })
+ <Button
+ type="submit"
+ variant="primary"
+ text="Save"
+ className="h-9 w-fit"
+ loading={isSubmitting}
+ disabled={
+ !isDirty ||
+ !isPartnerDifferent ||
+ !linkId ||
+ Boolean(commissionsCountError) ||
+ !isVerified
}
- className="mt-0.5"
/>
- <span className="text-sm text-neutral-700">
- Create a clawback for{" "}
- <span className="font-medium text-neutral-900">
- {currencyFormatter(paidEarnings)}
- </span>{" "}
- already paid to the previous partner.
- </span>
- </label>
- )}
-
- <div className="mt-6 flex items-center justify-end gap-2">
- <Button
- type="button"
- variant="secondary"
- text="Cancel"
- className="h-9 w-fit"
- onClick={() => setShowModal(false)}
- disabled={isSubmitting}
- />
- <Button
- type="submit"
- variant="primary"
- text="Save"
- className="h-9 w-fit"
- loading={isSubmitting}
- disabled={
- !isDirty ||
- isUnchanged ||
- !partnerId ||
- !linkId ||
- Boolean(commissionsCountError)
- }
- />
- </div>
- </form>
+ </div>
+ </form>
+ </div>
</Modal>
);
}