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.

use server components

Kiran K5eca0e250a83
diff --git a/apps/web/app/api/tokens/embed/route.ts b/apps/web/app/api/tokens/embed/route.ts
index a2d0f3b442..54b831c98e 100644
--- a/apps/web/app/api/tokens/embed/route.ts
+++ b/apps/web/app/api/tokens/embed/route.ts
@@ -13,6 +13,7 @@ export const POST = withWorkspace(async ({ workspace, req }) => {
const { linkId } = createReferralTokenSchema.parse(
await parseRequestBody(req),
);
+
await getLinkOrThrow({ linkId, workspaceId: workspace.id });
const token = await createOrRetrieveEmbedToken(linkId);
diff --git a/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx b/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx
index 44d074a984..456f97383c 100644
--- a/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx
@@ -17,22 +17,20 @@ import {
} from "@dub/ui";
import { Copy, MoneyBill2 } from "@dub/ui/src/icons";
import { getPrettyUrl } from "@dub/utils";
-import { notFound } from "next/navigation";
+import { Link, Program } from "@prisma/client";
import { useContext, useEffect } from "react";
import useReferralAnalytics from "../use-referral-analytics";
-import { useReferralLink } from "../use-referral-link";
-import { useReferralProgram } from "../use-referral-program";
import { SaleTable } from "./sale-table";
-export function RewardDashboardPageClient() {
- const { program } = useReferralProgram();
+export function RewardDashboardPageClient({
+ program,
+ link,
+}: {
+ program: Program;
+ link: Link;
+}) {
const { searchParamsObj } = useRouterStuff();
const [copied, copyToClipboard] = useCopyToClipboard();
- const {
- link,
- isLoading: isLoadingLink,
- error: linkError,
- } = useReferralLink();
const {
start,
@@ -46,28 +44,11 @@ export function RewardDashboardPageClient() {
token?: string;
};
- useEffect(() => {
- if (linkError) {
- window.parent.postMessage(
- {
- originator: "Dub",
- event: "ERROR",
- data: linkError.info,
- },
- "*",
- );
- }
- }, [linkError]);
-
- if (!token) {
- notFound();
- }
-
return (
<>
<div className="relative flex flex-col rounded-lg border border-neutral-300 bg-gradient-to-r from-neutral-50 p-4 md:p-6">
{program && (
- <HeroBackground logo={program?.logo} color={program?.brandColor} />
+ <HeroBackground logo={program.logo} color={program.brandColor} />
)}
<span className="flex items-center gap-2 text-sm text-neutral-500">
@@ -76,11 +57,7 @@ export function RewardDashboardPageClient() {
</span>
<div className="relative mt-24 text-lg text-neutral-900 sm:max-w-[50%]">
- {program ? (
- <ProgramCommissionDescription program={program} />
- ) : (
- <div className="mb-7 h-7 w-full animate-pulse rounded-md bg-neutral-200" />
- )}
+ <ProgramCommissionDescription program={program} />
</div>
<span className="mb-1.5 mt-6 block text-sm text-neutral-800">
@@ -88,16 +65,13 @@ export function RewardDashboardPageClient() {
</span>
<div className="xs:flex-row relative flex flex-col items-center gap-2">
- {!isLoadingLink && link ? (
- <input
- type="text"
- readOnly
- value={getPrettyUrl(link.shortLink)}
- className="xs:w-auto h-10 w-full rounded-md border border-neutral-300 px-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 lg:min-w-64 xl:min-w-72"
- />
- ) : (
- <div className="h-10 w-16 animate-pulse rounded-md bg-neutral-200 lg:w-72" />
- )}
+ <input
+ type="text"
+ readOnly
+ value={getPrettyUrl(link.shortLink)}
+ className="xs:w-auto h-10 w-full rounded-md border border-neutral-300 px-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 lg:min-w-64 xl:min-w-72"
+ />
+
<Button
icon={
copied ? (
@@ -108,7 +82,7 @@ export function RewardDashboardPageClient() {
}
text={copied ? "Copied link" : "Copy link"}
className="xs:w-fit"
- onClick={() => copyToClipboard(getPrettyUrl(link?.shortLink))}
+ onClick={() => copyToClipboard(getPrettyUrl(link.shortLink))}
/>
</div>
</div>
@@ -119,7 +93,7 @@ export function RewardDashboardPageClient() {
start: start ? new Date(start) : undefined,
end: end ? new Date(end) : undefined,
interval,
- color: program?.brandColor ?? undefined,
+ color: program.brandColor ?? undefined,
}}
>
<div className="mt-6 rounded-lg border border-neutral-300">
@@ -175,6 +149,19 @@ const EarningsChartContainer = () => {
end,
});
+ useEffect(() => {
+ if (error) {
+ window.parent.postMessage(
+ {
+ originator: "Dub",
+ event: "ERROR",
+ data: error.info,
+ },
+ "*",
+ );
+ }
+ }, [error]);
+
return (
<EarningsChart
timeseries={timeseries}
diff --git a/apps/web/app/app.dub.co/embed/dashboard/page.tsx b/apps/web/app/app.dub.co/embed/dashboard/page.tsx
index 795b21a739..5ac0dd69e1 100644
--- a/apps/web/app/app.dub.co/embed/dashboard/page.tsx
+++ b/apps/web/app/app.dub.co/embed/dashboard/page.tsx
@@ -1,5 +1,14 @@
+import { getLinkAndProgram } from "../utils";
import { RewardDashboardPageClient } from "./page-client";
-export default async function RewardDashboardPage() {
- return <RewardDashboardPageClient />;
+export default async function RewardDashboardPage({
+ searchParams,
+}: {
+ searchParams: { token: string };
+}) {
+ const { token } = searchParams;
+
+ const { link, program } = await getLinkAndProgram(token);
+
+ return <RewardDashboardPageClient program={program} link={link} />;
}
diff --git a/apps/web/app/app.dub.co/embed/use-referral-link.ts b/apps/web/app/app.dub.co/embed/use-referral-link.ts
deleted file mode 100644
index 902d02cbaa..0000000000
--- a/apps/web/app/app.dub.co/embed/use-referral-link.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { fetcher } from "@dub/utils";
-import { Link } from "@prisma/client";
-import useSWR from "swr";
-
-export const useReferralLink = () => {
- const {
- data: link,
- error,
- isLoading,
- } = useSWR<Link>(`/api/referrals/link`, fetcher);
-
- return {
- link,
- error,
- isLoading,
- };
-};
diff --git a/apps/web/app/app.dub.co/embed/use-referral-program.ts b/apps/web/app/app.dub.co/embed/use-referral-program.ts
deleted file mode 100644
index 7a3dd39acf..0000000000
--- a/apps/web/app/app.dub.co/embed/use-referral-program.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { fetcher } from "@dub/utils";
-import { Program } from "@prisma/client";
-import useSWR from "swr";
-
-export const useReferralProgram = () => {
- const {
- data: program,
- error,
- isLoading,
- } = useSWR<Program>(`/api/referrals/program`, fetcher);
-
- return {
- program,
- error,
- isLoading,
- };
-};
diff --git a/apps/web/app/app.dub.co/embed/utils.ts b/apps/web/app/app.dub.co/embed/utils.ts
new file mode 100644
index 0000000000..2468bbfd71
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/utils.ts
@@ -0,0 +1,37 @@
+import { prisma } from "@/lib/prisma";
+import { notFound } from "next/navigation";
+
+export const getLinkAndProgram = async (linkToken: string) => {
+ const token = await prisma.embedPublicToken.findUnique({
+ where: {
+ publicToken: linkToken,
+ expires: {
+ gt: new Date(),
+ },
+ },
+ });
+
+ if (!token) {
+ notFound();
+ }
+
+ const linkAndProgram = await prisma.link.findUnique({
+ where: {
+ id: token.linkId,
+ },
+ include: {
+ program: true,
+ },
+ });
+
+ if (!linkAndProgram) {
+ notFound();
+ }
+
+ const { program, ...link } = linkAndProgram;
+
+ return {
+ program,
+ link,
+ };
+};
diff --git a/apps/web/app/app.dub.co/embed/widget/page-client.tsx b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
index 8ec75071d2..f62a287e65 100644
--- a/apps/web/app/app.dub.co/embed/widget/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
@@ -10,35 +10,33 @@ import {
} from "@dub/ui";
import { GiftFill } from "@dub/ui/src/icons";
import { currencyFormatter, getPrettyUrl } from "@dub/utils";
-import { useEffect, useState } from "react";
-import { useReferralLink } from "../use-referral-link";
-import { useReferralProgram } from "../use-referral-program";
+import { Link, Program } from "@prisma/client";
+import { useState } from "react";
type Tab = "invite" | "rewards";
-export function EmbedWidgetPageClient() {
+export function EmbedWidgetPageClient({
+ program,
+ link,
+}: {
+ program: Program;
+ link: Link;
+}) {
const [copied, copyToClipboard] = useCopyToClipboard();
const [selectedTab, setSelectedTab] = useState<Tab>("invite");
- const { program } = useReferralProgram();
- const {
- link,
- isLoading: isLoadingLink,
- error: linkError,
- } = useReferralLink();
-
- useEffect(() => {
- if (linkError) {
- window.parent.postMessage(
- {
- originator: "Dub",
- event: "ERROR",
- data: linkError.info,
- },
- "*",
- );
- }
- }, [linkError]);
+ // useEffect(() => {
+ // if (linkError) {
+ // window.parent.postMessage(
+ // {
+ // originator: "Dub",
+ // event: "ERROR",
+ // data: linkError.info,
+ // },
+ // "*",
+ // );
+ // }
+ // }, [linkError]);
return (
<div>
@@ -74,16 +72,13 @@ export function EmbedWidgetPageClient() {
<>
<div className="flex flex-col gap-2">
<span className="text-sm text-neutral-500">Invite link</span>
- {!isLoadingLink && link ? (
- <input
- type="text"
- readOnly
- value={getPrettyUrl(link.shortLink)}
- className="xs:w-auto h-10 w-full rounded-md border border-neutral-300 px-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 lg:min-w-64 xl:min-w-72"
- />
- ) : (
- <div className="h-10 animate-pulse rounded-md bg-neutral-200" />
- )}
+ <input
+ type="text"
+ readOnly
+ value={getPrettyUrl(link.shortLink)}
+ className="xs:w-auto h-10 w-full rounded-md border border-neutral-300 px-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 lg:min-w-64 xl:min-w-72"
+ />
+
<Button
icon={
copied ? (
@@ -93,8 +88,7 @@ export function EmbedWidgetPageClient() {
)
}
text={copied ? "Copied link" : "Copy link"}
- onClick={() => copyToClipboard(getPrettyUrl(link?.shortLink))}
- disabled={isLoadingLink}
+ onClick={() => copyToClipboard(getPrettyUrl(link.shortLink))}
/>
</div>
</>
@@ -103,46 +97,29 @@ export function EmbedWidgetPageClient() {
{selectedTab === "rewards" && (
<>
<h2 className="text-lg font-bold">Rewards earned</h2>
- {isLoadingLink || !link ? (
- <div className="mt-4 grid grid-cols-3 gap-4">
- <div className="flex animate-pulse flex-col gap-2 rounded-lg bg-gray-200 p-3 shadow-sm">
- <span className="h-4 w-1/2 rounded bg-gray-300"></span>
- <span className="h-6 w-3/4 rounded bg-gray-300"></span>
- </div>
- <div className="flex animate-pulse flex-col gap-2 rounded-lg bg-gray-200 p-3 shadow-sm">
- <span className="h-4 w-1/2 rounded bg-gray-300"></span>
- <span className="h-6 w-3/4 rounded bg-gray-300"></span>
- </div>
- <div className="flex animate-pulse flex-col gap-2 rounded-lg bg-gray-200 p-3 shadow-sm">
- <span className="h-4 w-1/2 rounded bg-gray-300"></span>
- <span className="h-6 w-3/4 rounded bg-gray-300"></span>
- </div>
+ <div className="mt-4 grid grid-cols-3 gap-4">
+ <div className="flex flex-col gap-2 rounded-lg bg-gray-100 p-3 shadow-sm">
+ <span className="text-sm text-gray-600">Clicks</span>
+ <span className="text-lg font-medium text-gray-500">
+ {link.clicks}
+ </span>
</div>
- ) : (
- <div className="mt-4 grid grid-cols-3 gap-4">
- <div className="flex flex-col gap-2 rounded-lg bg-gray-100 p-3 shadow-sm">
- <span className="text-sm text-gray-600">Clicks</span>
- <span className="text-lg font-medium text-gray-500">
- {link.clicks}
- </span>
- </div>
- <div className="flex flex-col gap-2 rounded-lg bg-gray-100 p-3 shadow-sm">
- <span className="text-sm text-gray-600">Signups</span>
- <span className="text-lg font-medium text-gray-500">
- {link.leads}
- </span>
- </div>
- <div className="flex flex-col gap-2 rounded-lg bg-gray-100 p-3 shadow-sm">
- <span className="text-sm text-gray-600">Total earned</span>
- <span className="text-lg font-medium text-gray-500">
- {currencyFormatter(link.saleAmount / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- </div>
+ <div className="flex flex-col gap-2 rounded-lg bg-gray-100 p-3 shadow-sm">
+ <span className="text-sm text-gray-600">Signups</span>
+ <span className="text-lg font-medium text-gray-500">
+ {link.leads}
+ </span>
</div>
- )}
+ <div className="flex flex-col gap-2 rounded-lg bg-gray-100 p-3 shadow-sm">
+ <span className="text-sm text-gray-600">Total earned</span>
+ <span className="text-lg font-medium text-gray-500">
+ {currencyFormatter(link.saleAmount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })}
+ </span>
+ </div>
+ </div>
</>
)}
diff --git a/apps/web/app/app.dub.co/embed/widget/page.tsx b/apps/web/app/app.dub.co/embed/widget/page.tsx
index c62d4b5722..9d5956ab2b 100644
--- a/apps/web/app/app.dub.co/embed/widget/page.tsx
+++ b/apps/web/app/app.dub.co/embed/widget/page.tsx
@@ -1,5 +1,14 @@
+import { getLinkAndProgram } from "../utils";
import { EmbedWidgetPageClient } from "./page-client";
-export default async function EmbedWidgetPage() {
- return <EmbedWidgetPageClient />;
+export default async function EmbedWidgetPage({
+ searchParams,
+}: {
+ searchParams: { token: string };
+}) {
+ const { token } = searchParams;
+
+ const { link, program } = await getLinkAndProgram(token);
+
+ return <EmbedWidgetPageClient program={program} link={link} />;
}