The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
smart initialData prefilling in AddCustomerModal
Steven Tey34deef8b0a70
diff --git a/apps/web/ui/customers/customer-selector.tsx b/apps/web/ui/customers/customer-selector.tsxindex aa7b305851..d12b5da19d 100644--- a/apps/web/ui/customers/customer-selector.tsx+++ b/apps/web/ui/customers/customer-selector.tsx@@ -4,7 +4,10 @@ import { Combobox } from "@dub/ui";import { cn, OG_AVATAR_URL } from "@dub/utils";import { useEffect, useMemo, useState } from "react";import { useDebounce } from "use-debounce";-import { AddCustomerModal } from "../modals/add-customer-modal";+import {+ AddCustomerModal,+ type AddCustomerInitialData,+} from "../modals/add-customer-modal";interface CustomerSelectorProps {selectedCustomerId: string;@@ -38,7 +41,9 @@ export function CustomerSelector({}, [customers, useAsync]);const [showAddCustomerModal, setShowAddCustomerModal] = useState(false);- const [newCustomerName, setNewCustomerName] = useState<string | undefined>();+ const [initialData, setInitialData] = useState<+ AddCustomerInitialData | undefined+ >();const customerOptions = useMemo(() => {return (@@ -101,7 +106,7 @@ export function CustomerSelector({<AddCustomerModalshowModal={showAddCustomerModal}setShowModal={setShowAddCustomerModal}- initialName={newCustomerName}+ initialData={initialData}onSuccess={(customer) => {setSelectedCustomerId(customer.id);}}@@ -121,7 +126,14 @@ export function CustomerSelector({`Create ${search ? `"${search}"` : "new customer"}`}onCreate={async (search) => {- setNewCustomerName(search);+ const trimmed = search?.trim() ?? "";+ setInitialData(+ trimmed+ ? trimmed.includes("@")+ ? { email: trimmed }+ : { name: trimmed }+ : undefined,+ );setShowAddCustomerModal(true);return true;}}diff --git a/apps/web/ui/modals/add-customer-modal.tsx b/apps/web/ui/modals/add-customer-modal.tsxindex 9ae459af7e..2aa04d1ca1 100644--- a/apps/web/ui/modals/add-customer-modal.tsx+++ b/apps/web/ui/modals/add-customer-modal.tsx@@ -22,11 +22,13 @@ import { toast } from "sonner";import { mutate } from "swr";import * as z from "zod/v4";+export type AddCustomerInitialData = { name?: string; email?: string };+interface AddCustomerModalProps {showModal: boolean;setShowModal: (showModal: boolean) => void;onSuccess?: (customer: CustomerProps) => void;- initialName?: string;+ initialData?: AddCustomerInitialData;}type FormData = z.infer<typeof createCustomerBodySchema>;@@ -48,7 +50,7 @@ export const AddCustomerModal = ({showModal,setShowModal,onSuccess,- initialName,+ initialData,}: AddCustomerModalProps) => {const { id: workspaceId } = useWorkspace();const { isMobile } = useMediaQuery();@@ -93,15 +95,15 @@ export const AddCustomerModal = ({setStripeSearchResults(null);setStripeSearchError(null);reset({- name: initialName || null,- email: null,+ name: initialData?.name ?? null,+ email: initialData?.email ?? null,externalId: "",stripeCustomerId: null,country: "US",});}prevShowModal.current = showModal;- }, [showModal, initialName, reset]);+ }, [showModal, initialData, reset]);useEffect(() => {if (!hasStripeCustomerId) {@@ -359,7 +361,7 @@ export const AddCustomerModal = ({autoComplete="off"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"placeholder="John Doe"- autoFocus={!isMobile}+ autoFocus={!isMobile && !initialData?.email}{...register("name", {setValueAs: (value) => (value === "" ? null : value),})}@@ -375,6 +377,7 @@ export const AddCustomerModal = ({autoComplete="off"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"placeholder="[email protected]"+ autoFocus={!isMobile && !!initialData?.email}{...register("email", {setValueAs: (value) => (value === "" ? null : value),})}@@ -496,7 +499,9 @@ export function useAddCustomerModal({onSuccess?: (customer: CustomerProps) => void;} = {}) {const [showAddCustomerModal, setShowAddCustomerModal] = useState(false);- const [initialName, setInitialName] = useState<string | undefined>();+ const [initialData, setInitialData] = useState<+ AddCustomerInitialData | undefined+ >();const AddCustomerModalCallback = useCallback(() => {return (@@ -505,28 +510,28 @@ export function useAddCustomerModal({setShowModal={(show) => {setShowAddCustomerModal(show);if (!show) {- setInitialName(undefined);+ setInitialData(undefined);}}}onSuccess={onSuccess}- initialName={initialName}+ initialData={initialData}/>);- }, [showAddCustomerModal, initialName, onSuccess]);+ }, [showAddCustomerModal, initialData, onSuccess]);- const setShowAddCustomerModalWithName = useCallback(- (show: boolean, name?: string) => {+ const setShowAddCustomerModalWithData = useCallback(+ (show: boolean, data?: AddCustomerInitialData) => {setShowAddCustomerModal(show);- setInitialName(name);+ setInitialData(data);},[],);return useMemo(() => ({- setShowAddCustomerModal: setShowAddCustomerModalWithName,+ setShowAddCustomerModal: setShowAddCustomerModalWithData,AddCustomerModal: AddCustomerModalCallback,}),- [setShowAddCustomerModalWithName, AddCustomerModalCallback],+ [setShowAddCustomerModalWithData, AddCustomerModalCallback],);}