The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add referrals page
Steven Teyec6780c5a28e
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.tsindex 6a49c81d34..84cb43cce9 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts@@ -1,7 +1,6 @@import { DubApiError } from "@/lib/api/errors";import { deleteWorkspace } from "@/lib/api/workspaces";import { withWorkspace } from "@/lib/auth";-import { dub } from "@/lib/dub";import { getFeatureFlags } from "@/lib/edge-config";import { prisma } from "@/lib/prisma";import {@@ -75,10 +74,10 @@ export const PATCH = withWorkspace(},});- // update the workspace's referral link to use the new slug- await dub.links.update(`ext_ws_${workspace.id}`, {- key: slug,- });+ // SOON: update the workspace's referral link to use the new slug+ // await dub.links.update(`ext_ws_${workspace.id}`, {+ // key: slug,+ // });}})(),);diff --git a/apps/web/app/api/workspaces/route.ts b/apps/web/app/api/workspaces/route.tsindex 818c4087da..f9f89a6000 100644--- a/apps/web/app/api/workspaces/route.ts+++ b/apps/web/app/api/workspaces/route.ts@@ -1,6 +1,5 @@import { DubApiError } from "@/lib/api/errors";import { withSession } from "@/lib/auth";-import { dub } from "@/lib/dub";import { checkIfUserExists } from "@/lib/planetscale";import { prisma } from "@/lib/prisma";import {@@ -119,14 +118,15 @@ export const POST = withSession(async ({ req, session }) => {waitUntil((async () => {- // create a referral link for the workspace- await dub.links.create({- domain: "refer.dub.co",- key: slug,- url: "https://dub.co",- externalId: `ws_${workspaceResponse.id}`,- tagIds: ["cm000srqx0004o6ldehod07zc"],- });+ // SOON: create a referral link for the workspace+ // await dub.links.create({+ // domain: "refer.dub.co",+ // key: slug,+ // url: "https://dub.co",+ // externalId: `ws_${workspaceResponse.id}`,+ // tagIds: ["cm000srqx0004o6ldehod07zc"],+ // trackConversion: true,+ // });if (session.user["defaultWorkspace"] === null) {await prisma.user.update({where: {diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsxindex 13101b080d..b2f1a908fa 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx@@ -5,6 +5,7 @@ import {ConnectedDots,CubeSettings,Gear2,+ Gift,Globe,Key,Receipt2,@@ -14,6 +15,8 @@ import {} from "@dub/ui/src/icons";import { ReactNode } from "react";+export const revalidate = 0;+export default async function WorkspaceSettingsLayout({children,params,@@ -67,6 +70,9 @@ export default async function WorkspaceSettingsLayout({icon: ShieldCheck,segment: "security",},+ ...(flags.referrals+ ? [{ name: "Referrals", icon: Gift, segment: "referrals" }]+ : []),],},diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsxnew file mode 100644index 0000000000..acac0c8944--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx@@ -0,0 +1,19 @@+"use client";++import useWorkspace from "@/lib/swr/use-workspace";+import { redirect } from "next/navigation";+import { ReactNode } from "react";++export default function ReferralsPageClient({+ children,+}: {+ children: ReactNode;+}) {+ const { slug, flags } = useWorkspace();++ if (!flags?.referrals) {+ redirect(`/${slug}/settings`);+ }++ return <>{children}</>;+}diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsxnew file mode 100644index 0000000000..21620aa592--- /dev/null+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx@@ -0,0 +1,107 @@+import { Button, CopyButton, Wordmark } from "@dub/ui";+import { Crosshairs, CursorRays, InvoiceDollar } from "@dub/ui/src/icons";+import { COUNTRIES, getPrettyUrl, timeAgo } from "@dub/utils";+import { Mail } from "lucide-react";+import ReferralsPageClient from "./page-client";++export default function ReferralsPage({+ params: { slug },+}: {+ params: { slug: string };+}) {+ const link = `https://refer.dub.co/${slug}`;+ return (+ <ReferralsPageClient>+ <div>+ <h1 className="text-2xl font-semibold tracking-tight text-black">+ Referrals+ </h1>+ <div className="my-4 grid gap-4 lg:grid-cols-3">+ <div className="relative flex flex-col items-center justify-center overflow-hidden rounded-xl border border-gray-200 bg-gradient-to-tr from-gray-100 p-4">+ <h2 className="text-2xl font-semibold text-gray-900">500 clicks</h2>+ <p className="text-sm text-gray-500">Referral Bonus</p>+ <Wordmark className="absolute inset-x-0 -bottom-8 mx-auto h-20 text-gray-200" />+ </div>+ <div className="flex gap-3 lg:col-span-2 lg:flex-col">+ <p className="font-medium text-gray-700">Why refer someone?</p>+ <div className="flex items-center gap-x-2">+ <Crosshairs className="size-4 text-gray-500" />+ <p className="text-sm text-gray-500">+ Earn 500 extra clicks quota per month per signup (up to 16,000+ total)+ </p>+ </div>+ <div className="flex items-center gap-x-2">+ <InvoiceDollar className="size-4 text-gray-500" />+ <p className="text-sm text-gray-500">+ Earn 10% recurring revenue per paying customer (up to 1 year)+ </p>+ </div>+ <div className="grid gap-1.5">+ <p className="text-xs text-gray-500">Referral Link</p>+ <div className="grid max-w-md grid-cols-[1fr_auto] gap-x-2">+ <div className="flex h-9 items-center justify-between gap-x-2 rounded-lg border border-gray-300 bg-white py-1.5 pl-4 pr-2">+ <p className="text-sm text-gray-500">{getPrettyUrl(link)}</p>+ <CopyButton+ value={link}+ variant="neutral"+ className="p-1.5 text-gray-500"+ />+ </div>+ <Button+ text="Invite via email"+ icon={<Mail className="size-4" />}+ className="h-9 rounded-lg"+ />+ </div>+ </div>+ </div>+ </div>+ <div className="w-full border-t border-gray-200" />+ <ClickEvents slug={slug} />+ </div>+ </ReferralsPageClient>+ );+}++async function ClickEvents({ slug }: { slug: string }) {+ const events = (await fetch(+ `https://api.dub.co/events?domain=refer.dub.co&key=${slug}`,+ {+ headers: {+ Authorization: `Bearer ${process.env.DUB_API_KEY}`,+ },+ },+ ).then((res) => res.json())) as {+ timestamp: string;+ click_id: string;+ country: string;+ }[];++ return (+ <div className="grid divide-y divide-gray-200">+ {events.map(({ click_id, country, timestamp }) => (+ <div key={click_id} className="flex justify-between gap-2 p-4">+ <div className="flex items-center gap-2 text-sm text-gray-500">+ <CursorRays className="size-4 text-gray-500" />+ <p>Someone from from</p>+ <img+ alt={country}+ src={`https://flag.vercel.app/m/${country}.svg`}+ className="h-3 w-5"+ />+ <p>+ <span className="font-semibold text-gray-700">+ {COUNTRIES[country]}+ </span>{" "}+ clicked on your link+ </p>+ </div>+ <p className="text-sm text-gray-400">+ {timeAgo(new Date(timestamp), { withAgo: true })}+ </p>+ </div>+ ))}+ </div>+ );+}diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.tsindex 775157739c..5a2fd303b0 100644--- a/apps/web/lib/api/workspaces.ts+++ b/apps/web/lib/api/workspaces.ts@@ -1,5 +1,4 @@import { deleteDomainAndLinks } from "@/lib/api/domains";-import { dub } from "@/lib/dub";import { prisma } from "@/lib/prisma";import { storage } from "@/lib/storage";import { cancelSubscription } from "@/lib/stripe";@@ -108,11 +107,11 @@ export async function deleteWorkspace(storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),// if they have a Stripe subscription, cancel itworkspace.stripeId && cancelSubscription(workspace.stripeId),- // set the referral link to `/deleted/[slug]`- dub.links.update(`ext_ws_${workspace.id}`, {- key: `/deleted/${workspace.slug}`,- archived: true,- }),+ // SOON: set the referral link to `/deleted/[slug]`+ // dub.links.update(`ext_ws_${workspace.id}`, {+ // key: `/deleted/${workspace.slug}`,+ // archived: true,+ // }),// delete the workspaceprisma.project.delete({where: {@@ -195,11 +194,11 @@ export async function deleteWorkspaceAdmin(storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),// if they have a Stripe subscription, cancel itworkspace.stripeId && cancelSubscription(workspace.stripeId),- // set the referral link to `/deleted/[slug]`- dub.links.update(`ext_ws_${workspace.id}`, {- key: `/deleted/${workspace.slug}`,- archived: true,- }),+ // SOON: set the referral link to `/deleted/[slug]`+ // dub.links.update(`ext_ws_${workspace.id}`, {+ // key: `/deleted/${workspace.slug}`,+ // archived: true,+ // }),// delete the workspaceprisma.project.delete({where: {diff --git a/apps/web/lib/edge-config/get-feature-flags.ts b/apps/web/lib/edge-config/get-feature-flags.tsindex f4ec738738..4420ef7cb7 100644--- a/apps/web/lib/edge-config/get-feature-flags.ts+++ b/apps/web/lib/edge-config/get-feature-flags.ts@@ -20,6 +20,7 @@ export const getFeatureFlags = async ({conversions: false,integrations: false,dublink: false,+ referrals: false,};if (!process.env.NEXT_PUBLIC_IS_DUB || !process.env.EDGE_CONFIG) {diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex be9e04a84f..5c80250247 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -70,7 +70,11 @@ export type PlanProps = (typeof plans)[number];export type RoleProps = (typeof roles)[number];-export type BetaFeatures = "conversions" | "integrations" | "dublink";+export type BetaFeatures =+ | "conversions"+ | "integrations"+ | "dublink"+ | "referrals";export interface WorkspaceProps extends Project {logo: string | null;diff --git a/apps/web/scripts/backfill-referral-links.ts b/apps/web/scripts/backfill-referral-links.tsnew file mode 100644index 0000000000..fce6cf767e--- /dev/null+++ b/apps/web/scripts/backfill-referral-links.ts@@ -0,0 +1,44 @@+import { dub } from "@/lib/dub";+import { prisma } from "@/lib/prisma";+import "dotenv-flow/config";++async function main() {+ const workspaces = await prisma.project+ .findMany({+ where: {+ slug: {+ in: ["dub", "steven", "acme"],+ },+ },+ select: {+ id: true,+ slug: true,+ },+ orderBy: {+ createdAt: "asc",+ },+ })+ .then((workspaces) =>+ workspaces.map((workspace) => ({+ id: `ws_${workspace.id}`,+ slug: workspace.slug,+ })),+ );++ console.table(workspaces.slice(0, 10));++ const res = await dub.links.createMany(+ workspaces.map((workspace) => ({+ domain: "refer.dub.co",+ key: workspace.slug,+ url: "https://dub.co",+ externalId: `ws_${workspace.id}`,+ tagIds: ["cm000srqx0004o6ldehod07zc"],+ trackConversion: true,+ })),+ );++ console.log(res);+}++main();diff --git a/packages/ui/src/icons/nucleo/gift.tsx b/packages/ui/src/icons/nucleo/gift.tsxnew file mode 100644index 0000000000..3233819c44--- /dev/null+++ b/packages/ui/src/icons/nucleo/gift.tsx@@ -0,0 +1,64 @@+import { SVGProps } from "react";++export function Gift(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <line+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x1="9"+ x2="9"+ y1="5.25"+ y2="16.25"+ />+ <path+ d="M3.75,3.5c0-.966,.784-1.75,1.75-1.75,2.589,0,3.5,3.5,3.5,3.5h-3.5c-.966,0-1.75-.784-1.75-1.75Z"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <path+ d="M12.5,5.25h-3.5s.911-3.5,3.5-3.5c.966,0,1.75,.784,1.75,1.75s-.784,1.75-1.75,1.75Z"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <path+ d="M14.25,8.25v6c0,1.105-.895,2-2,2H5.75c-1.105,0-2-.895-2-2v-6"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ />+ <rect+ height="3"+ width="14.5"+ fill="none"+ rx="1"+ ry="1"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="1.5"+ x="1.75"+ y="5.25"+ />+ </g>+ </svg>+ );+}diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.tsindex 0a0041a72d..1bb892ab81 100644--- a/packages/ui/src/icons/nucleo/index.ts+++ b/packages/ui/src/icons/nucleo/index.ts@@ -36,6 +36,7 @@ export * from "./flag2";export * from "./gaming-console";export * from "./gear";export * from "./gear2";+export * from "./gift";export * from "./globe";export * from "./globe2";export * from "./grid-layout-rows";