The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import useCustomers from "@/lib/swr/use-customers";
import { Combobox, ComboboxProps } from "@dub/ui";
import { cn } from "@dub/utils";
import { useMemo, useState } from "react";
import { useDebounce } from "use-debounce";
import {
AddCustomerModal,
type AddCustomerInitialData,
} from "../modals/add-customer-modal";
import { CustomerAvatar } from "./customer-avatar";
type CustomerSelectorProps = {
selectedCustomerId: string | null;
setSelectedCustomerId: (customerId: string) => void;
disabled?: boolean;
variant?: "default" | "header";
} & Partial<ComboboxProps<false, any>>;
export function CustomerSelector({
selectedCustomerId,
setSelectedCustomerId,
disabled,
variant = "default",
...rest
}: CustomerSelectorProps) {
const [search, setSearch] = useState("");
const [debouncedSearch] = useDebounce(search, 500);
const [openPopover, setOpenPopover] = useState(false);
const { customers, loading } = useCustomers({
query: { search: debouncedSearch },
});
const { customers: selectedCustomers, loading: selectedCustomersLoading } =
useCustomers({
query: selectedCustomerId
? { customerIds: [selectedCustomerId] }
: undefined,
});
const [showAddCustomerModal, setShowAddCustomerModal] = useState(false);
const [initialData, setInitialData] = useState<
AddCustomerInitialData | undefined
>();
const customerOptions = useMemo(() => {
return (
customers?.map((customer) => ({
value: customer.id,
label: customer.name || customer.email || customer.externalId,
icon: (
<span className="shrink-0 text-neutral-600">
<CustomerAvatar customer={customer} className="size-4" />
</span>
),
})) || []
);
}, [customers]);
const selectedOption = useMemo(() => {
if (!selectedCustomerId) return null;
const customer = [...(customers || []), ...(selectedCustomers || [])].find(
(c) => c.id === selectedCustomerId,
);
if (!customer) return null;
return {
value: customer.email || customer.id,
label: customer.name || customer.email || customer.externalId,
icon: (
<span className="shrink-0 text-neutral-600">
<CustomerAvatar customer={customer} className="size-4" />
</span>
),
};
}, [customers, selectedCustomers, selectedCustomerId]);
return (
<>
<AddCustomerModal
showModal={showAddCustomerModal}
setShowModal={setShowAddCustomerModal}
initialData={initialData}
onSuccess={(customer) => {
setSelectedCustomerId(customer.id);
}}
/>
<Combobox
options={loading ? undefined : customerOptions}
setSelected={(option) => {
if (!option) return;
setSelectedCustomerId(option.value);
}}
selected={selectedOption}
icon={
variant === "header" && !selectedOption?.icon ? (
<div className="size-5 flex-none animate-pulse rounded-full bg-neutral-200" />
) : (
selectedOption?.icon
)
}
caret={true}
placeholder={variant === "header" ? "" : "Select customer"}
searchPlaceholder="Search by email, name, or ID"
onSearchChange={setSearch}
{...(variant !== "header" && {
createLabel: (search: string) =>
`Create ${search ? `"${search}"` : "new customer"}`,
onCreate: async (search: string | undefined) => {
const trimmed = search?.trim() ?? "";
setInitialData(
trimmed
? trimmed.includes("@")
? { email: trimmed }
: { name: trimmed }
: undefined,
);
setShowAddCustomerModal(true);
return true;
},
})}
shouldFilter={false}
matchTriggerWidth
open={openPopover}
onOpenChange={setOpenPopover}
{...(variant === "header"
? {
popoverProps: {
contentClassName: "min-w-[280px]",
},
labelProps: {
className: "text-lg font-semibold leading-7 text-neutral-900",
},
iconProps: {
className: "size-6",
},
buttonProps: {
disabled,
className:
"w-full justify-start px-2 py-1 h-8 transition-none max-md:bg-bg-subtle hover:bg-bg-subtle md:hover:bg-subtle border-none rounded-lg",
},
}
: {
buttonProps: {
disabled,
className: cn(
"w-full justify-start border-neutral-300 px-3",
"data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",
"focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",
),
},
})}
{...rest}
>
{variant === "header" && !selectedOption?.label ? (
<div className="h-6 w-[120px] animate-pulse rounded bg-neutral-100" />
) : selectedCustomersLoading ? (
<div className="my-0.5 h-5 w-1/3 animate-pulse rounded bg-neutral-200" />
) : (
selectedOption?.label
)}
</Combobox>
</>
);
}