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.

Pull out the common components to `@/ui/partners`

Kiran Kfeb1f7fd619b
diff --git a/apps/web/app/app.dub.co/embed/context.tsx b/apps/web/app/app.dub.co/embed/context.tsx
deleted file mode 100644
index 6e3ebfd46b..0000000000
--- a/apps/web/app/app.dub.co/embed/context.tsx
+++ /dev/null
@@ -1,11 +0,0 @@
-"use client";
-
-import { IntervalOptions } from "@/lib/analytics/types";
-import { createContext } from "react";
-
-export const ProgramOverviewContext = createContext<{
- start?: Date;
- end?: Date;
- interval?: IntervalOptions;
- color?: string;
-}>({});
diff --git a/apps/web/app/app.dub.co/embed/page-client.tsx b/apps/web/app/app.dub.co/embed/page-client.tsx
index b5be0121d2..8fc8f179f1 100644
--- a/apps/web/app/app.dub.co/embed/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/page-client.tsx
@@ -1,15 +1,19 @@
"use client";
import { IntervalOptions } from "@/lib/analytics/types";
+import {
+ EarningsChart,
+ ProgramOverviewContext,
+} from "@/ui/partners/earnings-chart";
import { HeroBackground } from "@/ui/partners/hero-background";
import { ProgramCommissionDescription } from "@/ui/partners/program-commission-description";
+import { StatCard } from "@/ui/partners/stat-card";
import { Button, Check2, useCopyToClipboard, useRouterStuff } from "@dub/ui";
import { Copy, MoneyBill2 } from "@dub/ui/src/icons";
import { getPrettyUrl } from "@dub/utils";
-import { ProgramOverviewContext } from "./context";
-import { EarningsChart } from "./earnings-chart";
+import { useContext } from "react";
import { SaleTable } from "./sale-table";
-import { StatCard } from "./stat-card";
+import useReferralAnalytics from "./use-referral-analytics";
import { useReferralLink } from "./use-referral-link";
import { useReferralProgram } from "./use-referral-program";
@@ -84,13 +88,13 @@ export function ReferralsEmbedPageClient() {
>
<div className="mt-6 rounded-lg border border-neutral-300">
<div className="p-4 md:p-6 md:pb-4">
- <EarningsChart />
+ <EarningsChartContainer />
</div>
</div>
<div className="mt-6 grid grid-cols-[minmax(0,1fr)] gap-4 sm:grid-cols-3">
- <StatCard title="Clicks" event="clicks" />
- <StatCard title="Leads" event="leads" />
- <StatCard title="Sales" event="sales" />
+ <StatCardContainer title="Clicks" event="clicks" />
+ <StatCardContainer title="Leads" event="leads" />
+ <StatCardContainer title="Sales" event="sales" />
</div>
<div className="mt-6">
<div className="flex items-center justify-between">
@@ -106,3 +110,64 @@ export function ReferralsEmbedPageClient() {
</>
);
}
+
+const EarningsChartContainer = () => {
+ const { start, end, interval, color } = useContext(ProgramOverviewContext);
+
+ const { data: { earnings: total } = {} } = useReferralAnalytics({
+ interval,
+ start,
+ end,
+ });
+
+ const { data: timeseries, error } = useReferralAnalytics({
+ groupBy: "timeseries",
+ interval,
+ start,
+ end,
+ });
+
+ return (
+ <EarningsChart
+ timeseries={timeseries}
+ total={total}
+ color={color}
+ error={error}
+ />
+ );
+};
+
+function StatCardContainer({
+ title,
+ event,
+}: {
+ title: string;
+ event: "clicks" | "leads" | "sales";
+}) {
+ const { start, end, interval, color } = useContext(ProgramOverviewContext);
+
+ const { data: total } = useReferralAnalytics({
+ interval,
+ start,
+ end,
+ });
+
+ const { data: timeseries, error } = useReferralAnalytics({
+ groupBy: "timeseries",
+ interval,
+ start,
+ end,
+ event,
+ });
+
+ return (
+ <StatCard
+ timeseries={timeseries}
+ total={total}
+ error={error}
+ title={title}
+ event={event}
+ color={color}
+ />
+ );
+}
diff --git a/apps/web/app/app.dub.co/embed/stat-card.tsx b/apps/web/app/app.dub.co/embed/stat-card.tsx
deleted file mode 100644
index c07c7e5dd7..0000000000
--- a/apps/web/app/app.dub.co/embed/stat-card.tsx
+++ /dev/null
@@ -1,67 +0,0 @@
-"use client";
-
-import { MiniAreaChart } from "@dub/ui";
-import { LoadingSpinner } from "@dub/ui/src/icons";
-import { nFormatter } from "@dub/utils";
-import { useContext } from "react";
-import { ProgramOverviewContext } from "./context";
-import useReferralAnalytics from "./use-referral-analytics";
-
-export function StatCard({
- title,
- event,
-}: {
- title: string;
- event: "clicks" | "leads" | "sales";
-}) {
- const { start, end, interval, color } = useContext(ProgramOverviewContext);
-
- const { data: total } = useReferralAnalytics({
- interval,
- start,
- end,
- });
-
- const { data: timeseries, error } = useReferralAnalytics({
- groupBy: "timeseries",
- interval,
- start,
- end,
- event,
- });
-
- return (
- <div className="hover:drop-shadow-card-hover block rounded-md border border-neutral-300 bg-white p-5 transition-[filter]">
- <span className="block text-sm text-neutral-500">{title}</span>
- {total !== undefined ? (
- <span className="block text-2xl text-neutral-800">
- {nFormatter(total[event])}
- </span>
- ) : (
- <div className="h-8 w-16 animate-pulse rounded-md bg-neutral-200" />
- )}
- <div className="mt-2 h-16 w-full">
- {timeseries ? (
- <MiniAreaChart
- data={timeseries.map((d) => ({
- date: new Date(d.start),
- value: d[event],
- }))}
- curve={false}
- color={color}
- />
- ) : (
- <div className="flex size-full items-center justify-center">
- {error ? (
- <span className="text-sm text-neutral-500">
- Failed to load data.
- </span>
- ) : (
- <LoadingSpinner />
- )}
- </div>
- )}
- </div>
- </div>
- );
-}
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx
index 4a803c2dfc..c728ad6493 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx
@@ -3,44 +3,28 @@
import { IntervalOptions } from "@/lib/analytics/types";
import usePartnerAnalytics from "@/lib/swr/use-partner-analytics";
import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
-import Areas from "@/ui/charts/areas";
-import { ChartContext } from "@/ui/charts/chart-context";
-import TimeSeriesChart from "@/ui/charts/time-series-chart";
-import XAxis from "@/ui/charts/x-axis";
-import YAxis from "@/ui/charts/y-axis";
+import {
+ EarningsChart,
+ ProgramOverviewContext,
+} from "@/ui/partners/earnings-chart";
import { HeroBackground } from "@/ui/partners/hero-background";
import { ProgramCommissionDescription } from "@/ui/partners/program-commission-description";
-import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
+import { StatCard } from "@/ui/partners/stat-card";
import {
Button,
buttonVariants,
Check2,
MaxWidthWrapper,
- MiniAreaChart,
useCopyToClipboard,
useRouterStuff,
} from "@dub/ui";
-import { Copy, LoadingSpinner, MoneyBill2 } from "@dub/ui/src/icons";
-import {
- cn,
- currencyFormatter,
- formatDate,
- getPrettyUrl,
- nFormatter,
-} from "@dub/utils";
-import { LinearGradient } from "@visx/gradient";
+import { Copy, MoneyBill2 } from "@dub/ui/src/icons";
+import { cn, getPrettyUrl } from "@dub/utils";
import Link from "next/link";
import { useParams } from "next/navigation";
-import { createContext, useContext, useId, useMemo } from "react";
+import { useContext } from "react";
import { SaleTablePartner } from "./sales/sale-table";
-const ProgramOverviewContext = createContext<{
- start?: Date;
- end?: Date;
- interval?: IntervalOptions;
- color?: string;
-}>({});
-
export default function ProgramPageClient() {
const { getQueryString, searchParamsObj } = useRouterStuff();
const { partnerId, programId } = useParams();
@@ -117,13 +101,13 @@ export default function ProgramPageClient() {
>
<div className="mt-6 rounded-lg border border-neutral-300">
<div className="p-4 md:p-6 md:pb-4">
- <EarningsChart />
+ <EarningsChartContainer />
</div>
</div>
<div className="mt-6 grid grid-cols-[minmax(0,1fr)] gap-4 sm:grid-cols-3">
- <StatCard title="Clicks" event="clicks" />
- <StatCard title="Leads" event="leads" />
- <StatCard title="Sales" event="sales" />
+ <StatCardContainer title="Clicks" event="clicks" />
+ <StatCardContainer title="Leads" event="leads" />
+ <StatCardContainer title="Sales" event="sales" />
</div>
<div className="mt-6">
<div className="flex items-center justify-between">
@@ -149,9 +133,7 @@ export default function ProgramPageClient() {
);
}
-function EarningsChart() {
- const id = useId();
-
+function EarningsChartContainer() {
const { start, end, interval, color } = useContext(ProgramOverviewContext);
const { data: { earnings: total } = {} } = usePartnerAnalytics({
@@ -159,6 +141,7 @@ function EarningsChart() {
start,
end,
});
+
const { data: timeseries, error } = usePartnerAnalytics({
groupBy: "timeseries",
interval,
@@ -166,120 +149,17 @@ function EarningsChart() {
end,
});
- const data = useMemo(
- () =>
- timeseries?.map(({ start, earnings }) => ({
- date: new Date(start),
- values: { earnings: earnings / 100 },
- })),
- [timeseries],
- );
-
return (
- <div>
- <div className="flex flex-col-reverse items-start justify-between gap-4 md:flex-row">
- <div>
- <span className="block text-sm text-neutral-500">Earnings</span>
- <div className="mt-1.5">
- {total !== undefined ? (
- <span className="text-2xl leading-none text-neutral-800">
- {currencyFormatter(total / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- ) : (
- <div className="h-7 w-24 animate-pulse rounded-md bg-neutral-200" />
- )}
- </div>
- </div>
- <div className="w-full md:w-auto">
- <SimpleDateRangePicker className="h-8 w-full md:w-fit" />
- </div>
- </div>
- <div className="relative mt-4 h-64 w-full">
- {data ? (
- <TimeSeriesChart
- data={data}
- series={[
- {
- id: "earnings",
- valueAccessor: (d) => d.values.earnings,
- colorClassName: color ? `text-[${color}]` : "text-violet-500",
- isActive: true,
- },
- ]}
- tooltipClassName="p-0"
- tooltipContent={(d) => {
- return (
- <>
- <p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
- {formatDate(d.date)}
- </p>
- <div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
- <div className="flex items-center gap-2">
- <div
- className={cn(
- "h-2 w-2 rounded-sm shadow-[inset_0_0_0_1px_#0003]",
- color ? `bg-[${color}]` : "bg-violet-500",
- )}
- />
- <p className="capitalize text-gray-600">Earnings</p>
- </div>
- <p className="text-right font-medium text-gray-900">
- {currencyFormatter(d.values.earnings, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </p>
- </div>
- </>
- );
- }}
- >
- <ChartContext.Consumer>
- {(context) => (
- <LinearGradient
- id={`${id}-color-gradient`}
- from={color || "#7D3AEC"}
- to={color || "#DA2778"}
- x1={0}
- x2={context?.width ?? 1}
- gradientUnits="userSpaceOnUse"
- />
- )}
- </ChartContext.Consumer>
-
- <XAxis />
- <YAxis showGridLines />
- <Areas
- seriesStyles={[
- {
- id: "earnings",
- areaFill: `url(#${id}-color-gradient)`,
- lineStroke: `url(#${id}-color-gradient)`,
- lineClassName: `text-[${color}]`,
- },
- ]}
- />
- </TimeSeriesChart>
- ) : (
- <div className="flex size-full items-center justify-center">
- {error ? (
- <span className="text-sm text-neutral-500">
- Failed to load earnings data.
- </span>
- ) : (
- <LoadingSpinner />
- )}
- </div>
- )}
- </div>
- </div>
+ <EarningsChart
+ timeseries={timeseries}
+ total={total}
+ color={color}
+ error={error}
+ />
);
}
-function StatCard({
+function StatCardContainer({
title,
event,
}: {
@@ -295,6 +175,7 @@ function StatCard({
start,
end,
});
+
const { data: timeseries, error } = usePartnerAnalytics({
groupBy: "timeseries",
interval,
@@ -304,45 +185,14 @@ function StatCard({
});
return (
- <Link
+ <StatCard
+ timeseries={timeseries}
+ total={total}
+ error={error}
+ title={title}
+ event={event}
+ color={color}
href={`/${partnerId}/${programId}/analytics?event=${event}${getQueryString()?.replace("?", "&")}`}
- className="hover:drop-shadow-card-hover block rounded-md border border-neutral-300 bg-white p-5 transition-[filter]"
- >
- <span className="block text-sm text-neutral-500">{title}</span>
- {total !== undefined ? (
- <div className="flex items-center gap-1 text-2xl text-neutral-800">
- {nFormatter(total[event])}
- {event === "sales" && (
- <span className="text-base text-neutral-500">
- ({currencyFormatter(total.saleAmount / 100)})
- </span>
- )}
- </div>
- ) : (
- <div className="h-8 w-16 animate-pulse rounded-md bg-neutral-200" />
- )}
- <div className="mt-2 h-16 w-full">
- {timeseries ? (
- <MiniAreaChart
- data={timeseries.map((d) => ({
- date: new Date(d.start),
- value: d[event],
- }))}
- curve={false}
- color={color}
- />
- ) : (
- <div className="flex size-full items-center justify-center">
- {error ? (
- <span className="text-sm text-neutral-500">
- Failed to load data.
- </span>
- ) : (
- <LoadingSpinner />
- )}
- </div>
- )}
- </div>
- </Link>
+ />
);
}
diff --git a/apps/web/lib/middleware/partners.ts b/apps/web/lib/middleware/partners.ts
index 9c41dba4b8..77e76fc4cd 100644
--- a/apps/web/lib/middleware/partners.ts
+++ b/apps/web/lib/middleware/partners.ts
@@ -32,12 +32,14 @@ export default async function PartnersMiddleware(req: NextRequest) {
if (
user &&
partnersEnabled &&
- !["/account", "/pn_"].some((p) => path.startsWith(p))
+ !["/account", "/pn_", "/onboarding"].some((p) => path.startsWith(p))
) {
const defaultPartner = await getDefaultPartner(user);
+
if (!defaultPartner) {
return NextResponse.redirect(new URL("/onboarding", req.url));
}
+
return NextResponse.redirect(
new URL(`/${defaultPartner}${path}${searchParamsString}`, req.url),
);
diff --git a/apps/web/app/app.dub.co/embed/earnings-chart.tsx b/apps/web/ui/partners/earnings-chart.tsx
similarity index 89%
rename from apps/web/app/app.dub.co/embed/earnings-chart.tsx
rename to apps/web/ui/partners/earnings-chart.tsx
index b66be6e5a1..c7d28d8eb9 100644
--- a/apps/web/app/app.dub.co/embed/earnings-chart.tsx
+++ b/apps/web/ui/partners/earnings-chart.tsx
@@ -1,5 +1,6 @@
"use client";
+import { IntervalOptions } from "@/lib/analytics/types";
import Areas from "@/ui/charts/areas";
import { ChartContext } from "@/ui/charts/chart-context";
import TimeSeriesChart from "@/ui/charts/time-series-chart";
@@ -9,26 +10,29 @@ import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
import { LoadingSpinner } from "@dub/ui/src/icons";
import { cn, currencyFormatter, formatDate } from "@dub/utils";
import { LinearGradient } from "@visx/gradient";
-import { useContext, useId, useMemo } from "react";
-import { ProgramOverviewContext } from "./context";
-import useReferralAnalytics from "./use-referral-analytics";
+import { createContext, useId, useMemo } from "react";
-export function EarningsChart() {
- const id = useId();
- const { start, end, interval, color } = useContext(ProgramOverviewContext);
+export const ProgramOverviewContext = createContext<{
+ start?: Date;
+ end?: Date;
+ interval?: IntervalOptions;
+ color?: string;
+}>({});
- const { data: { earnings: total } = {} } = useReferralAnalytics({
- interval,
- start,
- end,
- });
+interface EarningsChartProps {
+ timeseries: any;
+ total: any;
+ color: any;
+ error: any;
+}
- const { data: timeseries, error } = useReferralAnalytics({
- groupBy: "timeseries",
- interval,
- start,
- end,
- });
+export function EarningsChart({
+ timeseries,
+ total,
+ color,
+ error,
+}: EarningsChartProps) {
+ const id = useId();
const data = useMemo(
() =>
diff --git a/apps/web/ui/partners/stat-card.tsx b/apps/web/ui/partners/stat-card.tsx
new file mode 100644
index 0000000000..295791fe99
--- /dev/null
+++ b/apps/web/ui/partners/stat-card.tsx
@@ -0,0 +1,90 @@
+import { MiniAreaChart } from "@dub/ui";
+import { LoadingSpinner } from "@dub/ui/src/icons";
+import { currencyFormatter, nFormatter } from "@dub/utils";
+import Link from "next/link";
+
+interface StatCardProps {
+ title: string;
+ event: "clicks" | "leads" | "sales";
+ timeseries?: any;
+ error?: any;
+ color?: string;
+ href?: string;
+ total?: {
+ clicks: number;
+ leads: number;
+ sales: number;
+ saleAmount: number;
+ };
+}
+
+export function StatCard({
+ title,
+ event,
+ total,
+ timeseries,
+ error,
+ color,
+ href,
+}: StatCardProps) {
+ return (
+ <Wrapper
+ href={href}
+ className="hover:drop-shadow-card-hover block rounded-md border border-neutral-300 bg-white p-5 transition-[filter]"
+ >
+ <span className="block text-sm text-neutral-500">{title}</span>
+ {total !== undefined ? (
+ <div className="flex items-center gap-1 text-2xl text-neutral-800">
+ {nFormatter(total[event])}
+ {event === "sales" && (
+ <span className="text-base text-neutral-500">
+ ({currencyFormatter(total.saleAmount / 100)})
+ </span>
+ )}
+ </div>
+ ) : (
+ <div className="h-8 w-16 animate-pulse rounded-md bg-neutral-200" />
+ )}
+ <div className="mt-2 h-16 w-full">
+ {timeseries ? (
+ <MiniAreaChart
+ data={timeseries.map((d) => ({
+ date: new Date(d.start),
+ value: d[event],
+ }))}
+ curve={false}
+ color={color}
+ />
+ ) : (
+ <div className="flex size-full items-center justify-center">
+ {error ? (
+ <span className="text-sm text-neutral-500">
+ Failed to load data.
+ </span>
+ ) : (
+ <LoadingSpinner />
+ )}
+ </div>
+ )}
+ </div>
+ </Wrapper>
+ );
+}
+
+const Wrapper = ({
+ href,
+ children,
+ className,
+}: {
+ href?: string;
+ children: React.ReactNode;
+ className?: string;
+}) => {
+ return href ? (
+ <Link href={href} className={className}>
+ {children}
+ </Link>
+ ) : (
+ <div className={className}>{children}</div>
+ );
+};