Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Setup: start from the domain the Resend integration set up
Claude556306fec51e
diff --git a/src/app/setup/page.tsx b/src/app/setup/page.tsxindex 09bfc66..1555a9f 100644--- a/src/app/setup/page.tsx+++ b/src/app/setup/page.tsx@@ -84,6 +84,7 @@ export default async function SetupPage() {<SetupShellhosted={isHostedMode()}onVercel={process.env.VERCEL === "1"}+ resendDomain={process.env.RESEND_EMAIL_DOMAIN?.trim() || null}siteHost={readSiteHost()}view={view}/>diff --git a/src/components/setup/setup-shell.tsx b/src/components/setup/setup-shell.tsxindex 824008b..076b181 100644--- a/src/components/setup/setup-shell.tsx+++ b/src/components/setup/setup-shell.tsx@@ -19,6 +19,7 @@ import {DEFAULT_SENDER_LOCAL_PART,DEFAULT_SENDING_SUBDOMAIN,joinSendingDomain,+ readResendEmailDomain,suggestSendingRootDomain,type SenderAddressParts,} from "@/lib/sending-domain";@@ -33,6 +34,8 @@ export type SetupShellView =type SetupShellProps = {hosted: boolean;onVercel: boolean;+ /** RESEND_EMAIL_DOMAIN, set by the Resend integration on Vercel. */+ resendDomain: string | null;/** The host this site answers on, used to guess the sending domain. */siteHost: string | null;view: SetupShellView;@@ -62,6 +65,7 @@ const emptyValues: SetupValues = {export function SetupShell({hosted,onVercel,+ resendDomain,siteHost,view,}: SetupShellProps) {@@ -80,6 +84,7 @@ export function SetupShell({<SetupContenthosted={hosted}onVercel={onVercel}+ resendDomain={resendDomain}siteHost={siteHost}view={view}/>@@ -92,7 +97,13 @@ export function SetupShell({);}-function SetupContent({ hosted, onVercel, siteHost, view }: SetupShellProps) {+function SetupContent({+ hosted,+ onVercel,+ resendDomain,+ siteHost,+ view,+}: SetupShellProps) {if (view.view === "no_database") {return (<SetupNotice title="Connect a database">@@ -176,7 +187,14 @@ function SetupContent({ hosted, onVercel, siteHost, view }: SetupShellProps) {return null;}- return <SetupForm hosted={hosted} siteHost={siteHost} view={view} />;+ return (+ <SetupForm+ hosted={hosted}+ resendDomain={resendDomain}+ siteHost={siteHost}+ view={view}+ />+ );}function SetupNotice({@@ -222,22 +240,33 @@ type SenderErrors = Partial<Record<keyof SenderAddressParts, string>>;function SetupForm({hosted,+ resendDomain,siteHost,view,}: {hosted: boolean;+ resendDomain: string | null;siteHost: string | null;view: Extract<SetupShellView, { view: "claim" | "create" }>;}) {const [values, setValues] = useState<SetupValues>(emptyValues);- const [sender, setSender] = useState<SenderAddressParts>({- localPart: DEFAULT_SENDER_LOCAL_PART,- rootDomain: "",- subdomain: DEFAULT_SENDING_SUBDOMAIN,- });- // Until the administrator types a domain of their own, it follows what+ // The domain the Resend integration set up comes first. Without one,+ // until the administrator types a domain of their own, it follows what// setup can tell from the site's address and the emails typed above.- const [rootDomainEdited, setRootDomainEdited] = useState(false);+ const [integrationSender] = useState(() =>+ readResendEmailDomain(resendDomain),+ );+ const [sender, setSender] = useState<SenderAddressParts>(+ () =>+ integrationSender ?? {+ localPart: DEFAULT_SENDER_LOCAL_PART,+ rootDomain: "",+ subdomain: DEFAULT_SENDING_SUBDOMAIN,+ },+ );+ const [rootDomainEdited, setRootDomainEdited] = useState(+ integrationSender !== null,+ );const [senderErrors, setSenderErrors] = useState<SenderErrors>({});const [errors, setErrors] = useState<SetupErrors>({});const [bannerMessage, setBannerMessage] = useState<string | null>(null);@@ -468,7 +497,14 @@ function SetupForm({onChange={handleSenderChange}parts={senderParts}serverError={errors.senderAddress}- suggested={!rootDomainEdited && suggestedRootDomain !== null}+ source={+ integrationSender &&+ sender.rootDomain === integrationSender.rootDomain+ ? "integration"+ : !rootDomainEdited && suggestedRootDomain !== null+ ? "guess"+ : null+ }/>) : null}@@ -536,7 +572,7 @@ function SenderFields({onChange,parts,serverError,- suggested,+ source,}: {disabled: boolean;errors: SenderErrors;@@ -545,7 +581,8 @@ function SenderFields({) => (event: ChangeEvent<HTMLInputElement>) => void;parts: SenderAddressParts;serverError?: string;- suggested: boolean;+ /** Where the domain shown came from, if setup filled it in. */+ source: "guess" | "integration" | null;}) {const built = buildSenderAddress(parts);const rootDomain = parts.rootDomain.trim().toLowerCase();@@ -593,9 +630,11 @@ function SenderFields({id="setup-sender-domain-description">{rootError ??- (suggested- ? "Filled in from this site's address and the emails above. Change it if your organization uses another domain."- : "The domain your organization's email uses, such as acmecorp.com.")}+ (source === "integration"+ ? "The domain the Resend integration was set up with on Vercel."+ : source === "guess"+ ? "Filled in from this site's address and the emails above. Change it if your organization uses another domain."+ : "The domain your organization's email uses, such as acmecorp.com.")}</p></div>diff --git a/src/lib/sending-domain.test.ts b/src/lib/sending-domain.test.tsindex e61e43f..c9d4ccc 100644--- a/src/lib/sending-domain.test.ts+++ b/src/lib/sending-domain.test.ts@@ -5,6 +5,7 @@ import {buildSenderAddress,isOwnableDomain,normalizeSubdomain,+ readResendEmailDomain,suggestSendingRootDomain,} from "./sending-domain.ts";@@ -118,3 +119,18 @@ test("no domain means deciding later, and bad parts name their field", () => {"localPart",);});++test("the domain the Resend integration set up is used as it is", () => {+ assert.deepEqual(readResendEmailDomain("sign.kevinrose.com"), {+ localPart: "notifications",+ rootDomain: "kevinrose.com",+ subdomain: "sign",+ });+ assert.deepEqual(readResendEmailDomain("mail.acme.com"), {+ localPart: "notifications",+ rootDomain: "mail.acme.com",+ subdomain: "",+ });+ assert.equal(readResendEmailDomain(""), null);+ assert.equal(readResendEmailDomain("acme.vercel.app"), null);+});diff --git a/src/lib/sending-domain.ts b/src/lib/sending-domain.tsindex 7cdfa64..da78373 100644--- a/src/lib/sending-domain.ts+++ b/src/lib/sending-domain.ts@@ -222,3 +222,35 @@ export function buildSenderAddress(sendingDomain,};}++/**+ * The Resend integration on the Vercel Marketplace asks for a domain when it+ * is installed, sets it up in Resend and hands it to the project as+ * RESEND_EMAIL_DOMAIN. Setup starts from that domain as it is, since its+ * DNS records may already be in place. One that begins with "sign." is+ * shown as that subdomain of its parent, so the form reads the same way.+ */+export function readResendEmailDomain(+ value: string | null | undefined,+): SenderAddressParts | null {+ const domain = value ? normalizeEmailDomain(value) : null;++ if (!domain || !isOwnableDomain(domain)) {+ return null;+ }++ const prefix = `${DEFAULT_SENDING_SUBDOMAIN}.`;+ const parent = domain.startsWith(prefix) ? domain.slice(prefix.length) : null;++ return parent && isOwnableDomain(parent)+ ? {+ localPart: DEFAULT_SENDER_LOCAL_PART,+ rootDomain: parent,+ subdomain: DEFAULT_SENDING_SUBDOMAIN,+ }+ : {+ localPart: DEFAULT_SENDER_LOCAL_PART,+ rootDomain: domain,+ subdomain: "",+ };+}