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 referredBy

Steven Tey524952c81aa3
diff --git a/apps/web/app/api/dub/webhook/lead-created.ts b/apps/web/app/api/dub/webhook/lead-created.ts
index 9d405cf178..3b310a6958 100644
--- a/apps/web/app/api/dub/webhook/lead-created.ts
+++ b/apps/web/app/api/dub/webhook/lead-created.ts
@@ -1,4 +1,5 @@
import { prisma } from "@/lib/prisma";
+import { REFERRAL_SIGNUPS_MAX } from "@/lib/referrals/constants";
import { sendEmail } from "emails";
import NewReferralSignup from "emails/new-referral-signup";
@@ -9,10 +10,6 @@ export async function leadCreated(data: any) {
return "Referral link not found in webhook payload";
}
- if (referralLink.leads > 16) {
- return `Referral limit reached for ${referralLink.id}. Skipping...`;
- }
-
const [user, workspace] = await Promise.all([
prisma.user.findUnique({
where: {
@@ -40,26 +37,35 @@ export async function leadCreated(data: any) {
return "User or workspace not found";
}
- const workspaceOwners = workspace.users.map(({ user }) => user);
-
await Promise.all([
- // Update the referrer's workspace usage
+ prisma.user.update({
+ where: {
+ id: user.id,
+ },
+ data: {
+ referredBy: workspace.id,
+ },
+ }),
prisma.project.update({
where: {
id: workspace.id,
},
-
data: {
referredSignups: {
increment: 1,
},
- usageLimit: {
- increment: 500,
- },
+ // If the referral link has less than the max number of signups,
+ // update the referrer's workspace usage
+ ...(referralLink.leads < REFERRAL_SIGNUPS_MAX && {
+ usageLimit: {
+ increment: 500,
+ },
+ }),
},
}),
- workspaceOwners.map(
- (owner) =>
+ // send notification email to workspace owners
+ workspace.users.map(
+ ({ user: owner }) =>
owner.email &&
sendEmail({
email: owner.email,
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 8fc5215d67..509af800bd 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -3,7 +3,6 @@ import { withSession } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { unsubscribe } from "@/lib/resend";
import { storage } from "@/lib/storage";
-import { redis } from "@/lib/upstash";
import { R2_URL, nanoid, trim } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -47,18 +46,8 @@ export const GET = withSession(async ({ session }) => {
}),
]);
- const migratedWorkspace = await redis.hget(
- "migrated_links_users",
- session.user.id,
- );
-
- if (migratedWorkspace) {
- await redis.hdel("migrated_links_users", session.user.id);
- }
-
return NextResponse.json({
...user,
- migratedWorkspace,
provider: account?.provider,
hasPassword: user?.passwordHash !== null,
passwordHash: undefined,
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index 950199f280..86c3197d4c 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -58,6 +58,17 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
? `${APP_DOMAIN}/${workspace.slug}?onboarded=true&plan=${plan}&period=${period}`
: `${baseUrl}${baseUrl.includes("?") ? "&" : "?"}${new URLSearchParams({ upgraded: "true", plan, period })}`;
+ // let referralCoupon: string | undefined;
+ // if (session.user["referredBy"]) {
+ // const coupon = await stripe.coupons.create({
+ // percent_off: 10,
+ // duration: "repeating",
+ // duration_in_months: 12,
+ // max_redemptions: 1,
+ // });
+ // referralCoupon = coupon.id;
+ // }
+
const stripeSession = await stripe.checkout.sessions.create({
customer_email: session.user.email,
billing_address_collection: "required",
@@ -76,6 +87,15 @@ export const POST = withWorkspace(async ({ req, workspace, session }) => {
metadata: {
dubCustomerId: session.user.id,
},
+ ...(referralCoupon
+ ? {
+ discounts: [
+ {
+ coupon: referralCoupon,
+ },
+ ],
+ }
+ : {}),
});
return NextResponse.json(stripeSession);
diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
index a3472d58a4..a187d8c0e0 100644
--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
@@ -1,6 +1,7 @@
import { MainNav } from "@/ui/layout/main-nav";
import NavTabs from "@/ui/layout/nav-tabs";
import Toolbar from "@/ui/layout/toolbar/toolbar";
+import UserSurveyPopup from "@/ui/layout/user-survey";
import { MaxWidthWrapper } from "@dub/ui";
import { constructMetadata } from "@dub/utils";
import { ReactNode, Suspense } from "react";
@@ -23,7 +24,7 @@ export default function Layout({ children }: { children: ReactNode }) {
</div>
{children}
</div>
- {/* <UserSurveyPopup /> */}
+ <UserSurveyPopup />
{/* <ChangelogPopup /> */}
<Toolbar />
</Providers>
diff --git a/apps/web/emails/clicks-exceeded.tsx b/apps/web/emails/clicks-exceeded.tsx
index 4e3beaa5eb..5bc8b4c7da 100644
--- a/apps/web/emails/clicks-exceeded.tsx
+++ b/apps/web/emails/clicks-exceeded.tsx
@@ -23,7 +23,6 @@ export default function ClicksExceeded({
slug: "acme",
usage: 2410,
usageLimit: 1000,
- referredSignups: 0,
plan: "business",
},
type = "firstUsageLimitEmail",
diff --git a/apps/web/lib/fetchers.ts b/apps/web/lib/fetchers.ts
index 4aaab80a14..f6f9f4dd3e 100644
--- a/apps/web/lib/fetchers.ts
+++ b/apps/web/lib/fetchers.ts
@@ -38,7 +38,6 @@ export const getWorkspace = cache(async ({ slug }: { slug: string }) => {
usage: true,
usageLimit: true,
referralLinkId: true,
- referredSignups: true,
plan: true,
stripeId: true,
billingCycleStart: true,
diff --git a/apps/web/lib/referrals/constants.ts b/apps/web/lib/referrals/constants.ts
index 78f944bc97..8c290e4e61 100644
--- a/apps/web/lib/referrals/constants.ts
+++ b/apps/web/lib/referrals/constants.ts
@@ -1,4 +1,4 @@
-export const REFERRAL_REVENUE_SHARE = 0.1;
-
+export const REFERRAL_SIGNUPS_MAX = 32;
export const REFERRAL_CLICKS_QUOTA_BONUS = 500;
export const REFERRAL_CLICKS_QUOTA_BONUS_MAX = 16000;
+export const REFERRAL_REVENUE_SHARE = 0.1;
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 5eada2bc64..225beb47aa 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -110,7 +110,6 @@ export interface UserProps {
image?: string;
createdAt: Date;
source: string | null;
- migratedWorkspace: string | null;
defaultWorkspace?: string;
isMachine: boolean;
hasPassword: boolean;
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index 2fcf44448c..4df94ccdb2 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -67,9 +67,6 @@ export const WorkspaceSchema = z
.string()
.nullable()
.describe("The ID of the referral link of the workspace."),
- referredSignups: z
- .number()
- .describe("The number of signups referred by the workspace."),
conversionEnabled: z
.boolean()
diff --git a/apps/web/prisma/schema/schema.prisma b/apps/web/prisma/schema/schema.prisma
index a0d62ed06c..c0f2be3d33 100644
--- a/apps/web/prisma/schema/schema.prisma
+++ b/apps/web/prisma/schema/schema.prisma
@@ -16,20 +16,25 @@ model User {
emailVerified DateTime?
image String?
isMachine Boolean @default(false)
+
+ // password data
passwordHash String?
invalidLoginAttempts Int @default(0)
lockedAt DateTime?
+ createdAt DateTime @default(now())
+ subscribed Boolean @default(true) // email subscription
+ source String? // where the user came from
+ defaultWorkspace String? // slug of the user's default workspace
+ referredBy String? // ID of the workspace that referred this user
+
+ // relational data
accounts Account[]
sessions Session[]
projects ProjectUsers[]
links Link[]
tokens Token[]
restrictedTokens RestrictedToken[]
- createdAt DateTime @default(now())
- subscribed Boolean @default(true)
- source String?
- defaultWorkspace String?
oAuthCodes OAuthCode[]
integrations Integration[] // Integrations user created in their workspace
installedIntegrations InstalledIntegration[] // Integrations user installed in their workspace
diff --git a/apps/web/tests/utils/schema.ts b/apps/web/tests/utils/schema.ts
index 8237b57242..b51a35311b 100644
--- a/apps/web/tests/utils/schema.ts
+++ b/apps/web/tests/utils/schema.ts
@@ -70,7 +70,6 @@ export const expectedWorkspace: Partial<Project> = {
usersLimit: expect.any(Number),
referralLinkId: expect.any(String),
- referredSignups: expect.any(Number),
createdAt: expect.any(String),
};
diff --git a/apps/web/ui/layout/user-survey/index.tsx b/apps/web/ui/layout/user-survey/index.tsx
index 454997371b..a947cb99dc 100644
--- a/apps/web/ui/layout/user-survey/index.tsx
+++ b/apps/web/ui/layout/user-survey/index.tsx
@@ -1,10 +1,10 @@
"use client";
-import useUser from "@/lib/swr/use-user";
import { CheckCircleFill } from "@/ui/shared/icons";
import { Popup, PopupContext, useResizeObserver } from "@dub/ui";
import { AnimatePresence, motion } from "framer-motion";
import { X } from "lucide-react";
+import { useSession } from "next-auth/react";
import { createContext, useContext, useRef, useState } from "react";
import { toast } from "sonner";
import SurveyForm from "./survey-form";
@@ -16,11 +16,11 @@ export const UserSurveyContext = createContext<{ status: UserSurveyStatus }>({
});
export default function UserSurveyPopup() {
- const { user } = useUser();
+ const { data: session } = useSession();
return (
- user &&
- !user.source && (
+ session?.user &&
+ !session.user["source"] && (
<Popup hiddenCookieId="hideUserSurveyPopup">
<UserSurveyPopupInner />
</Popup>
@@ -30,6 +30,7 @@ export default function UserSurveyPopup() {
export function UserSurveyPopupInner() {
const { hidePopup } = useContext(PopupContext);
+ const { update } = useSession();
const contentWrapperRef = useRef<HTMLDivElement>(null);
const resizeObserverEntry = useResizeObserver(contentWrapperRef);
@@ -72,7 +73,10 @@ export function UserSurveyPopupInner() {
body: JSON.stringify({ source }),
});
setStatus("success");
- setTimeout(hidePopup, 3000);
+ setTimeout(() => {
+ update();
+ hidePopup();
+ }, 3000);
} catch (e) {
toast.error("Error saving response. Please try again.");
setStatus("idle");
diff --git a/apps/web/ui/layout/user-survey/survey-form.tsx b/apps/web/ui/layout/user-survey/survey-form.tsx
index e7b74d4f23..1e42e4b87b 100644
--- a/apps/web/ui/layout/user-survey/survey-form.tsx
+++ b/apps/web/ui/layout/user-survey/survey-form.tsx
@@ -4,15 +4,16 @@ import {
Google,
Label,
LinkedIn,
- Logo,
ProductHunt,
RadioGroup,
RadioGroupItem,
Twitter,
useMediaQuery,
+ Wordmark,
} from "@dub/ui";
+import { Globe } from "@dub/ui/src/icons";
import { cn } from "@dub/utils";
-import { ChevronRight, Globe } from "lucide-react";
+import { ChevronRight } from "lucide-react";
import { useContext, useState } from "react";
import { UserSurveyContext } from ".";
@@ -62,8 +63,8 @@ export default function SurveyForm({
const { status } = useContext(UserSurveyContext);
return (
- <div className="flex flex-col space-y-4">
- <Logo className="h-8 w-8" />
+ <div className="grid gap-4">
+ <Wordmark className="h-8" />
<p className="text-sm font-medium text-gray-800">
Where did you hear about Dub?
</p>
@@ -100,7 +101,7 @@ export default function SurveyForm({
/>
<Label
htmlFor={option.value}
- className="flex h-full cursor-pointer select-none items-center space-x-4 px-4 py-2 text-gray-600"
+ className="flex h-full cursor-pointer select-none items-center gap-2 px-4 py-2 text-gray-600"
>
<option.icon
className={cn(
diff --git a/apps/web/ui/workspaces/pro-banner.tsx b/apps/web/ui/workspaces/pro-banner.tsx
index efcb34848c..11658dc13e 100644
--- a/apps/web/ui/workspaces/pro-banner.tsx
+++ b/apps/web/ui/workspaces/pro-banner.tsx
@@ -1,4 +1,4 @@
-import { Button, buttonVariants, useRouterStuff } from "@dub/ui";
+import { Button, buttonVariants } from "@dub/ui";
import { cn } from "@dub/utils";
import Cookies from "js-cookie";
import Link from "next/link";
@@ -12,7 +12,6 @@ export default function ProBanner({
setShowProBanner: Dispatch<SetStateAction<boolean>>;
}) {
const { slug } = useParams() as { slug: string };
- const { queryParams } = useRouterStuff();
return (
<div className="fixed bottom-24 z-10 mx-5 flex flex-col space-y-3 rounded-lg border border-gray-200 bg-white p-5 shadow-lg sm:right-5 sm:mx-auto sm:max-w-sm">
diff --git a/apps/web/ui/workspaces/workspace-card.tsx b/apps/web/ui/workspaces/workspace-card.tsx
index c5aabc179d..214b7b2ebf 100644
--- a/apps/web/ui/workspaces/workspace-card.tsx
+++ b/apps/web/ui/workspaces/workspace-card.tsx
@@ -1,28 +1,13 @@
"use client";
-import useUser from "@/lib/swr/use-user";
import { WorkspaceProps } from "@/lib/types";
import { CheckCircleFill, XCircleFill } from "@/ui/shared/icons";
-import {
- Badge,
- BlurImage,
- Globe2,
- InlineSnippet,
- NumberTooltip,
- Tooltip,
-} from "@dub/ui";
-import {
- DICEBEAR_AVATAR_URL,
- cn,
- fetcher,
- nFormatter,
- punycode,
-} from "@dub/utils";
+import { Badge, BlurImage, Globe2, NumberTooltip, Tooltip } from "@dub/ui";
+import { DICEBEAR_AVATAR_URL, fetcher, nFormatter, punycode } from "@dub/utils";
import { BarChart2, ExternalLink, Link2, MinusCircle } from "lucide-react";
import Link from "next/link";
import useSWR from "swr";
import PlanBadge from "./plan-badge";
-import WorkspaceArrow from "./workspace-arrow";
export default function WorkspaceCard({
id,
@@ -38,142 +23,112 @@ export default function WorkspaceCard({
fetcher,
);
- const { user } = useUser();
-
- const isMigratedWorkspace = user?.migratedWorkspace === id;
-
return (
- <div className="group relative">
- {isMigratedWorkspace && (
- <>
- <div className="absolute -inset-1 rounded-lg bg-gradient-to-r from-red-600 to-violet-600 opacity-25 blur-lg transition duration-1000 group-hover:opacity-75 group-hover:duration-200" />
- <WorkspaceArrow className="absolute -bottom-20 right-56 z-10 text-violet-600 lg:right-0" />
- <div className="absolute -bottom-28 right-0 z-10 w-full max-w-[16rem] rounded-lg border border-gray-200 bg-white p-3 text-center text-sm shadow lg:-right-56">
- <p>
- Your <InlineSnippet>dub.sh</InlineSnippet> links have been
- migrated to a custom workspace.
- </p>
- <a
- href="https://dub.co/changelog/dub-links-updates"
- target="_blank"
- className="mt-1 block text-gray-500 underline underline-offset-4 hover:text-gray-800"
- >
- Read the changelog.
- </a>
- </div>
- </>
- )}
- <Link
- key={slug}
- href={`/${slug}`}
- className={cn(
- "relative flex flex-col justify-between space-y-10 rounded-lg border border-gray-100 bg-white p-6 shadow transition-all hover:shadow-lg",
- {
- "border-violet-600": isMigratedWorkspace,
- },
- )}
- >
- <div className="flex items-start justify-between">
- <div className="flex items-center space-x-3">
- <BlurImage
- src={logo || `${DICEBEAR_AVATAR_URL}${name}`}
- alt={id}
- className="h-10 w-10 flex-shrink-0 overflow-hidden rounded-full"
- width={48}
- height={48}
- />
- <div>
- <h2 className="max-w-[200px] truncate text-lg font-medium text-gray-700">
- {name}
- </h2>
- {!domains ? (
- <div className="mt-1 flex items-center space-x-2">
- <div className="h-5 w-20 animate-pulse rounded-md bg-gray-200" />
- <div className="h-5 w-5 animate-pulse rounded-full bg-gray-200" />
- </div>
- ) : (
- <div className="flex items-center">
- <p className="max-w-[160px] truncate text-gray-500">
- {domains.length > 0
- ? domains.find((d) => d.primary)?.slug || domains[0].slug
- : "dub.sh"}
- </p>
- <Tooltip
- content={
- <DomainsTooltip
- domains={domains}
- title={
- domains.length === 0
- ? "No custom domains added yet – currently using Dub default domains."
- : "Here are all the domains for this workspace."
- }
- cta={
- domains.length === 0
- ? "Add Domain"
- : `Manage Domain${domains.length > 1 ? "s" : ""}`
- }
- href={`/${slug}/settings/domains`}
- />
- }
- >
- <div className="ml-1 flex items-center">
- {domains.length > 1 ? (
- <Badge
- variant="gray"
- className="border-gray-300 transition-all hover:bg-gray-200"
- >
- +{domains.length - 1}
- </Badge>
- ) : domains.length === 0 ? (
- <MinusCircle
- fill="rgb(209 213 219)"
- className="h-5 w-5 text-white"
- />
- ) : (
- <CheckCircleFill className="h-5 w-5 text-blue-500" />
- )}
- </div>
- </Tooltip>
- </div>
- )}
- </div>
- </div>
- <PlanBadge plan={plan} />
- </div>
- <div className="flex items-center space-x-4">
- <div className="flex items-center space-x-1 text-gray-500">
- <Globe2 className="h-4 w-4" />
+ <Link
+ key={slug}
+ href={`/${slug}`}
+ className="relative flex flex-col justify-between space-y-10 rounded-lg border border-gray-100 bg-white p-6 shadow transition-all hover:shadow-lg"
+ >
+ <div className="flex items-start justify-between">
+ <div className="flex items-center space-x-3">
+ <BlurImage
+ src={logo || `${DICEBEAR_AVATAR_URL}${name}`}
+ alt={id}
+ className="h-10 w-10 flex-shrink-0 overflow-hidden rounded-full"
+ width={48}
+ height={48}
+ />
+ <div>
+ <h2 className="max-w-[200px] truncate text-lg font-medium text-gray-700">
+ {name}
+ </h2>
{!domains ? (
- <div className="h-4 w-16 animate-pulse rounded-md bg-gray-200" />
+ <div className="mt-1 flex items-center space-x-2">
+ <div className="h-5 w-20 animate-pulse rounded-md bg-gray-200" />
+ <div className="h-5 w-5 animate-pulse rounded-full bg-gray-200" />
+ </div>
) : (
- <h2 className="whitespace-nowrap text-sm">
- {nFormatter(domains.length)} domain{domains.length != 1 && "s"}
- </h2>
- )}
- </div>
- <div className="flex items-center space-x-1 text-gray-500">
- <Link2 className="h-4 w-4" />
- {count || count === 0 ? (
- <NumberTooltip value={count} unit="links">
- <h2 className="whitespace-nowrap text-sm">
- {nFormatter(count)} link{count != 1 && "s"}
- </h2>
- </NumberTooltip>
- ) : (
- <div className="h-4 w-16 animate-pulse rounded-md bg-gray-200" />
+ <div className="flex items-center">
+ <p className="max-w-[160px] truncate text-gray-500">
+ {domains.length > 0
+ ? domains.find((d) => d.primary)?.slug || domains[0].slug
+ : "dub.sh"}
+ </p>
+ <Tooltip
+ content={
+ <DomainsTooltip
+ domains={domains}
+ title={
+ domains.length === 0
+ ? "No custom domains added yet – currently using Dub default domains."
+ : "Here are all the domains for this workspace."
+ }
+ cta={
+ domains.length === 0
+ ? "Add Domain"
+ : `Manage Domain${domains.length > 1 ? "s" : ""}`
+ }
+ href={`/${slug}/settings/domains`}
+ />
+ }
+ >
+ <div className="ml-1 flex items-center">
+ {domains.length > 1 ? (
+ <Badge
+ variant="gray"
+ className="border-gray-300 transition-all hover:bg-gray-200"
+ >
+ +{domains.length - 1}
+ </Badge>
+ ) : domains.length === 0 ? (
+ <MinusCircle
+ fill="rgb(209 213 219)"
+ className="h-5 w-5 text-white"
+ />
+ ) : (
+ <CheckCircleFill className="h-5 w-5 text-blue-500" />
+ )}
+ </div>
+ </Tooltip>
+ </div>
)}
</div>
- <div className="flex items-center space-x-1 text-gray-500">
- <BarChart2 className="h-4 w-4" />
- <NumberTooltip value={usage}>
+ </div>
+ <PlanBadge plan={plan} />
+ </div>
+ <div className="flex items-center space-x-4">
+ <div className="flex items-center space-x-1 text-gray-500">
+ <Globe2 className="h-4 w-4" />
+ {!domains ? (
+ <div className="h-4 w-16 animate-pulse rounded-md bg-gray-200" />
+ ) : (
+ <h2 className="whitespace-nowrap text-sm">
+ {nFormatter(domains.length)} domain{domains.length != 1 && "s"}
+ </h2>
+ )}
+ </div>
+ <div className="flex items-center space-x-1 text-gray-500">
+ <Link2 className="h-4 w-4" />
+ {count || count === 0 ? (
+ <NumberTooltip value={count} unit="links">
<h2 className="whitespace-nowrap text-sm">
- {nFormatter(usage)} click{usage != 1 && "s"}
+ {nFormatter(count)} link{count != 1 && "s"}
</h2>
</NumberTooltip>
- </div>
+ ) : (
+ <div className="h-4 w-16 animate-pulse rounded-md bg-gray-200" />
+ )}
</div>
- </Link>
- </div>
+ <div className="flex items-center space-x-1 text-gray-500">
+ <BarChart2 className="h-4 w-4" />
+ <NumberTooltip value={usage}>
+ <h2 className="whitespace-nowrap text-sm">
+ {nFormatter(usage)} click{usage != 1 && "s"}
+ </h2>
+ </NumberTooltip>
+ </div>
+ </div>
+ </Link>
);
}