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.

add plan-features-check

Steven Teyf117ab845d52
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/default-domains.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/default-domains.tsx
index 3da60b8c70..4ab0774074 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/default-domains.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/domains/default-domains.tsx
@@ -135,7 +135,7 @@ export function DefaultDomains() {
if (error.message.includes("Upgrade to Pro")) {
toast.custom(() => (
<UpgradeRequiredToast
- title="You've discovered a Pro feature!"
+ planToUpgradeTo="Pro"
message={error.message}
/>
));
diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx
index eea7ce6e92..ecc1821b78 100644
--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx
+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/link/form.tsx
@@ -117,9 +117,12 @@ export function Form() {
const { error } = await res.json();
if (error) {
if (error.message.includes("Upgrade to")) {
+ const planToUpgradeTo = error.message.match(
+ /Upgrade to (.*) to use/,
+ )?.[1];
toast.custom(() => (
<UpgradeRequiredToast
- title={`You've discovered a ${nextPlan.name} feature!`}
+ planToUpgradeTo={planToUpgradeTo}
message={error.message}
/>
));
diff --git a/apps/web/lib/api/links/plan-features-check.ts b/apps/web/lib/api/links/plan-features-check.ts
new file mode 100644
index 0000000000..70efd9577c
--- /dev/null
+++ b/apps/web/lib/api/links/plan-features-check.ts
@@ -0,0 +1,64 @@
+import { NewLinkProps } from "@/lib/types";
+import { combineWords } from "@dub/utils";
+
+export const proFeaturesCheck = (payload: NewLinkProps) => {
+ const {
+ proxy,
+ password,
+ rewrite,
+ expiresAt,
+ ios,
+ android,
+ geo,
+ testVariants,
+ trackConversion,
+ doIndex,
+ } = payload;
+
+ if (
+ proxy ||
+ password ||
+ rewrite ||
+ expiresAt ||
+ ios ||
+ android ||
+ geo ||
+ testVariants ||
+ trackConversion ||
+ doIndex
+ ) {
+ const proFeaturesString = combineWords(
+ [
+ proxy && "custom social media cards",
+ password && "password protection",
+ rewrite && "link cloaking",
+ expiresAt && "link expiration",
+ ios && "iOS targeting",
+ android && "Android targeting",
+ geo && "geo targeting",
+ doIndex && "search engine indexing",
+ ].filter(Boolean) as string[],
+ );
+
+ throw new Error(
+ `You can only use ${proFeaturesString} on a Pro plan and above. Upgrade to Pro to use these features.`,
+ );
+ }
+};
+
+export const businessFeaturesCheck = (payload: NewLinkProps) => {
+ const { testVariants, trackConversion } = payload;
+
+ if (testVariants || trackConversion) {
+ const businessFeaturesString = combineWords(
+ [
+ testVariants && "A/B testing",
+ trackConversion && "conversion tracking",
+ ].filter(Boolean) as string[],
+ );
+
+ throw new Error(
+ `You can only use ${businessFeaturesString} on a Business plan and above. Upgrade to Business to use these features.`,
+ );
+ }
+};
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index e612a11899..6a4cf6c808 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -8,7 +8,6 @@ import { prisma } from "@dub/prisma";
import {
DUB_DOMAINS,
UTMTags,
- combineWords,
constructURLFromUTMParams,
getApexDomain,
getDomainWithoutWWW,
@@ -20,6 +19,7 @@ import {
pluralize,
} from "@dub/utils";
import { combineTagIds } from "../tags/combine-tag-ids";
+import { businessFeaturesCheck, proFeaturesCheck } from "./plan-features-check";
import { keyChecks, processKey } from "./utils";
export async function processLink<T extends Record<string, any>>({
@@ -61,13 +61,7 @@ export async function processLink<T extends Record<string, any>>({
image,
proxy,
trackConversion,
- password,
- rewrite,
expiredUrl,
- ios,
- android,
- geo,
- doIndex,
tagNames,
folderId,
externalId,
@@ -123,38 +117,36 @@ export async function processLink<T extends Record<string, any>>({
code: "forbidden",
};
}
-
- if (
- proxy ||
- password ||
- rewrite ||
- expiresAt ||
- ios ||
- android ||
- geo ||
- doIndex
- ) {
- const proFeaturesString = combineWords(
- [
- proxy && "custom social media cards",
- password && "password protection",
- rewrite && "link cloaking",
- expiresAt && "link expiration",
- ios && "iOS targeting",
- android && "Android targeting",
- geo && "geo targeting",
- doIndex && "search engine indexing",
- ].filter(Boolean) as string[],
- );
-
+ try {
+ businessFeaturesCheck(payload);
+ proFeaturesCheck(payload);
+ } catch (error) {
+ return {
+ link: payload,
+ error: error.message,
+ code: "forbidden",
+ };
+ }
+ } else if (workspace.plan === "pro") {
+ try {
+ businessFeaturesCheck(payload);
+ } catch (error) {
return {
link: payload,
- error: `You can only use ${proFeaturesString} on a Pro plan and above. Upgrade to Pro to use these features.`,
+ error: error.message,
code: "forbidden",
};
}
}
+ if (!trackConversion && testVariants) {
+ return {
+ link: payload,
+ error: "Conversion tracking must be enabled to use A/B testing.",
+ code: "unprocessable_entity",
+ };
+ }
+
const domains = workspace
? await prisma.domain.findMany({
where: { projectId: workspace.id },
@@ -288,34 +280,6 @@ export async function processLink<T extends Record<string, any>>({
}
}
- if (!workspace || workspace.plan === "free" || workspace.plan === "pro") {
- if (trackConversion) {
- return {
- link: payload,
- error:
- "Conversion tracking is only available for workspaces with a Business plan and above. Please upgrade to continue.",
- code: "forbidden",
- };
- }
-
- if (testVariants) {
- return {
- link: payload,
- error:
- "A/B testing is only available for workspaces with a Business plan and above. Please upgrade to continue.",
- code: "forbidden",
- };
- }
- }
-
- if (!trackConversion && testVariants) {
- return {
- link: payload,
- error: "Conversion tracking must be enabled to use A/B testing.",
- code: "unprocessable_entity",
- };
- }
-
if (externalId && workspace && !skipExternalIdChecks) {
const link = await prisma.link.findUnique({
where: {
diff --git a/apps/web/ui/domains/add-edit-domain-form.tsx b/apps/web/ui/domains/add-edit-domain-form.tsx
index 4b80b5dd8f..b80fe46f8d 100644
--- a/apps/web/ui/domains/add-edit-domain-form.tsx
+++ b/apps/web/ui/domains/add-edit-domain-form.tsx
@@ -243,7 +243,7 @@ export function AddEditDomainForm({
if (error.message.includes("Upgrade to Pro")) {
toast.custom(() => (
<UpgradeRequiredToast
- title="You've discovered a Pro feature!"
+ planToUpgradeTo="Pro"
message={error.message}
/>
));
diff --git a/apps/web/ui/modals/link-builder/ab-testing-modal.tsx b/apps/web/ui/modals/link-builder/ab-testing-modal.tsx
index 08ca9c46af..65c82eec61 100644
--- a/apps/web/ui/modals/link-builder/ab-testing-modal.tsx
+++ b/apps/web/ui/modals/link-builder/ab-testing-modal.tsx
@@ -347,7 +347,7 @@ function ABTestingModal({
onClick={addTestUrl}
disabledTooltip={
testVariants.length >= MAX_TEST_COUNT
- ? `You may only add ${MAX_TEST_COUNT} URLs`
+ ? `You may only add up to ${MAX_TEST_COUNT} URLs for an A/B test`
: undefined
}
text="Add URL"
diff --git a/apps/web/ui/modals/link-builder/ab-testing/ab-testing-modal.tsx b/apps/web/ui/modals/link-builder/ab-testing/ab-testing-modal.tsx
index 1c93247b16..0de43e52f2 100644
--- a/apps/web/ui/modals/link-builder/ab-testing/ab-testing-modal.tsx
+++ b/apps/web/ui/modals/link-builder/ab-testing/ab-testing-modal.tsx
@@ -389,7 +389,7 @@ function ABTestingEdit({
onClick={addTestUrl}
disabledTooltip={
testVariants.length >= MAX_TEST_COUNT
- ? `You may only add ${MAX_TEST_COUNT} URLs`
+ ? `You may only add up to ${MAX_TEST_COUNT} URLs for an A/B test`
: undefined
}
text="Add URL"
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index ec1bba3b1e..636134af74 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -311,9 +311,12 @@ function LinkBuilderInner({
if (error) {
if (error.message.includes("Upgrade to")) {
+ const planToUpgradeTo = error.message.match(
+ /Upgrade to (.*) to use/,
+ )?.[1];
toast.custom(() => (
<UpgradeRequiredToast
- title={`You've discovered a ${nextPlan.name} feature!`}
+ planToUpgradeTo={planToUpgradeTo}
message={error.message}
/>
));
diff --git a/apps/web/ui/shared/upgrade-required-toast.tsx b/apps/web/ui/shared/upgrade-required-toast.tsx
index ae5dc0f838..6172cbcba2 100644
--- a/apps/web/ui/shared/upgrade-required-toast.tsx
+++ b/apps/web/ui/shared/upgrade-required-toast.tsx
@@ -1,23 +1,30 @@
"use client";
import useWorkspace from "@/lib/swr/use-workspace";
+import { capitalize } from "@dub/utils";
import { Crown } from "lucide-react";
import Link from "next/link";
export const UpgradeRequiredToast = ({
title,
+ planToUpgradeTo,
message,
}: {
- title: string;
+ title?: string;
+ planToUpgradeTo?: string;
message: string;
}) => {
const { slug, nextPlan } = useWorkspace();
+ planToUpgradeTo = planToUpgradeTo || nextPlan?.name;
return (
<div className="flex flex-col space-y-3 rounded-lg bg-white p-6 shadow-lg">
<div className="flex items-center space-x-1.5">
<Crown className="h-5 w-5 text-black" />{" "}
- <p className="font-semibold">{title}</p>
+ <p className="font-semibold">
+ {title ||
+ `You've discovered a ${capitalize(planToUpgradeTo)} feature!`}
+ </p>
</div>
<p className="text-sm text-neutral-600">{message}</p>
<Link
@@ -25,7 +32,7 @@ export const UpgradeRequiredToast = ({
target="_blank"
className="w-full rounded-md border border-black bg-black px-3 py-1.5 text-center text-sm text-white transition-all hover:bg-neutral-800 hover:ring-4 hover:ring-neutral-200"
>
- {nextPlan ? `Upgrade to ${nextPlan.name}` : "Contact support"}
+ {planToUpgradeTo ? `Upgrade to ${planToUpgradeTo}` : "Contact support"}
</Link>
</div>
);