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.

update partner link creation

Steven Tey948d62f9b933
diff --git a/apps/web/app/(ee)/api/embed/referrals/links/route.ts b/apps/web/app/(ee)/api/embed/referrals/links/route.ts
index e0cd4b1922..1b0275c88d 100644
--- a/apps/web/app/(ee)/api/embed/referrals/links/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/links/route.ts
@@ -44,11 +44,21 @@ export const POST = withReferralsEmbedToken(
});
}
- if (url && getApexDomain(url) !== getApexDomain(program.url)) {
- throw new DubApiError({
- code: "bad_request",
- message: `The provided URL domain (${getApexDomain(url)}) does not match the program's domain (${getApexDomain(program.url)}).`,
- });
+ if (url) {
+ if (
+ program.urlValidationMode === "domain" &&
+ getApexDomain(url) !== getApexDomain(program.url)
+ ) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `The provided URL domain (${getApexDomain(url)}) does not match the program's domain (${getApexDomain(program.url)}).`,
+ });
+ } else if (program.urlValidationMode === "exact" && url !== program.url) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `The provided URL (${url}) does not match the program's URL (${program.url}).`,
+ });
+ }
}
const workspaceOwner = await prisma.projectUsers.findFirst({
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/add-edit-link.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/add-edit-link.tsx
index 9ccabe1a2b..e49fef4649 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/add-edit-link.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/add-edit-link.tsx
@@ -7,7 +7,12 @@ import {
useCopyToClipboard,
useMediaQuery,
} from "@dub/ui";
-import { cn, linkConstructor, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";
+import {
+ cn,
+ getDomainWithoutWWW,
+ TAB_ITEM_ANIMATION_SETTINGS,
+} from "@dub/utils";
+import { Program } from "@prisma/client";
import { motion } from "framer-motion";
import { useMemo, useState } from "react";
import { useForm } from "react-hook-form";
@@ -15,8 +20,10 @@ import { useEmbedToken } from "../use-embed-token";
import { ReferralsEmbedLink } from "./types";
interface Props {
- destinationDomain: string;
- shortLinkDomain: string;
+ program: Pick<
+ Program,
+ "domain" | "url" | "urlValidationMode" | "maxPartnerLinks"
+ >;
link?: ReferralsEmbedLink | null;
onCancel: () => void;
}
@@ -27,8 +34,7 @@ interface FormData {
}
export function ReferralsEmbedCreateUpdateLink({
- destinationDomain,
- shortLinkDomain,
+ program,
link,
onCancel,
}: Props) {
@@ -39,6 +45,12 @@ export function ReferralsEmbedCreateUpdateLink({
const [isSubmitting, setIsSubmitting] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
+ const shortLinkDomain = program.domain || "";
+ const destinationDomain = program.url
+ ? getDomainWithoutWWW(program.url)!
+ : "";
+ const isExactMode = program.urlValidationMode === "exact";
+
const {
watch,
setValue,
@@ -75,10 +87,7 @@ export function ReferralsEmbedCreateUpdateLink({
},
body: JSON.stringify({
...data,
- url: linkConstructor({
- domain: destinationDomain,
- key: data.url,
- }),
+ url: isExactMode ? undefined : data.url,
}),
});
@@ -202,53 +211,55 @@ export function ReferralsEmbedCreateUpdateLink({
)}
</div>
- <div>
- <div className="flex items-center gap-2">
- <label
- htmlFor="url"
- className="text-content-default block text-sm font-medium"
- >
- Destination URL
- </label>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="The URL your users will get redirected to when they visit your referral link."
- cta="Learn more."
- href="https://dub.co/help/article/how-to-create-link"
- />
- }
- />
- </div>
- <div className="mt-2 flex rounded-md">
- <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm dark:border-neutral-700 dark:bg-neutral-800 dark:text-neutral-400">
- {destinationDomain}
- </span>
- <input
- type="text"
- placeholder="about"
- className="border-border-default text-content-default bg-bg-default block w-full rounded-r-md placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm dark:placeholder-neutral-500 dark:focus:border-neutral-400 dark:focus:ring-neutral-400"
- {...register("url", { required: false })}
- onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
- e.preventDefault();
+ {!isExactMode && (
+ <div>
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor="url"
+ className="text-content-default block text-sm font-medium"
+ >
+ Destination URL
+ </label>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="The URL your users will get redirected to when they visit your referral link."
+ cta="Learn more."
+ href="https://dub.co/help/article/how-to-create-link"
+ />
+ }
+ />
+ </div>
+ <div className="mt-2 flex rounded-md">
+ <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm dark:border-neutral-700 dark:bg-neutral-800 dark:text-neutral-400">
+ {destinationDomain}
+ </span>
+ <input
+ type="text"
+ placeholder="about"
+ className="border-border-default text-content-default bg-bg-default block w-full rounded-r-md placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm dark:placeholder-neutral-500 dark:focus:border-neutral-400 dark:focus:ring-neutral-400"
+ {...register("url", { required: false })}
+ onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
+ e.preventDefault();
- // if pasting in a URL, extract the pathname
- const text = e.clipboardData.getData("text/plain");
+ // if pasting in a URL, extract the pathname
+ const text = e.clipboardData.getData("text/plain");
- try {
- const url = new URL(text);
- e.currentTarget.value = url.pathname.slice(1);
- } catch (err) {
- e.currentTarget.value = text;
- }
+ try {
+ const url = new URL(text);
+ e.currentTarget.value = url.pathname.slice(1);
+ } catch (err) {
+ e.currentTarget.value = text;
+ }
- setValue("url", e.currentTarget.value, {
- shouldDirty: true,
- });
- }}
- />
+ setValue("url", e.currentTarget.value, {
+ shouldDirty: true,
+ });
+ }}
+ />
+ </div>
</div>
- </div>
+ )}
</div>
</form>
</motion.div>
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx
index 127e3cb7bc..2abbf056ac 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/links-list.tsx
@@ -1,4 +1,3 @@
-import { PARTNER_LINKS_LIMIT } from "@/lib/embed/constants";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
import { Button, CopyButton, Table, Users, useTable } from "@dub/ui";
import { Pen2, Plus2 } from "@dub/ui/icons";
@@ -9,6 +8,7 @@ import {
nFormatter,
TAB_ITEM_ANIMATION_SETTINGS,
} from "@dub/utils";
+import { Program } from "@prisma/client";
import { motion } from "framer-motion";
import { useEffect, useState } from "react";
import useSWR from "swr";
@@ -17,12 +17,17 @@ import { ReferralsEmbedLink } from "./types";
interface Props {
links: ReferralsEmbedLink[];
+ program: Pick<
+ Program,
+ "domain" | "url" | "urlValidationMode" | "maxPartnerLinks"
+ >;
onCreateLink: () => void;
onEditLink: (link: ReferralsEmbedLink) => void;
}
export function ReferralsEmbedLinksList({
links,
+ program,
onCreateLink,
onEditLink,
}: Props) {
@@ -48,7 +53,8 @@ export function ReferralsEmbedLinksList({
}
}, [refreshedLinks]);
- const linksLimitReached = partnerLinks.length >= PARTNER_LINKS_LIMIT;
+ const maxPartnerLinks = program.maxPartnerLinks;
+ const linksLimitReached = partnerLinks.length >= maxPartnerLinks;
const { table, ...tableProps } = useTable({
data: partnerLinks,
@@ -100,7 +106,7 @@ export function ReferralsEmbedLinksList({
disabled={linksLimitReached}
disabledTooltip={
linksLimitReached
- ? `You have reached the limit of ${PARTNER_LINKS_LIMIT} referral links.`
+ ? `You have reached the limit of ${maxPartnerLinks} referral links.`
: undefined
}
/>
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/links.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/links.tsx
index ca0d8ce5bf..fef3676289 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/links.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/links.tsx
@@ -1,3 +1,4 @@
+import { Program } from "@prisma/client";
import { useState } from "react";
import { ReferralsEmbedCreateUpdateLink } from "./add-edit-link";
import { ReferralsEmbedLinksList } from "./links-list";
@@ -5,15 +6,13 @@ import { ReferralsEmbedLink } from "./types";
interface Props {
links: ReferralsEmbedLink[];
- destinationDomain: string;
- shortLinkDomain: string;
+ program: Pick<
+ Program,
+ "domain" | "url" | "urlValidationMode" | "maxPartnerLinks"
+ >;
}
-export default function ReferralsEmbedLinks({
- links,
- destinationDomain,
- shortLinkDomain,
-}: Props) {
+export default function ReferralsEmbedLinks({ links, program }: Props) {
const [createLink, setCreateLink] = useState(false);
const [link, setLink] = useState<ReferralsEmbedLink | null>(null);
@@ -21,8 +20,7 @@ export default function ReferralsEmbedLinks({
<div className="flex flex-col space-y-6">
{createLink ? (
<ReferralsEmbedCreateUpdateLink
- destinationDomain={destinationDomain}
- shortLinkDomain={shortLinkDomain}
+ program={program}
link={link}
onCancel={() => {
setCreateLink(false);
@@ -32,6 +30,7 @@ export default function ReferralsEmbedLinks({
) : (
<ReferralsEmbedLinksList
links={links}
+ program={program}
onCreateLink={() => setCreateLink(true)}
onEditLink={(link) => {
setLink(link);
diff --git a/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx b/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx
index 19a91594fa..b8c58d0a68 100644
--- a/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx
+++ b/apps/web/app/(ee)/app.dub.co/embed/referrals/page-client.tsx
@@ -20,7 +20,7 @@ import {
useLocalStorage,
Wordmark,
} from "@dub/ui";
-import { cn, getDomainWithoutWWW, getPrettyUrl } from "@dub/utils";
+import { cn, getPrettyUrl } from "@dub/utils";
import { AnimatePresence } from "framer-motion";
import { useEffect, useMemo, useState } from "react";
import { ReferralsEmbedActivity } from "./activity";
@@ -99,11 +99,6 @@ export function ReferralsEmbedPageClient({
if (!tabs.includes(selectedTab)) setSelectedTab(tabs[0]);
}, [tabs, selectedTab]);
- const shortLinkDomain = program.domain || "";
- const destinationDomain = program.url
- ? getDomainWithoutWWW(program.url)!
- : "";
-
const partnerLink =
links.length > 0
? constructPartnerLink({
@@ -257,11 +252,7 @@ export function ReferralsEmbedPageClient({
) : selectedTab === "Earnings" ? (
<ReferralsEmbedEarnings salesCount={stats.sales} />
) : selectedTab === "Links" ? (
- <ReferralsEmbedLinks
- links={links}
- destinationDomain={destinationDomain}
- shortLinkDomain={shortLinkDomain}
- />
+ <ReferralsEmbedLinks links={links} program={program} />
) : selectedTab === "Leaderboard" &&
programEmbedData?.leaderboard?.mode !== "disabled" ? (
<ReferralsEmbedLeaderboard />
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx
index 4ea4652070..4d2a24e0c2 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx
@@ -62,7 +62,8 @@ export function ProgramLinksPageClient() {
defaultInterval={DUB_PARTNERS_ANALYTICS_INTERVAL}
/>
{links &&
- links.length < programEnrollment?.program?.maxPartnerLinks && (
+ links.length <
+ (programEnrollment?.program?.maxPartnerLinks ?? 10) && (
<Button
text="Create Link"
className="w-fit"
diff --git a/apps/web/lib/partners/get-link-structure-options.ts b/apps/web/lib/partners/get-link-structure-options.ts
index 2cd48f2661..3de940945a 100644
--- a/apps/web/lib/partners/get-link-structure-options.ts
+++ b/apps/web/lib/partners/get-link-structure-options.ts
@@ -1,9 +1,9 @@
"use client";
-import { LinkStructure } from "@dub/prisma/client";
+import { PartnerLinkStructure } from "@dub/prisma/client";
import { getDomainWithoutWWW } from "@dub/utils";
-export const getLinkStructureOptions = ({
+export const getPartnerLinkStructureOptions = ({
domain,
url,
}: {
@@ -15,19 +15,19 @@ export const getLinkStructureOptions = ({
return [
{
- id: LinkStructure.short,
+ id: PartnerLinkStructure.short,
label: "Short link",
example: `${shortDomain}/{partnerName}`,
comingSoon: false,
},
{
- id: LinkStructure.query,
+ id: PartnerLinkStructure.query,
label: "Query parameter",
example: `${websiteDomain}?via={partnerName}`,
comingSoon: false,
},
{
- id: LinkStructure.path,
+ id: PartnerLinkStructure.path,
label: "Dynamic path",
example: `${websiteDomain}/refer/{partnerName}`,
comingSoon: true,
diff --git a/apps/web/lib/zod/schemas/program-onboarding.ts b/apps/web/lib/zod/schemas/program-onboarding.ts
index da48c20914..3c968ecd8d 100644
--- a/apps/web/lib/zod/schemas/program-onboarding.ts
+++ b/apps/web/lib/zod/schemas/program-onboarding.ts
@@ -1,5 +1,5 @@
import { ToltProgramSchema } from "@/lib/tolt/schemas";
-import { LinkStructure, RewardStructure } from "@dub/prisma/client";
+import { PartnerLinkStructure, RewardStructure } from "@dub/prisma/client";
import { z } from "zod";
import { maxDurationSchema } from "./misc";
import { updateProgramSchema } from "./programs";
@@ -11,7 +11,7 @@ export const programInfoSchema = z.object({
logo: z.string(),
domain: z.string(),
url: parseUrlSchema.nullable(),
- linkStructure: z.nativeEnum(LinkStructure).default("short"),
+ linkStructure: z.nativeEnum(PartnerLinkStructure).default("short"),
linkParameter: z.string().nullish(),
});
diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts
index fc21510aa9..7f283fc242 100644
--- a/apps/web/lib/zod/schemas/programs.ts
+++ b/apps/web/lib/zod/schemas/programs.ts
@@ -4,8 +4,9 @@ import {
} from "@/lib/analytics/constants";
import { ALLOWED_MIN_PAYOUT_AMOUNTS } from "@/lib/partners/constants";
import {
- LinkStructure,
PartnerBannedReason,
+ PartnerLinkStructure,
+ PartnerUrlValidationMode,
ProgramEnrollmentStatus,
} from "@dub/prisma/client";
import { z } from "zod";
@@ -28,8 +29,10 @@ export const ProgramSchema = z.object({
cookieLength: z.number(),
holdingPeriodDays: z.number(),
minPayoutAmount: z.number(),
- linkStructure: z.nativeEnum(LinkStructure),
+ linkStructure: z.nativeEnum(PartnerLinkStructure),
linkParameter: z.string().nullish(),
+ urlValidationMode: z.nativeEnum(PartnerUrlValidationMode),
+ maxPartnerLinks: z.number(),
landerPublishedAt: z.date().nullish(),
autoApprovePartnersEnabledAt: z.date().nullish(),
rewards: z.array(RewardSchema).nullish(),
@@ -65,7 +68,7 @@ export const updateProgramSchema = z.object({
.refine((val) => ALLOWED_MIN_PAYOUT_AMOUNTS.includes(val), {
message: `Minimum payout amount must be one of ${ALLOWED_MIN_PAYOUT_AMOUNTS.join(", ")}`,
}),
- linkStructure: z.nativeEnum(LinkStructure),
+ linkStructure: z.nativeEnum(PartnerLinkStructure),
supportEmail: z.string().email().max(255).nullish(),
helpUrl: z.string().url().max(500).nullish(),
termsUrl: z.string().url().max(500).nullish(),
diff --git a/apps/web/ui/modals/partner-link-modal.tsx b/apps/web/ui/modals/partner-link-modal.tsx
index dc6e90e5b7..553cba7c10 100644
--- a/apps/web/ui/modals/partner-link-modal.tsx
+++ b/apps/web/ui/modals/partner-link-modal.tsx
@@ -170,6 +170,9 @@ function PartnerLinkModalContent({
getDomainWithoutWWW(programEnrollment?.program?.url || "https://dub.co") ??
"dub.co";
const shortLinkDomain = programEnrollment?.program?.domain ?? "dub.sh";
+ const urlValidationMode =
+ programEnrollment?.program?.urlValidationMode ?? "domain";
+ const isExactMode = urlValidationMode === "exact";
const [lockKey, setLockKey] = useState(Boolean(link));
const [isLoading, setIsLoading] = useState(false);
@@ -233,10 +236,7 @@ function PartnerLinkModalContent({
},
body: JSON.stringify({
...data,
- url: linkConstructor({
- domain: destinationDomain,
- key: data.url,
- }),
+ url: isExactMode ? undefined : data.url,
}),
},
);
@@ -339,48 +339,50 @@ function PartnerLinkModalContent({
</div>
</div>
- <div>
- <div className="flex items-center gap-2">
- <label
- htmlFor="url"
- className="block text-sm font-medium text-neutral-700"
- >
- Destination URL
- </label>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="The URL your users will get redirected to when they visit your short link."
- cta="Learn more."
- href="https://dub.co/help/article/how-to-create-link"
- />
- }
- />
- </div>
- <div className="mt-2 flex rounded-md">
- <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
- {destinationDomain}
- </span>
- <input
- {...register("url", { required: false })}
- type="text"
- id="url"
- placeholder="(optional)"
- onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
- e.preventDefault();
- // if pasting in a URL, extract the pathname
- const text = e.clipboardData.getData("text/plain");
- try {
- const url = new URL(text);
- e.currentTarget.value = url.pathname.slice(1);
- } catch (err) {
- e.currentTarget.value = text;
+ {!isExactMode && (
+ <div>
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor="url"
+ className="block text-sm font-medium text-neutral-700"
+ >
+ Destination URL
+ </label>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="The URL your users will get redirected to when they visit your short link."
+ cta="Learn more."
+ href="https://dub.co/help/article/how-to-create-link"
+ />
}
- }}
- className="block w-full rounded-r-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- />
+ />
+ </div>
+ <div className="mt-2 flex rounded-md">
+ <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
+ {destinationDomain}
+ </span>
+ <input
+ {...register("url", { required: false })}
+ type="text"
+ id="url"
+ placeholder="(optional)"
+ onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
+ e.preventDefault();
+ // if pasting in a URL, extract the pathname
+ const text = e.clipboardData.getData("text/plain");
+ try {
+ const url = new URL(text);
+ e.currentTarget.value = url.pathname.slice(1);
+ } catch (err) {
+ e.currentTarget.value = text;
+ }
+ }}
+ className="block w-full rounded-r-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ />
+ </div>
</div>
- </div>
+ )}
<div>
<div className="flex items-center gap-2">
diff --git a/packages/prisma/client.ts b/packages/prisma/client.ts
index 2b9eec6898..7d2ed9aadd 100644
--- a/packages/prisma/client.ts
+++ b/packages/prisma/client.ts
@@ -7,11 +7,12 @@ export {
FolderType,
FolderUserRole,
InvoiceStatus,
- LinkStructure,
PartnerBannedReason,
+ PartnerLinkStructure,
PartnerProfileType,
PartnerRole,
PartnerStatus,
+ PartnerUrlValidationMode,
PayoutStatus,
Prisma,
ProgramEnrollmentStatus,