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 useProgram hook

Steven Tey963d2fd5678e
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/auth.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/auth.tsx
new file mode 100644
index 0000000000..940eb484b8
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/auth.tsx
@@ -0,0 +1,21 @@
+"use client";
+
+import useProgram from "@/lib/swr/use-program";
+import LayoutLoader from "@/ui/layout/layout-loader";
+import { redirect, useParams } from "next/navigation";
+import { ReactNode } from "react";
+
+export default function ProgramAuth({ children }: { children: ReactNode }) {
+ const { slug } = useParams();
+ const { loading, error } = useProgram();
+
+ if (loading) {
+ return <LayoutLoader />;
+ }
+
+ if (error && error.status === 404) {
+ redirect(`/${slug}`);
+ }
+
+ return children;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/layout.tsx
new file mode 100644
index 0000000000..2f28f09211
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/layout.tsx
@@ -0,0 +1,6 @@
+import { ReactNode } from "react";
+import ProgramAuth from "./auth";
+
+export default function ProgramLayout({ children }: { children: ReactNode }) {
+ return <ProgramAuth>{children}</ProgramAuth>;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/overview.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/overview.tsx
deleted file mode 100644
index 970461b854..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/overview.tsx
+++ /dev/null
@@ -1,104 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import { Button } from "@dub/ui";
-import { Program } from "@prisma/client";
-import { notFound } from "next/navigation";
-
-export async function ProgramOverview({ programId }: { programId: string }) {
- // TODO:
- // Check program is owned by the partner
-
- const program = await prisma.program.findUnique({
- where: {
- id: programId,
- },
- });
-
- if (!program) {
- notFound();
- }
-
- return (
- <div className="flex flex-col divide-y divide-neutral-200 rounded-md border border-neutral-200 bg-[#f9f9f9]">
- <div className="flex items-center justify-between px-4 py-4">
- <div className="flex flex-col gap-1">
- <span className="text-xs font-medium text-neutral-500">
- Sign up page
- </span>
- <span className="text-sm font-medium leading-none text-neutral-900">
- partner.dub.co/{program?.slug}
- </span>
- </div>
-
- <div className="flex items-center justify-between gap-5 divide-x divide-neutral-200">
- <div className="flex flex-col gap-1 text-right">
- <span className="text-xs font-medium text-neutral-500">
- Commission
- </span>
- <span className="text-sm font-medium leading-none text-neutral-900">
- {program.commissionAmount}
- {program.commissionType === "percentage" ? "%" : "$"}
- </span>
- </div>
-
- <div className="flex flex-col gap-1 pl-5 text-right">
- <span className="text-xs font-medium text-neutral-500">
- Cookie length
- </span>
- <span className="text-sm font-medium leading-none text-neutral-900">
- {program.cookieLength} days
- </span>
- </div>
-
- <div className="flex flex-col gap-1 pl-5 text-right">
- <span className="text-xs font-medium text-neutral-500">
- Min payout
- </span>
- <span className="text-sm font-medium leading-none text-neutral-900">
- ${program.minimumPayout}
- </span>
- </div>
- </div>
- </div>
-
- <div className="flex flex-wrap items-center justify-center px-4 py-4">
- <div
- className="flex-1 text-sm leading-none text-neutral-900"
- dangerouslySetInnerHTML={{
- __html: commissionDescription(program),
- }}
- />
- <Button className="h-8 w-fit" text="Edit program" />
- </div>
- </div>
- );
-}
-
-const commissionDescription = (program: Program) => {
- const texts = ["Earn"];
-
- if (program.commissionType === "flat") {
- texts.push(
- `<span class="font-semibold text-blue-600">${program.commissionAmount}</span>`,
- );
- } else {
- texts.push(
- `<span class="font-semibold text-blue-600">${program.commissionAmount}%</span>`,
- );
- }
-
- texts.push("for each conversion");
-
- if (program.recurringCommission) {
- if (program.isLifetimeRecurring) {
- texts.push(
- "and again for all future renewals throughout <span class='font-semibold'>the customer's lifetime</span>",
- );
- } else {
- texts.push(
- `and again for all renewals during the first <span class='font-semibold'>${program.recurringDuration} months</span>`,
- );
- }
- }
-
- return `${texts.join(" ")}.`;
-};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/page.tsx
deleted file mode 100644
index fa8f757ab9..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/page.tsx
+++ /dev/null
@@ -1,34 +0,0 @@
-import { PageContent } from "@/ui/layout/page-content";
-import { MaxWidthWrapper } from "@dub/ui";
-import { ProgramOverview } from "./overview";
-import { PendingPartners } from "./pending-partners";
-import { PendingPayouts } from "./pending-payouts";
-
-export default async function ProgramOverviewPage({
- params,
-}: {
- params: { slug: string; programId: string };
-}) {
- const { programId } = params;
-
- // TODO:
- // Add suspense loader
-
- return (
- <PageContent title="Overview">
- <MaxWidthWrapper>
- <div className="mt-8 space-y-10">
- <ProgramOverview programId={programId} />
- <div className="flex flex-col gap-10 sm:flex-row">
- <div className="basis-1/2">
- <PendingPartners />
- </div>
- <div className="basis-1/2">
- <PendingPayouts />
- </div>
- </div>
- </div>
- </MaxWidthWrapper>
- </PageContent>
- );
-}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/pending-partners.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/pending-partners.tsx
deleted file mode 100644
index 6a45d4f1b1..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/pending-partners.tsx
+++ /dev/null
@@ -1,34 +0,0 @@
-"use client";
-
-import useWorkspace from "@/lib/swr/use-workspace";
-import { buttonVariants } from "@dub/ui";
-import { cn } from "@dub/utils";
-import Link from "next/link";
-import { useParams } from "next/navigation";
-
-export function PendingPartners() {
- const { slug } = useWorkspace();
- const { programId } = useParams();
-
- return (
- <>
- <div className="mb-4 flex items-center justify-between">
- <h2 className="text-base font-semibold text-neutral-900">
- Pending partners
- </h2>
-
- <Link
- href={`/${slug}/programs/${programId}/partners`}
- className={cn(
- buttonVariants(),
- "flex h-8 items-center rounded-lg border px-3 text-sm",
- )}
- >
- View all
- </Link>
- </div>
-
- <div className="min-h-[200px] rounded-md border"></div>
- </>
- );
-}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/pending-payouts.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/pending-payouts.tsx
deleted file mode 100644
index def8daedf4..0000000000
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview/pending-payouts.tsx
+++ /dev/null
@@ -1,34 +0,0 @@
-"use client";
-
-import useWorkspace from "@/lib/swr/use-workspace";
-import { buttonVariants } from "@dub/ui";
-import { cn } from "@dub/utils";
-import Link from "next/link";
-import { useParams } from "next/navigation";
-
-export function PendingPayouts() {
- const { slug } = useWorkspace();
- const { programId } = useParams();
-
- return (
- <>
- <div className="mb-4 flex items-center justify-between">
- <h2 className="text-base font-semibold text-neutral-900">
- Pending payouts
- </h2>
-
- <Link
- href={`/${slug}/programs/${programId}/payouts`}
- className={cn(
- buttonVariants(),
- "flex h-8 items-center rounded-lg border px-3 text-sm",
- )}
- >
- View all
- </Link>
- </div>
-
- <div className="min-h-[200px] rounded-md border"></div>
- </>
- );
-}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx
new file mode 100644
index 0000000000..573c418692
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx
@@ -0,0 +1,144 @@
+"use client";
+
+import useProgram from "@/lib/swr/use-program";
+import { ProgramProps } from "@/lib/types";
+import { Button, buttonVariants } from "@dub/ui";
+import { cn } from "@dub/utils";
+import Link from "next/link";
+import { redirect, useParams } from "next/navigation";
+
+export default function ProgramOverviewPageClient() {
+ const { slug, programId } = useParams();
+ const { program } = useProgram();
+
+ if (!program) {
+ redirect(`/${slug}`);
+ }
+
+ return (
+ <div className="mt-8 space-y-10">
+ <div className="flex flex-col divide-y divide-neutral-200 rounded-md border border-neutral-200 bg-[#f9f9f9]">
+ <div className="flex items-center justify-between px-4 py-4">
+ <div className="flex flex-col gap-1">
+ <span className="text-xs font-medium text-neutral-500">
+ Sign up page
+ </span>
+ <span className="text-sm font-medium leading-none text-neutral-900">
+ partner.dub.co/{program?.slug}
+ </span>
+ </div>
+
+ <div className="flex items-center justify-between gap-5 divide-x divide-neutral-200">
+ <div className="flex flex-col gap-1 text-right">
+ <span className="text-xs font-medium text-neutral-500">
+ Commission
+ </span>
+ <span className="text-sm font-medium leading-none text-neutral-900">
+ {program.commissionAmount}
+ {program.commissionType === "percentage" ? "%" : "$"}
+ </span>
+ </div>
+
+ <div className="flex flex-col gap-1 pl-5 text-right">
+ <span className="text-xs font-medium text-neutral-500">
+ Cookie length
+ </span>
+ <span className="text-sm font-medium leading-none text-neutral-900">
+ {program.cookieLength} days
+ </span>
+ </div>
+
+ <div className="flex flex-col gap-1 pl-5 text-right">
+ <span className="text-xs font-medium text-neutral-500">
+ Min payout
+ </span>
+ <span className="text-sm font-medium leading-none text-neutral-900">
+ ${program.minimumPayout}
+ </span>
+ </div>
+ </div>
+ </div>
+
+ <div className="flex flex-wrap items-center justify-center px-4 py-4">
+ <div
+ className="flex-1 text-sm leading-none text-neutral-900"
+ dangerouslySetInnerHTML={{
+ __html: commissionDescription(program),
+ }}
+ />
+ <Button className="h-8 w-fit" text="Edit program" />
+ </div>
+ </div>
+ <div className="flex flex-col gap-10 sm:flex-row">
+ <div className="basis-1/2">
+ <div className="mb-4 flex items-center justify-between">
+ <h2 className="text-base font-semibold text-neutral-900">
+ Pending partners
+ </h2>
+
+ <Link
+ href={`/${slug}/programs/${programId}/partners`}
+ className={cn(
+ buttonVariants(),
+ "flex h-8 items-center rounded-lg border px-3 text-sm",
+ )}
+ >
+ View all
+ </Link>
+ </div>
+
+ <div className="min-h-[200px] rounded-md border"></div>
+ </div>
+ <div className="basis-1/2">
+ <div className="mb-4 flex items-center justify-between">
+ <h2 className="text-base font-semibold text-neutral-900">
+ Pending payouts
+ </h2>
+
+ <Link
+ href={`/${slug}/programs/${programId}/payouts`}
+ className={cn(
+ buttonVariants(),
+ "flex h-8 items-center rounded-lg border px-3 text-sm",
+ )}
+ >
+ View all
+ </Link>
+ </div>
+
+ <div className="min-h-[200px] rounded-md border"></div>
+ </div>
+ </div>
+ </div>
+ );
+}
+
+const commissionDescription = (program: ProgramProps) => {
+ const texts = ["Earn"];
+
+ if (program.commissionType === "flat") {
+ texts.push(
+ `<span class="font-semibold text-blue-600">${program.commissionAmount}</span>`,
+ );
+ } else {
+ texts.push(
+ `<span class="font-semibold text-blue-600">${program.commissionAmount}%</span>`,
+ );
+ }
+
+ texts.push("for each conversion");
+
+ if (program.recurringCommission) {
+ if (program.isLifetimeRecurring) {
+ texts.push(
+ "and again for all future renewals throughout <span class='font-semibold'>the customer's lifetime</span>",
+ );
+ } else {
+ texts.push(
+ `and again for all renewals during the first <span class='font-semibold'>${program.recurringDuration} months</span>`,
+ );
+ }
+ }
+
+ return `${texts.join(" ")}.`;
+};
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page.tsx
index 6f1a768592..95eb01b6af 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page.tsx
@@ -1,10 +1,13 @@
-import { redirect } from "next/navigation";
+import { PageContent } from "@/ui/layout/page-content";
+import { MaxWidthWrapper } from "@dub/ui";
+import ProgramOverviewPageClient from "./page-client";
-export default function ProgramInfo({
- params,
-}: {
- params: { slug: string; programId: string };
-}) {
- redirect(`/${params.slug}/programs/${params.programId}/overview`);
- return null;
+export default async function ProgramOverviewPage() {
+ return (
+ <PageContent title="Overview">
+ <MaxWidthWrapper>
+ <ProgramOverviewPageClient />
+ </MaxWidthWrapper>
+ </PageContent>
+ );
}
diff --git a/apps/web/lib/api/programs/get-program.ts b/apps/web/lib/api/programs/get-program.ts
index 1f1da93dcf..91de52a8df 100644
--- a/apps/web/lib/api/programs/get-program.ts
+++ b/apps/web/lib/api/programs/get-program.ts
@@ -1,5 +1,6 @@
import { prisma } from "@/lib/prisma";
import { ProgramSchema } from "@/lib/zod/schemas/partners";
+import { DubApiError } from "../errors";
export const getProgramOrThrow = async ({
workspaceId,
@@ -8,12 +9,19 @@ export const getProgramOrThrow = async ({
workspaceId: string;
programId: string;
}) => {
- const program = await prisma.program.findUniqueOrThrow({
+ const program = await prisma.program.findUnique({
where: {
id: programId,
workspaceId,
},
});
+ if (!program) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Program not found",
+ });
+ }
+
return ProgramSchema.parse(program);
};
diff --git a/apps/web/lib/swr/use-program-enrollment.ts b/apps/web/lib/swr/use-program-enrollment.ts
index dd44b889c5..7725f6e568 100644
--- a/apps/web/lib/swr/use-program-enrollment.ts
+++ b/apps/web/lib/swr/use-program-enrollment.ts
@@ -13,7 +13,7 @@ export default function useProgramEnrollment() {
dedupingInterval: 60000,
},
);
- programEnrollment;
+
return {
programEnrollment,
error,
diff --git a/apps/web/lib/swr/use-program.ts b/apps/web/lib/swr/use-program.ts
new file mode 100644
index 0000000000..e5ceeafe4a
--- /dev/null
+++ b/apps/web/lib/swr/use-program.ts
@@ -0,0 +1,24 @@
+import { fetcher } from "@dub/utils";
+import { useParams } from "next/navigation";
+import useSWR from "swr";
+import { ProgramProps } from "../types";
+import useWorkspace from "./use-workspace";
+
+export default function useProgram() {
+ const { id: workspaceId } = useWorkspace();
+ const { programId } = useParams();
+
+ const { data: program, error } = useSWR<ProgramProps>(
+ `/api/programs/${programId}?workspaceId=${workspaceId}`,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
+
+ return {
+ program,
+ error,
+ loading: programId && !program && !error ? true : false,
+ };
+}
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index dc7cd0c703..d62d9f88b5 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -45,7 +45,7 @@ export const ProgramSchema = z.object({
recurringCommission: z.boolean(),
recurringDuration: z.number().nullable(),
recurringInterval: z.nativeEnum(CommissionInterval).nullable(),
- isLifetimeRecurring: z.boolean(),
+ isLifetimeRecurring: z.boolean().nullable(),
createdAt: z.date(),
updatedAt: z.date(),
});
diff --git a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
index 891f5607f8..ab11c3d198 100644
--- a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
@@ -76,7 +76,7 @@ const NAV_AREAS: SidebarNavAreas<{
items: [
{
name: "Overview",
- href: `/${slug}/programs/${programs[0].id}/overview`,
+ href: `/${slug}/programs/${programs[0].id}`,
},
{
name: "Partners",