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.

fix sales tables for both business and partner

Steven Tey8059ec24a3af
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview-chart.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview-chart.tsx
index 9118376a74..5736a74047 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview-chart.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/overview-chart.tsx
@@ -1,10 +1,10 @@
-import { INTERVAL_DATA, INTERVAL_DISPLAYS } from "@/lib/analytics/constants";
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 { DateRangePicker, useRouterStuff } from "@dub/ui";
+import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
+import { useRouterStuff } from "@dub/ui";
import { LoadingSpinner } from "@dub/ui/src/icons";
import { currencyFormatter, formatDate } from "@dub/utils";
import { LinearGradient } from "@visx/gradient";
@@ -72,60 +72,7 @@ export function OverviewChart() {
</span>
)}
</div>
- <div>
- <DateRangePicker
- className="h-9 w-full px-2 md:w-fit"
- align="end"
- value={
- start && end
- ? {
- from: start,
- to: end,
- }
- : undefined
- }
- presetId={!start || !end ? interval ?? "24h" : undefined}
- onChange={(range, preset) => {
- if (preset) {
- queryParams({
- del: ["start", "end"],
- set: {
- interval: preset.id,
- },
- scroll: false,
- });
-
- return;
- }
-
- // Regular range
- if (!range || !range.from || !range.to) return;
-
- queryParams({
- del: "interval",
- set: {
- start: range.from.toISOString(),
- end: range.to.toISOString(),
- },
- scroll: false,
- });
- }}
- presets={INTERVAL_DISPLAYS.map(({ display, value, shortcut }) => {
- const start = INTERVAL_DATA[value].startDate;
- const end = new Date();
-
- return {
- id: value,
- label: display,
- dateRange: {
- from: start,
- to: end,
- },
- shortcut,
- };
- })}
- />
- </div>
+ <SimpleDateRangePicker className="h-9 w-full px-2 md:w-fit" />
</div>
<div className="relative mt-4 h-72 md:h-96">
{dataLoading ? (
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
index cd250b5950..722848e4b4 100644
--- 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
@@ -9,6 +9,7 @@ import { redirect, useParams } from "next/navigation";
import { OverviewChart } from "./overview-chart";
import { PendingPayouts } from "./pending-payouts";
import { ProgramStats } from "./program-stats";
+import { SaleTableBusiness } from "./sales/sale-table";
import { TopPartners } from "./top-partners";
export default function ProgramOverviewPageClient() {
@@ -62,6 +63,25 @@ export default function ProgramOverviewPageClient() {
<TopPartners />
<PendingPayouts />
</div>
+
+ <div className="mt-6">
+ <div className="flex items-center justify-between pb-3">
+ <h2 className="text-base font-semibold text-neutral-900">
+ Recent sales
+ </h2>
+
+ <Link
+ href={`/${slug}/programs/${programId}/sales`}
+ className={cn(
+ buttonVariants({ variant: "secondary" }),
+ "flex h-8 items-center rounded-lg border px-2 text-sm",
+ )}
+ >
+ View all
+ </Link>
+ </div>
+ <SaleTableBusiness limit={10} />
+ </div>
</div>
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/page.tsx
index 3dad384db1..ed39ba045b 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/page.tsx
@@ -1,20 +1,12 @@
import { PageContent } from "@/ui/layout/page-content";
import { MaxWidthWrapper } from "@dub/ui";
-import { SaleTable } from "./sale-table";
-
-export default function ProgramSales({
- params,
-}: {
- params: { slug: string; programId: string };
-}) {
- const { programId } = params;
+import { SaleTableBusiness } from "./sale-table";
+export default function ProgramSales() {
return (
<PageContent title="Sales">
<MaxWidthWrapper>
- <div className="mt-6">
- <SaleTable programId={programId} />
- </div>
+ <SaleTableBusiness />
</MaxWidthWrapper>
</PageContent>
);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/sale-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/sale-table.tsx
index 4870198270..b1ff0905fe 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/sale-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/sales/sale-table.tsx
@@ -3,7 +3,7 @@
import { generateRandomName } from "@/lib/names";
import { saleEventResponseSchema } from "@/lib/zod/schemas/sales";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
-import { SearchBoxPersisted } from "@/ui/shared/search-box";
+import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
import {
AnimatedSizeContainer,
Avatar,
@@ -17,11 +17,13 @@ import {
} from "@dub/ui";
import { MoneyBill2 } from "@dub/ui/src/icons";
import { currencyFormatter, fetcher, formatDate } from "@dub/utils";
+import { useParams } from "next/navigation";
import useSWR from "swr";
import { z } from "zod";
import { useSaleFilters } from "./use-sale-filters";
-export function SaleTable({ programId }: { programId: string }) {
+export function SaleTableBusiness({ limit }: { limit?: number }) {
+ const { programId } = useParams();
const { queryParams, searchParams } = useRouterStuff();
const sortBy = searchParams.get("sort") || "timestamp";
@@ -51,10 +53,10 @@ export function SaleTable({ programId }: { programId: string }) {
const loading = (!sales || !analyticsData) && !error && !analyticsError;
- const { pagination, setPagination } = usePagination();
+ const { pagination, setPagination } = usePagination(limit);
const table = useTable({
- data: sales || [],
+ data: sales?.slice(0, limit) || [],
columns: [
{
id: "timestamp",
@@ -119,18 +121,22 @@ export function SaleTable({ programId }: { programId: string }) {
}),
},
],
- pagination,
- onPaginationChange: setPagination,
- sortableColumns: ["timestamp"],
- sortBy,
- sortOrder: order,
- onSortChange: ({ sortBy, sortOrder }) =>
- queryParams({
- set: {
- ...(sortBy && { sort: sortBy }),
- ...(sortOrder && { order: sortOrder }),
- },
- }),
+ ...(!limit
+ ? {
+ pagination,
+ onPaginationChange: setPagination,
+ sortableColumns: ["timestamp"],
+ sortBy,
+ sortOrder: order,
+ onSortChange: ({ sortBy, sortOrder }) =>
+ queryParams({
+ set: {
+ ...(sortBy && { sort: sortBy }),
+ ...(sortOrder && { order: sortOrder }),
+ },
+ }),
+ }
+ : {}),
thClassName: "border-l-0",
tdClassName: "border-l-0",
resourceName: (p) => `sale${p ? "s" : ""}`,
@@ -141,41 +147,43 @@ export function SaleTable({ programId }: { programId: string }) {
return loading || sales?.length ? (
<div className="flex flex-col gap-3">
- <div>
- <div className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
- {loading ? (
- <>
- <div className="h-10 w-full animate-pulse rounded-md bg-neutral-200 md:w-24" />
- <div className="h-10 w-full animate-pulse rounded-md bg-neutral-200 md:w-32" />
- </>
- ) : (
- <>
- <Filter.Select
- className="w-full md:w-fit"
- filters={filters}
- activeFilters={activeFilters}
- onSelect={onSelect}
- onRemove={onRemove}
- />
- <SearchBoxPersisted />
- </>
- )}
- </div>
- <AnimatedSizeContainer height>
- <div>
- {activeFilters.length > 0 && (
- <div className="pt-3">
- <Filter.List
+ {!limit && (
+ <div>
+ <div className="flex flex-col gap-3 md:flex-row md:items-center">
+ {loading ? (
+ <>
+ <div className="h-10 w-full animate-pulse rounded-md bg-neutral-200 md:w-24" />
+ <div className="h-10 w-full animate-pulse rounded-md bg-neutral-200 md:w-32" />
+ </>
+ ) : (
+ <>
+ <Filter.Select
+ className="w-full md:w-fit"
filters={filters}
activeFilters={activeFilters}
+ onSelect={onSelect}
onRemove={onRemove}
- onRemoveAll={onRemoveAll}
/>
- </div>
+ <SimpleDateRangePicker className="w-full sm:min-w-[200px] md:w-fit" />
+ </>
)}
</div>
- </AnimatedSizeContainer>
- </div>
+ <AnimatedSizeContainer height>
+ <div>
+ {activeFilters.length > 0 && (
+ <div className="pt-3">
+ <Filter.List
+ filters={filters}
+ activeFilters={activeFilters}
+ onRemove={onRemove}
+ onRemoveAll={onRemoveAll}
+ />
+ </div>
+ )}
+ </div>
+ </AnimatedSizeContainer>
+ </div>
+ )}
<Table {...table} />
</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 346a845010..bac5dfaf92 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
@@ -1,8 +1,6 @@
"use client";
-import { INTERVAL_DATA, INTERVAL_DISPLAYS } from "@/lib/analytics/constants";
import usePartnerAnalytics from "@/lib/swr/use-partner-analytics";
-import usePartnerEvents from "@/lib/swr/use-partner-events";
import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
import Areas from "@/ui/charts/areas";
import { ChartContext } from "@/ui/charts/chart-context";
@@ -10,26 +8,17 @@ import TimeSeriesChart from "@/ui/charts/time-series-chart";
import XAxis from "@/ui/charts/x-axis";
import YAxis from "@/ui/charts/y-axis";
import { ProgramCommissionDescription } from "@/ui/programs/program-commission-description";
-import EmptyState from "@/ui/shared/empty-state";
+import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
import { MiniAreaChart } from "@dub/blocks";
import {
Button,
buttonVariants,
Check2,
- DateRangePicker,
MaxWidthWrapper,
- Table,
useCopyToClipboard,
- usePagination,
useRouterStuff,
- useTable,
} from "@dub/ui";
-import {
- CircleDollar,
- Copy,
- LoadingSpinner,
- MoneyBill2,
-} from "@dub/ui/src/icons";
+import { Copy, LoadingSpinner, MoneyBill2 } from "@dub/ui/src/icons";
import {
cn,
currencyFormatter,
@@ -43,6 +32,7 @@ import Link from "next/link";
import { useParams } from "next/navigation";
import { createContext, useContext, useId, useMemo } from "react";
import { HeroBackground } from "./hero-background";
+import { SaleTablePartner } from "./sales/sale-table";
const ProgramOverviewContext = createContext<{
start?: Date;
@@ -139,7 +129,9 @@ export default function ProgramPageClient() {
</div>
<div className="mt-6">
<div className="flex items-center justify-between">
- <h2 className="text-base font-medium text-neutral-900">Sales</h2>
+ <h2 className="text-base font-medium text-neutral-900">
+ Recent sales
+ </h2>
<Link
href={`/${partnerId}/${programId}/sales`}
className={cn(
@@ -151,7 +143,7 @@ export default function ProgramPageClient() {
</Link>
</div>
<div className="mt-4">
- <SalesTable />
+ <SaleTablePartner limit={10} />
</div>
</div>
</ProgramOverviewContext.Provider>
@@ -205,58 +197,7 @@ function EarningsChart() {
</div>
</div>
<div className="w-full md:w-auto">
- <DateRangePicker
- className="h-8 w-full md:w-fit"
- align="end"
- value={
- start && end
- ? {
- from: start,
- to: end,
- }
- : undefined
- }
- presetId={!start || !end ? interval ?? "24h" : undefined}
- onChange={(range, preset) => {
- if (preset) {
- queryParams({
- del: ["start", "end"],
- set: {
- interval: preset.id,
- },
- scroll: false,
- });
-
- return;
- }
-
- // Regular range
- if (!range || !range.from || !range.to) return;
-
- queryParams({
- del: "interval",
- set: {
- start: range.from.toISOString(),
- end: range.to.toISOString(),
- },
- scroll: false,
- });
- }}
- presets={INTERVAL_DISPLAYS.map(({ display, value, shortcut }) => {
- const start = INTERVAL_DATA[value].startDate;
- const end = new Date();
-
- return {
- id: value,
- label: display,
- dateRange: {
- from: start,
- to: end,
- },
- shortcut,
- };
- })}
- />
+ <SimpleDateRangePicker className="h-8 w-full md:w-fit" />
</div>
</div>
<div className="relative mt-4 h-64 w-full">
@@ -402,79 +343,3 @@ function StatCard({
</Link>
);
}
-
-function SalesTable() {
- const { start, end, interval } = useContext(ProgramOverviewContext);
-
- const { data: { sales: totalSaleEvents } = {} } = usePartnerAnalytics({
- interval: interval as any,
- start,
- end,
- });
- const {
- data: saleEvents,
- loading,
- error,
- } = usePartnerEvents({
- event: "sales",
- interval: interval as any,
- start,
- end,
- });
-
- const { pagination, setPagination } = usePagination();
-
- const { table, ...tableProps } = useTable({
- data: saleEvents ?? [],
- loading,
- error: error ? "Failed to fetch sales events." : undefined,
- columns: [
- {
- id: "timestamp",
- header: "Date",
- accessorKey: "timestamp",
- cell: ({ row }) => {
- return formatDate(row.original.timestamp, { month: "short" });
- },
- },
- {
- id: "customer",
- header: "Customer",
- accessorKey: "customer",
- cell: ({ row }) => {
- return row.original.customer.email;
- },
- },
- {
- id: "earned",
- header: "Earned",
- accessorKey: "earnings",
- cell: ({ row }) => {
- return currencyFormatter(row.original.earnings / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- });
- },
- },
- ],
- pagination,
- onPaginationChange: setPagination,
- rowCount: totalSaleEvents,
- 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]/sales/page.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/sales/page.tsx
new file mode 100644
index 0000000000..a0a38ef53e
--- /dev/null
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/sales/page.tsx
@@ -0,0 +1,13 @@
+import { PageContent } from "@/ui/layout/page-content";
+import { MaxWidthWrapper } from "@dub/ui";
+import { SaleTablePartner } from "./sale-table";
+
+export default function ProgramSales() {
+ return (
+ <PageContent title="Sales">
+ <MaxWidthWrapper>
+ <SaleTablePartner />
+ </MaxWidthWrapper>
+ </PageContent>
+ );
+}
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/sales/sale-table.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/sales/sale-table.tsx
new file mode 100644
index 0000000000..7ae854ed0c
--- /dev/null
+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/sales/sale-table.tsx
@@ -0,0 +1,156 @@
+"use client";
+
+import usePartnerAnalytics from "@/lib/swr/use-partner-analytics";
+import usePartnerEvents from "@/lib/swr/use-partner-events";
+import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
+import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
+import { Table, usePagination, useRouterStuff, useTable } from "@dub/ui";
+import { CircleDollar } from "@dub/ui/src/icons";
+import { currencyFormatter, formatDate } from "@dub/utils";
+
+export function SaleTablePartner({ limit }: { limit?: number }) {
+ const { queryParams, searchParamsObj } = useRouterStuff();
+
+ const {
+ start,
+ end,
+ interval,
+ sortBy = "timestamp",
+ order = "desc",
+ } = searchParamsObj as {
+ start?: string;
+ end?: string;
+ interval?: string;
+ sortBy?: "timestamp";
+ order?: "asc" | "desc";
+ };
+
+ const { data: { sales: totalSaleEvents } = {} } = usePartnerAnalytics({
+ interval: interval as any,
+ start: start ? new Date(start) : undefined,
+ end: end ? new Date(end) : undefined,
+ });
+
+ const {
+ data: saleEvents,
+ loading,
+ error,
+ } = usePartnerEvents({
+ event: "sales",
+ interval: interval as any,
+ start: start ? new Date(start) : undefined,
+ end: end ? new Date(end) : undefined,
+ order,
+ sortBy,
+ });
+
+ const { pagination, setPagination } = usePagination(limit);
+
+ const { table, ...tableProps } = useTable({
+ data: saleEvents?.slice(0, limit) || [],
+ loading,
+ error: error ? "Failed to fetch sales events." : undefined,
+ columns: [
+ {
+ id: "timestamp",
+ header: "Date",
+ accessorKey: "timestamp",
+ cell: ({ row }) => {
+ return formatDate(row.original.timestamp, { month: "short" });
+ },
+ },
+ {
+ id: "customer",
+ header: "Customer",
+ accessorKey: "customer",
+ cell: ({ row }) => {
+ return row.original.customer.email;
+ },
+ },
+ {
+ id: "saleAmount",
+ header: "Sale Amount",
+ accessorKey: "sale",
+ cell: ({ row }) => {
+ return currencyFormatter(row.original.sale.amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+ },
+ },
+ {
+ id: "earnings",
+ header: "Earnings",
+ accessorKey: "earnings",
+ cell: ({ row }) => {
+ return currencyFormatter(row.original.earnings / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+ },
+ },
+ ],
+ ...(!limit && {
+ pagination,
+ onPaginationChange: setPagination,
+ sortableColumns: ["timestamp"],
+ sortBy,
+ sortOrder: order,
+ onSortChange: ({ sortBy, sortOrder }) =>
+ queryParams({
+ set: {
+ ...(sortBy && { sort: sortBy }),
+ ...(sortOrder && { order: sortOrder }),
+ },
+ }),
+ }),
+ rowCount: totalSaleEvents,
+ emptyState: (
+ <AnimatedEmptyState
+ title="No sales found"
+ description="No sales have been made for this program yet."
+ cardContent={() => (
+ <>
+ <CircleDollar className="size-4 text-neutral-700" />
+ <div className="h-2.5 w-24 min-w-0 rounded-sm bg-neutral-200" />
+ </>
+ )}
+ />
+ ),
+ resourceName: (plural) => `sale${plural ? "s" : ""}`,
+ });
+
+ return loading || saleEvents?.length ? (
+ <div className="flex flex-col gap-3">
+ {!limit && (
+ <div className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
+ {loading ? (
+ <>
+ <div className="h-10 w-full animate-pulse rounded-md bg-neutral-200 md:w-32" />
+ </>
+ ) : (
+ <>
+ <SimpleDateRangePicker className="w-full sm:min-w-[200px] md:w-fit" />
+ </>
+ )}
+ </div>
+ )}
+ <Table
+ {...tableProps}
+ table={table}
+ containerClassName="border-neutral-300"
+ />
+ </div>
+ ) : (
+ <AnimatedEmptyState
+ title="No sales found"
+ description="No sales have been made for this program yet."
+ cardContent={() => (
+ <>
+ <CircleDollar className="size-4 text-neutral-700" />
+ <div className="h-2.5 w-24 min-w-0 rounded-sm bg-neutral-200" />
+ </>
+ )}
+ />
+ );
+}
diff --git a/apps/web/ui/shared/simple-date-range-picker.tsx b/apps/web/ui/shared/simple-date-range-picker.tsx
new file mode 100644
index 0000000000..953064736f
--- /dev/null
+++ b/apps/web/ui/shared/simple-date-range-picker.tsx
@@ -0,0 +1,70 @@
+import { INTERVAL_DATA, INTERVAL_DISPLAYS } from "@/lib/analytics/constants";
+import { DateRangePicker, useRouterStuff } from "@dub/ui";
+
+export default function SimpleDateRangePicker({
+ className,
+}: {
+ className?: string;
+}) {
+ const { queryParams, searchParamsObj } = useRouterStuff();
+ const { start, end, interval } = searchParamsObj as {
+ start?: string;
+ end?: string;
+ interval?: string;
+ };
+
+ return (
+ <DateRangePicker
+ className={className}
+ align="end"
+ value={
+ start && end
+ ? {
+ from: new Date(start),
+ to: new Date(end),
+ }
+ : undefined
+ }
+ presetId={!start || !end ? interval ?? "30d" : undefined}
+ onChange={(range, preset) => {
+ if (preset) {
+ queryParams({
+ del: ["start", "end"],
+ set: {
+ interval: preset.id,
+ },
+ scroll: false,
+ });
+
+ return;
+ }
+
+ // Regular range
+ if (!range || !range.from || !range.to) return;
+
+ queryParams({
+ del: "interval",
+ set: {
+ start: range.from.toISOString(),
+ end: range.to.toISOString(),
+ },
+ scroll: false,
+ });
+ }}
+ presets={INTERVAL_DISPLAYS.map(({ display, value, shortcut }) => {
+ const start = INTERVAL_DATA[value].startDate;
+ const end = new Date();
+
+ return {
+ id: value,
+ label: display,
+ dateRange: {
+ from: start,
+ to: end,
+ },
+ shortcut,
+ };
+ })}
+ />
+ );
+}