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 WIP program overview page w/ mock data

Tim Wilson233f4c829d82
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
new file mode 100644
index 0000000000..2c872ab464
--- /dev/null
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx
@@ -0,0 +1,327 @@
+"use client";
+
+import Areas from "@/ui/charts/areas";
+import TimeSeriesChart from "@/ui/charts/time-series-chart";
+import XAxis from "@/ui/charts/x-axis";
+import YAxis from "@/ui/charts/y-axis";
+import EmptyState from "@/ui/shared/empty-state";
+import { MiniAreaChart } from "@dub/blocks";
+import {
+ Button,
+ MaxWidthWrapper,
+ Table,
+ usePagination,
+ useTable,
+} from "@dub/ui";
+import { CircleDollar, Copy, LoadingSpinner } from "@dub/ui/src/icons";
+import { currencyFormatter, formatDate, nFormatter } from "@dub/utils";
+import { LinearGradient } from "@visx/gradient";
+import { subDays } from "date-fns";
+import { useId, useMemo } from "react";
+
+const mockValues = (factor = 1) => {
+ const timeseries = [...Array(30)].map((_, i) => ({
+ date: subDays(new Date(), 30 - i),
+ value: Math.floor(Math.random() * 100 * factor),
+ }));
+
+ return {
+ total: timeseries.reduce((acc, curr) => acc + curr.value, 0),
+ timeseries,
+ isError: false,
+ };
+};
+
+const mockSales = () =>
+ [...Array(10)].map((_, i) => ({
+ date: subDays(new Date(), 10 - i),
+ earnings: Math.floor(Math.random() * 15),
+ customer: `*****@${["dub.co", "gmail.com"][i % 2]}`,
+ }));
+
+export default function ProgramPageClient() {
+ const referralLink = "framer.link/steven"; // TODO: get from API
+
+ // TODO: get all of these values from API
+
+ const { total: earnings, timeseries: earningsTimeseries } = useMemo(
+ () => mockValues(100),
+ [],
+ );
+ const earningsError = false;
+
+ const { total: clicks, timeseries: clicksTimeseries } = useMemo(
+ () => mockValues(3),
+ [],
+ );
+ const { total: leads, timeseries: leadsTimeseries } = useMemo(
+ () => mockValues(1),
+ [],
+ );
+ const { total: sales, timeseries: salesTimeseries } = useMemo(
+ () => mockValues(0.25),
+ [],
+ );
+ const statsError = false;
+
+ return (
+ <MaxWidthWrapper className="pb-10">
+ <div className="flex flex-col items-start justify-between gap-4 rounded-lg border border-neutral-300 bg-neutral-50 p-4 sm:flex-row sm:items-center md:p-8">
+ <div>
+ <span className="block text-xl font-medium leading-none text-neutral-900">
+ Referral link
+ </span>
+ <p className="mt-2 text-sm text-neutral-500">
+ Track events and conversions when using your referral link
+ </p>
+ </div>
+ <div className="flex items-center gap-2">
+ {referralLink !== undefined ? (
+ <input
+ type="text"
+ readOnly
+ value={referralLink}
+ className="h-10 rounded-md border border-neutral-300 px-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 lg:min-w-72"
+ />
+ ) : (
+ <div className="h-10 w-16 animate-pulse rounded-md bg-neutral-200 lg:w-72" />
+ )}
+ <Button
+ icon={<Copy className="size-4" />}
+ text="Copy link"
+ className="w-fit"
+ />
+ </div>
+ </div>
+ <div className="mt-6 rounded-lg border border-neutral-300">
+ <div className="p-4 md:p-8 md:pb-4">
+ <EarningsChart
+ amount={earnings}
+ timeseries={earningsTimeseries}
+ error={Boolean(earningsError)}
+ />
+ </div>
+ </div>
+ <div className="mt-6 grid grid-cols-[minmax(0,1fr)] gap-4 sm:grid-cols-3">
+ <StatCard
+ title="Clicks"
+ value={clicks}
+ timeseries={clicksTimeseries}
+ error={Boolean(statsError)}
+ />
+ <StatCard
+ title="Leads"
+ value={leads}
+ timeseries={leadsTimeseries}
+ error={Boolean(statsError)}
+ />
+ <StatCard
+ title="Sales"
+ value={sales}
+ timeseries={salesTimeseries}
+ error={Boolean(statsError)}
+ />
+ </div>
+ <div className="mt-6">
+ <SalesTable />
+ </div>
+ </MaxWidthWrapper>
+ );
+}
+
+function EarningsChart({
+ amount,
+ timeseries,
+ error,
+}: {
+ amount?: number;
+ timeseries?: { date: Date; value: number }[];
+ error?: boolean;
+}) {
+ const id = useId();
+ const data = useMemo(
+ () => timeseries?.map(({ date, value }) => ({ date, values: { value } })),
+ [timeseries],
+ );
+
+ return (
+ <div>
+ <span className="block text-sm text-neutral-500">Earnings</span>
+ <div className="mt-2">
+ {amount !== undefined ? (
+ <span className="text-3xl text-neutral-800">
+ {currencyFormatter(amount / 100)}
+ </span>
+ ) : (
+ <div className="h-9 w-24 animate-pulse rounded-md bg-neutral-200" />
+ )}
+ </div>
+ <div className="relative h-64 w-full">
+ {data ? (
+ <TimeSeriesChart
+ data={data}
+ series={[
+ {
+ id: "value",
+ valueAccessor: (d) => d.values.value / 100,
+ colorClassName: "text-[#DA2778]",
+ 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="h-2 w-2 rounded-sm bg-violet-500 shadow-[inset_0_0_0_1px_#0003]" />
+ <p className="capitalize text-gray-600">Earnings</p>
+ </div>
+ <p className="text-right font-medium text-gray-900">
+ {currencyFormatter(d.values.value / 100)}
+ </p>
+ </div>
+ </>
+ );
+ }}
+ >
+ <LinearGradient
+ id={`${id}-color-gradient`}
+ from="#7D3AEC"
+ to="#DA2778"
+ x1={0}
+ x2={1}
+ />
+
+ <XAxis />
+ <YAxis showGridLines />
+ <Areas
+ seriesStyles={[
+ {
+ id: "value",
+ areaFill: `url(#${id}-color-gradient)`,
+ lineStroke: `url(#${id}-color-gradient)`,
+ lineClassName: "text-[#DA2778]",
+ },
+ ]}
+ />
+ </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>
+ );
+}
+
+function StatCard({
+ title,
+ value,
+ timeseries,
+ error,
+}: {
+ title: string;
+ value?: number;
+ timeseries?: { date: Date; value: number }[];
+ error?: boolean;
+}) {
+ return (
+ <div className="rounded-md border border-neutral-300 p-5">
+ <span className="block text-sm text-neutral-500">{title}</span>
+ {value !== undefined ? (
+ <span className="block text-2xl text-neutral-800">
+ {nFormatter(value)}
+ </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} curve={false} />
+ ) : (
+ <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>
+ );
+}
+
+function SalesTable() {
+ // TODO: get from API
+ const salesEvents = useMemo(() => mockSales(), []);
+ const totalSalesEvents = salesEvents.length;
+ const loading = false;
+ const error = false;
+
+ const { pagination, setPagination } = usePagination();
+
+ const { table, ...tableProps } = useTable({
+ data: salesEvents,
+ loading,
+ error: error ? "Failed to fetch sales events." : undefined,
+ columns: [
+ {
+ id: "date",
+ header: "Date",
+ accessorKey: "date",
+ cell: ({ row }) => {
+ return formatDate(row.original.date, { month: "short" });
+ },
+ },
+ {
+ id: "customer",
+ header: "Customer",
+ accessorKey: "customer",
+ },
+ {
+ id: "earned",
+ header: "Earned",
+ accessorKey: "earnings",
+ cell: ({ row }) => {
+ return currencyFormatter(row.original.earnings, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+ },
+ },
+ ],
+ pagination,
+ onPaginationChange: setPagination,
+ rowCount: totalSalesEvents,
+ emptyState: (
+ <EmptyState
+ icon={CircleDollar}
+ title="No sales recorded"
+ description={`Referral sales will appear here.`}
+ />
+ ),
+ resourceName: (plural) => `sale${plural ? "s" : ""}`,
+ });
+
+ return (
+ <Table
+ {...tableProps}
+ table={table}
+ containerClassName="border-neutral-300"
+ />
+ );
+}
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page.tsx
index a1c780d231..2ea72f54a4 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page.tsx
@@ -1,5 +1,10 @@
import { PageContent } from "@/ui/layout/page-content";
+import ProgramPageClient from "./page-client";
export default function ProgramPage() {
- return <PageContent title="Overview"></PageContent>;
+ return (
+ <PageContent title="Overview">
+ <ProgramPageClient />
+ </PageContent>
+ );
}
diff --git a/apps/web/ui/charts/areas.tsx b/apps/web/ui/charts/areas.tsx
index b468bea1d2..d547153f12 100644
--- a/apps/web/ui/charts/areas.tsx
+++ b/apps/web/ui/charts/areas.tsx
@@ -7,9 +7,15 @@ import { useMemo } from "react";
import { useChartContext, useChartTooltipContext } from "./chart-context";
export default function Areas({
- seriesClassNames,
+ seriesStyles,
}: {
- seriesClassNames?: { id: string; gradient?: string; line?: string }[];
+ seriesStyles?: {
+ id: string;
+ gradientClassName?: string;
+ lineClassName?: string;
+ areaFill?: string;
+ lineStroke?: string;
+ }[];
}) {
const { data, series, margin, xScale, yScale, startDate, endDate } =
useChartContext();
@@ -32,86 +38,97 @@ export default function Areas({
<AnimatePresence>
{series
.filter(({ isActive }) => isActive)
- .map((s) => (
- // Prevent ugly x-scale animations when start/end dates change with unique key
- <motion.g
- initial={{ opacity: 1 }}
- exit={{ opacity: 0 }}
- transition={{ duration: 0.1 }}
- key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
- >
- {/* Area background gradient */}
- <LinearGradient
- className={cn(
- s.colorClassName ?? "text-blue-500",
- seriesClassNames?.find(({ id }) => id === s.id)?.gradient,
- )}
- id={`${s.id}-background`}
- fromOffset="20%"
- from="currentColor"
- fromOpacity={0.01}
- to="currentColor"
- toOpacity={0.2}
- x1={0}
- x2={0}
- y1={1}
- />
-
- {/* Area */}
- <AreaClosed
- data={data}
- x={(d) => xScale(d.date)}
- y={(d) => yScale(s.valueAccessor(d) ?? 0)}
- yScale={yScale}
+ .map((s) => {
+ const seriesStyle = seriesStyles?.find(({ id }) => id === s.id);
+ return (
+ // Prevent ugly x-scale animations when start/end dates change with unique key
+ <motion.g
+ initial={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.1 }}
+ key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
>
- {({ path }) => {
- return (
+ {/* Area background mask gradient */}
+ <LinearGradient
+ id={`${s.id}-mask-gradient`}
+ from="white"
+ to="white"
+ fromOpacity={0.2}
+ toOpacity={0}
+ x1={0}
+ x2={0}
+ y1={0}
+ y2={1}
+ />
+ <mask id={`${s.id}-mask`} maskContentUnits="objectBoundingBox">
+ <rect
+ width="1"
+ height="1"
+ fill={`url(#${s.id}-mask-gradient)`}
+ />
+ </mask>
+
+ {/* Area */}
+ <AreaClosed
+ data={data}
+ x={(d) => xScale(d.date)}
+ y={(d) => yScale(s.valueAccessor(d) ?? 0)}
+ yScale={yScale}
+ >
+ {({ path }) => {
+ return (
+ <motion.path
+ initial={{ d: path(zeroedData) || "", opacity: 0 }}
+ animate={{ d: path(data) || "", opacity: 1 }}
+ className={cn(
+ s.colorClassName ?? "text-blue-500",
+ seriesStyle?.gradientClassName,
+ )}
+ mask={`url(#${s.id}-mask)`}
+ fill={seriesStyle?.areaFill ?? "currentColor"}
+ />
+ );
+ }}
+ </AreaClosed>
+
+ {/* Line */}
+ <Area
+ data={data}
+ x={(d) => xScale(d.date)}
+ y={(d) => yScale(s.valueAccessor(d) ?? 0)}
+ >
+ {({ path }) => (
<motion.path
- initial={{ d: path(zeroedData) || "", opacity: 0 }}
- animate={{ d: path(data) || "", opacity: 1 }}
- fill={`url(#${s.id}-background)`}
+ initial={{ d: path(zeroedData) || "" }}
+ animate={{ d: path(data) || "" }}
+ className={cn(
+ s.colorClassName ?? "text-blue-700",
+ seriesStyle?.lineClassName,
+ )}
+ stroke={seriesStyle?.lineStroke ?? "currentColor"}
+ strokeOpacity={0.8}
+ strokeWidth={2}
+ fill="transparent"
/>
- );
- }}
- </AreaClosed>
+ )}
+ </Area>
- {/* Line */}
- <Area
- data={data}
- x={(d) => xScale(d.date)}
- y={(d) => yScale(s.valueAccessor(d) ?? 0)}
- >
- {({ path }) => (
- <motion.path
- initial={{ d: path(zeroedData) || "" }}
- animate={{ d: path(data) || "" }}
+ {/* Latest value dot */}
+ {!tooltipData && (
+ <Circle
+ cx={xScale(data.at(-1)!.date)}
+ cy={yScale(s.valueAccessor(data.at(-1)!))}
+ r={4}
className={cn(
s.colorClassName ?? "text-blue-700",
- seriesClassNames?.find(({ id }) => id === s.id)?.line,
+ seriesStyle?.lineClassName,
)}
- stroke="currentColor"
- strokeOpacity={0.8}
- strokeWidth={2}
- fill="transparent"
+ fill="currentColor"
/>
)}
- </Area>
-
- {/* Latest value dot */}
- {!tooltipData && (
- <Circle
- cx={xScale(data.at(-1)!.date)}
- cy={yScale(s.valueAccessor(data.at(-1)!))}
- r={4}
- className={cn(
- s.colorClassName ?? "text-blue-700",
- seriesClassNames?.find(({ id }) => id === s.id)?.line,
- )}
- fill="currentColor"
- />
- )}
- </motion.g>
- ))}
+ </motion.g>
+ );
+ })}
</AnimatePresence>
</Group>
);
diff --git a/packages/blocks/src/mini-area-chart.tsx b/packages/blocks/src/mini-area-chart.tsx
index e331955d9c..9245d2978c 100644
--- a/packages/blocks/src/mini-area-chart.tsx
+++ b/packages/blocks/src/mini-area-chart.tsx
@@ -9,6 +9,7 @@ import { useMemo } from "react";
export type MiniAreaChartProps = {
data: { date: Date; value: number }[];
+ curve?: boolean;
};
export function MiniAreaChart(props: MiniAreaChartProps) {
@@ -27,9 +28,10 @@ export function MiniAreaChart(props: MiniAreaChartProps) {
const padding = { top: 8, right: 2, bottom: 2, left: 2 };
function MiniAreaChartInner({
- data,
width,
height,
+ data,
+ curve = true,
}: MiniAreaChartProps & { width: number; height: number }) {
const zeroedData = useMemo(
() =>
@@ -94,7 +96,7 @@ function MiniAreaChartInner({
data={data}
x={({ date }) => xScale(date)}
y={({ value }) => yScale(value) ?? 0}
- curve={curveNatural}
+ curve={curve ? curveNatural : undefined}
>
{({ path }) => {
return (
@@ -113,7 +115,7 @@ function MiniAreaChartInner({
x={({ date }) => xScale(date)}
y={({ value }) => yScale(value) ?? 0}
yScale={yScale}
- curve={curveNatural}
+ curve={curve ? curveNatural : undefined}
>
{({ path }) => {
return (
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 3601acf4c5..7422e2714f 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -52,6 +52,7 @@ type UseTableProps<T> = {
resourceName?: (plural: boolean) => string;
className?: string;
+ containerClassName?: string;
scrollWrapperClassName?: string;
thClassName?: string;
tdClassName?: string;
@@ -142,6 +143,7 @@ export function Table<T>({
onSortChange,
sortableColumns = [],
className,
+ containerClassName,
scrollWrapperClassName,
thClassName,
tdClassName,
@@ -152,7 +154,12 @@ export function Table<T>({
children,
}: TableProps<T>) {
return (
- <div className="relative border border-gray-200 bg-white sm:rounded-xl">
+ <div
+ className={cn(
+ "relative border border-gray-200 bg-white sm:rounded-xl",
+ containerClassName,
+ )}
+ >
{(!error && !!data?.length) || loading ? (
<div
className={cn(