The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import useWorkspace from "@/lib/swr/use-workspace";
import { CustomerActivityResponse, CustomerEnriched } from "@/lib/types";
import {
Button,
CalendarIcon,
CopyText,
Envelope,
Globe,
Hyperlink,
MoneyBill2,
TimestampTooltip,
Tooltip,
UTM_PARAMETERS,
} from "@dub/ui";
import {
capitalize,
cn,
COUNTRIES,
currencyFormatter,
getParamsFromURL,
getPrettyUrl,
} from "@dub/utils";
import { Pencil } from "lucide-react";
import { Fragment, HTMLProps, useMemo } from "react";
import { DeviceIcon } from "../analytics/device-icon";
import { useEditCustomerModal } from "../modals/edit-customer-modal";
import { ConditionalLink } from "../shared/conditional-link";
import { CustomerAvatar } from "./customer-avatar";
export function CustomerDetailsColumn({
customer,
customerActivity,
isCustomerActivityLoading,
}: {
customer?: CustomerEnriched;
customerActivity?: CustomerActivityResponse;
isCustomerActivityLoading: boolean;
}) {
const { slug: workspaceSlug } = useWorkspace();
const { EditCustomerModal, openEditCustomerModal } = useEditCustomerModal();
const basicFields = [
customer?.email
? {
id: "email",
icon: <Envelope className="size-3.5 shrink-0" />,
text: (
<CopyText
value={customer.email}
className="min-w-0 truncate text-xs font-medium"
>
{customer.email}
</CopyText>
),
}
: null,
{
id: "country",
icon: customer?.country ? (
<img
alt={`Flag of ${COUNTRIES[customer.country]}`}
src={`https://flag.vercel.app/m/${customer.country}.svg`}
className="size-3.5 rounded-full"
/>
) : (
<Globe className="size-3.5 shrink-0" />
),
text: customer?.country ? COUNTRIES[customer.country] : "Planet Earth",
},
customer?.createdAt
? {
id: "since",
icon: <CalendarIcon className="size-3.5 shrink-0" />,
text: (
<span>
Since{" "}
<TimestampTooltip
timestamp={customer.createdAt}
rows={["local", "utc", "unix"]}
side="left"
>
<span>
{new Date(customer.createdAt).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
})}
</span>
</TimestampTooltip>
</span>
),
}
: null,
customer?.saleAmount != null
? {
id: "ltv",
icon: <MoneyBill2 className="size-3.5 shrink-0" />,
text: (
<span>
LTV:{" "}
{currencyFormatter(customer.saleAmount, {
trailingZeroDisplay: "stripIfInteger",
})}
</span>
),
}
: null,
].filter((field): field is NonNullable<typeof field> => field !== null);
const link = customerActivity?.link;
const click = customerActivity?.events.find((e) => e.event === "click");
const utmParams = useMemo(() => {
if (!click?.url) return null;
const allParams = getParamsFromURL(click.url);
return UTM_PARAMETERS.map((p) => ({
...p,
value: allParams?.[p.key],
})).filter(({ value }) => value);
}, [click?.url]);
return (
<>
<EditCustomerModal />
<div className="grid grid-cols-1 gap-6 overflow-hidden whitespace-nowrap text-sm text-neutral-900">
<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">
{customer ? (
<CustomerAvatar
customer={customer}
className="size-10 border border-neutral-100"
/>
) : (
<div className="size-10 animate-pulse rounded-full bg-neutral-200" />
)}
</div>
{customer && workspaceSlug && (
<Button
variant="secondary"
icon={<Pencil className="size-3.5" />}
text="Edit"
className="h-7 w-fit rounded-lg px-2"
onClick={() => openEditCustomerModal(customer)}
/>
)}
</div>
<div className="mt-3">
{customer ? (
<div className="flex items-center gap-2">
<span className="text-content-emphasis text-base font-semibold">
{customer.name || customer.email}
</span>
</div>
) : (
<div className="h-7 w-24 animate-pulse rounded bg-neutral-200" />
)}
</div>
</div>
<div className="flex flex-col gap-2 p-4">
{basicFields.map(({ id, icon, text }) => (
<div key={id}>
<div className="text-content-default flex items-center gap-1.5">
{text !== undefined ? (
<>
{icon}
<span className="min-w-0 truncate text-xs font-medium">
{text}
</span>
</>
) : (
<div className="h-4 w-24 animate-pulse rounded bg-neutral-200" />
)}
</div>
</div>
))}
</div>
<div className="@md/page:grid-cols-2 @3xl/page:grid-cols-1 grid grid-cols-1 gap-5 p-4 text-xs">
<div>
<h2 className="text-content-emphasis text-sm font-semibold">
Details
</h2>
<div className="mt-2.5 flex flex-col gap-5 text-xs">
<div className="flex flex-col gap-2">
{click
? [
{
key: "device",
icon: (
<DeviceIcon
display={capitalize(click.device)!}
tab="devices"
className="size-3.5 shrink-0"
/>
),
value: click.device,
},
{
key: "browser",
icon: (
<DeviceIcon
display={capitalize(click.browser)!}
tab="browsers"
className="size-3.5 shrink-0"
/>
),
value: click.browser,
},
{
key: "os",
icon: (
<DeviceIcon
display={capitalize(click.os)!}
tab="os"
className="size-3.5 shrink-0"
/>
),
value: click.os,
},
]
.filter(({ value }) => value)
.map(({ key, icon, value }) => (
<ConditionalLink
key={key}
href={
value === "Unknown"
? undefined
: `/${workspaceSlug}/links/analytics?${key}=${encodeURIComponent(value)}`
}
target="_blank"
>
<span className="flex items-center gap-2">
{icon}
<span className="truncate">{value}</span>
</span>
</ConditionalLink>
))
: (isCustomerActivityLoading || !customer) && (
<div className="h-5 w-12 animate-pulse rounded-md bg-neutral-100" />
)}
</div>
{customer?.externalId && (
<div className="flex flex-col gap-2.5">
<DetailHeading>External ID</DetailHeading>
<div>
<CopyText
value={customer.externalId}
className="truncate text-xs"
>
{customer.externalId}
</CopyText>
</div>
</div>
)}
{customer?.stripeCustomerId && (
<div className="flex flex-col gap-2.5">
<DetailHeading>Stripe Customer ID</DetailHeading>
<div>
<Tooltip content="View in Stripe" align="start">
<div>
<ConditionalLink
href={`https://dashboard.stripe.com/customers/${customer.stripeCustomerId}`}
target="_blank"
rel="noopener noreferrer"
className="truncate text-xs"
>
{customer.stripeCustomerId}
</ConditionalLink>
</div>
</Tooltip>
</div>
</div>
)}
</div>
</div>
{utmParams && Boolean(utmParams.length) && (
<div className="flex flex-col gap-5 text-xs">
<div className="flex flex-col gap-2.5">
<DetailHeading>UTM</DetailHeading>
<div className="grid w-full grid-cols-[min-content,minmax(0,1fr)] gap-x-4 gap-y-2 overflow-hidden">
{utmParams.map(({ key, label, value }) => (
<Fragment key={key}>
<span className="truncate">{label}</span>
<ConditionalLink
href={`/${workspaceSlug}/links/analytics?${key}=${encodeURIComponent(value)}`}
target="_blank"
className="truncate text-neutral-500"
>
{value}
</ConditionalLink>
</Fragment>
))}
</div>
</div>
</div>
)}
</div>
</div>
<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}/links/${link.domain}/${link.key}`
: undefined
}
target="_blank"
className="min-w-0 overflow-hidden truncate"
>
{getPrettyUrl(link.shortLink)}
</ConditionalLink>
</div>
) : (
<span>-</span>
)}
</div>
</div>
</div>
</>
);
}
const DetailHeading = ({
className,
...rest
}: HTMLProps<HTMLHeadingElement>) => (
<h3
className={cn("text-content-emphasis text-xs font-semibold", className)}
{...rest}
/>
);