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.

Optimize sales table

Tim Wilson1c0f1ebe91b5
diff --git a/apps/web/app/api/programs/[programId]/sales/route.ts b/apps/web/app/api/programs/[programId]/sales/route.ts
index 9ff591b4f9..c1178aca70 100644
--- a/apps/web/app/api/programs/[programId]/sales/route.ts
+++ b/apps/web/app/api/programs/[programId]/sales/route.ts
@@ -24,8 +24,16 @@ export const GET = withWorkspace(
async ({ workspace, params, searchParams }) => {
const { programId } = params;
const parsed = getSalesQuerySchema.parse(searchParams);
- const { page, pageSize, status, order, sortBy, payoutId, partnerId } =
- parsed;
+ const {
+ page,
+ pageSize,
+ status,
+ order,
+ sortBy,
+ customerId,
+ payoutId,
+ partnerId,
+ } = parsed;
let { interval, start, end } = parsed;
let granularity: "minute" | "hour" | "day" | "month" = "day";
@@ -61,6 +69,7 @@ export const GET = withWorkspace(
where: {
programId,
...(status && { status }),
+ ...(customerId && { customerId }),
...(payoutId && { payoutId }),
...(partnerId && { partnerId }),
// createdAt: {
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 5af79d11a4..eb3d057c79 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
@@ -32,6 +32,7 @@ import {
formatDate,
} from "@dub/utils";
import { useParams } from "next/navigation";
+import { memo } from "react";
import useSWR from "swr";
import { z } from "zod";
import { useSaleFilters } from "./use-sale-filters";
@@ -83,16 +84,14 @@ export const SaleStatusBadges = {
};
export function SaleTableBusiness({ limit }: { limit?: number }) {
- const { programId } = useParams();
- const { id: workspaceId } = useWorkspace();
- const { pagination, setPagination } = usePagination(limit);
- const { queryParams, getQueryString, searchParamsObj } = useRouterStuff();
- const { sortBy, order } = searchParamsObj as {
- sortBy: string;
- order: "asc" | "desc";
- };
+ const filters = useSaleFilters();
- const {
+ return <SaleTableBusinessInner limit={limit} {...filters} />;
+}
+
+const SaleTableBusinessInner = memo(
+ ({
+ limit,
filters,
activeFilters,
onSelect,
@@ -101,196 +100,205 @@ export function SaleTableBusiness({ limit }: { limit?: number }) {
isFiltered,
setSearch,
setSelectedFilter,
- } = useSaleFilters();
+ }: { limit?: number } & ReturnType<typeof useSaleFilters>) => {
+ const { programId } = useParams();
+ const { id: workspaceId } = useWorkspace();
+ const { pagination, setPagination } = usePagination(limit);
+ const { queryParams, getQueryString, searchParamsObj } = useRouterStuff();
+ const { sortBy, order } = searchParamsObj as {
+ sortBy: string;
+ order: "asc" | "desc";
+ };
- const { salesCount } = useSalesCount();
- const { data: sales, error } = useSWR<z.infer<typeof salesSchema>[]>(
- `/api/programs/${programId}/sales${getQueryString({
- workspaceId,
- })}`,
- fetcher,
- );
+ const { salesCount } = useSalesCount();
+ const { data: sales, error } = useSWR<z.infer<typeof salesSchema>[]>(
+ `/api/programs/${programId}/sales${getQueryString({
+ workspaceId,
+ })}`,
+ fetcher,
+ );
- const loading = !sales && !error;
+ const loading = !sales && !error;
- const table = useTable({
- data: sales?.slice(0, limit) || [],
- columns: [
- {
- id: "createdAt",
- header: "Date",
- accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
- },
- {
- header: "Customer",
- cell: ({ row }) => {
- return (
- <div className="flex items-center gap-2">
- <img
- src={
- row.original.customer.avatar ||
- `${DICEBEAR_AVATAR_URL}${row.original.customer.name}`
- }
- alt={row.original.customer.name}
- className="size-5 rounded-full"
- />
- <div>{row.original.customer.name}</div>
- </div>
- );
+ const table = useTable({
+ data: sales?.slice(0, limit) || [],
+ columns: [
+ {
+ id: "createdAt",
+ header: "Date",
+ accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
},
- meta: {
- filterParams: ({ row }) => ({
- customerId: row.original.customer.id,
- }),
+ {
+ header: "Customer",
+ cell: ({ row }) => {
+ return (
+ <div className="flex items-center gap-2">
+ <img
+ src={
+ row.original.customer.avatar ||
+ `${DICEBEAR_AVATAR_URL}${row.original.customer.name}`
+ }
+ alt={row.original.customer.name}
+ className="size-5 rounded-full"
+ />
+ <div>{row.original.customer.name}</div>
+ </div>
+ );
+ },
+ meta: {
+ filterParams: ({ row }) => ({
+ customerId: row.original.customer.id,
+ }),
+ },
},
- },
- {
- header: "Partner",
- cell: ({ row }) => {
- return (
- <div className="flex items-center gap-2">
- <img
- src={
- row.original.partner.logo ||
- `${DICEBEAR_AVATAR_URL}${row.original.partner.name}`
- }
- alt={row.original.partner.name}
- className="size-5 rounded-full"
- />
- <div>{row.original.partner.name}</div>
- </div>
- );
+ {
+ header: "Partner",
+ cell: ({ row }) => {
+ return (
+ <div className="flex items-center gap-2">
+ <img
+ src={
+ row.original.partner.logo ||
+ `${DICEBEAR_AVATAR_URL}${row.original.partner.name}`
+ }
+ alt={row.original.partner.name}
+ className="size-5 rounded-full"
+ />
+ <div>{row.original.partner.name}</div>
+ </div>
+ );
+ },
+ meta: {
+ filterParams: ({ row }) => ({
+ partnerId: row.original.partner.id,
+ }),
+ },
},
- meta: {
- filterParams: ({ row }) => ({
- partnerId: row.original.partner.id,
- }),
+ {
+ id: "amount",
+ header: "Amount",
+ accessorFn: (d) =>
+ currencyFormatter(d.amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ }),
},
- },
- {
- id: "amount",
- header: "Amount",
- accessorFn: (d) =>
- currencyFormatter(d.amount / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- }),
- },
- {
- header: "Status",
- cell: ({ row }) => {
- const badge = SaleStatusBadges[row.original.status];
+ {
+ header: "Status",
+ cell: ({ row }) => {
+ const badge = SaleStatusBadges[row.original.status];
- return (
- <StatusBadge icon={null} variant={badge.variant}>
- {badge.label}
- </StatusBadge>
- );
+ return (
+ <StatusBadge icon={null} variant={badge.variant}>
+ {badge.label}
+ </StatusBadge>
+ );
+ },
},
+ // Menu
+ {
+ id: "menu",
+ enableHiding: false,
+ minSize: 43,
+ size: 43,
+ maxSize: 43,
+ cell: ({ row }) => <SaleRowMenu row={row} />,
+ },
+ ],
+ columnPinning: { right: ["menu"] },
+ cellRight: (cell) => {
+ const meta = cell.column.columnDef.meta as
+ | {
+ filterParams?: any;
+ }
+ | undefined;
+
+ return (
+ meta?.filterParams && <FilterButton set={meta.filterParams(cell)} />
+ );
},
- // Menu
- {
- id: "menu",
- enableHiding: false,
- minSize: 43,
- size: 43,
- maxSize: 43,
- cell: ({ row }) => <SaleRowMenu row={row} />,
- },
- ],
- columnPinning: { right: ["menu"] },
- cellRight: (cell) => {
- const meta = cell.column.columnDef.meta as
- | {
- filterParams?: any;
+ ...(!limit
+ ? {
+ pagination,
+ onPaginationChange: setPagination,
+ sortableColumns: ["createdAt", "amount"],
+ sortBy,
+ sortOrder: order,
+ onSortChange: ({ sortBy, sortOrder }) =>
+ queryParams({
+ set: {
+ ...(sortBy && { sortBy: sortBy }),
+ ...(sortOrder && { order: sortOrder }),
+ },
+ }),
}
- | undefined;
-
- return (
- meta?.filterParams && <FilterButton set={meta.filterParams(cell)} />
- );
- },
- ...(!limit
- ? {
- pagination,
- onPaginationChange: setPagination,
- sortableColumns: ["createdAt", "amount"],
- sortBy,
- sortOrder: order,
- onSortChange: ({ sortBy, sortOrder }) =>
- queryParams({
- set: {
- ...(sortBy && { sortBy: sortBy }),
- ...(sortOrder && { order: sortOrder }),
- },
- }),
- }
- : {
- thClassName: "border-l-0",
- tdClassName: "border-l-0",
- }),
- resourceName: (p) => `sale${p ? "s" : ""}`,
- rowCount: salesCount?.[status || "all"] ?? 0,
- loading,
- error: error ? "Failed to load sales" : undefined,
- });
+ : {
+ thClassName: "border-l-0",
+ tdClassName: "border-l-0",
+ }),
+ resourceName: (p) => `sale${p ? "s" : ""}`,
+ rowCount: salesCount?.[status || "all"] ?? 0,
+ loading,
+ error: error ? "Failed to load sales" : undefined,
+ });
- return (
- <div className="flex flex-col gap-3">
- <div>
- <div className="flex flex-col gap-3 md:flex-row md:items-center">
- <Filter.Select
- className="w-full md:w-fit"
- filters={filters}
- activeFilters={activeFilters}
- onSelect={onSelect}
- onRemove={onRemove}
- onSearchChange={setSearch}
- onSelectedFilterChange={setSelectedFilter}
- />
- <SimpleDateRangePicker className="w-full sm:min-w-[200px] md:w-fit" />
+ return (
+ <div className="flex flex-col gap-3">
+ <div>
+ <div className="flex flex-col gap-3 md:flex-row md:items-center">
+ <Filter.Select
+ className="w-full md:w-fit"
+ filters={filters}
+ activeFilters={activeFilters}
+ onSelect={onSelect}
+ onRemove={onRemove}
+ onSearchChange={setSearch}
+ onSelectedFilterChange={setSelectedFilter}
+ />
+ <SimpleDateRangePicker className="w-full sm:min-w-[200px] md:w-fit" />
+ </div>
+ <AnimatedSizeContainer height>
+ <div>
+ {activeFilters.length > 0 && (
+ <div className="pt-3">
+ <Filter.List
+ filters={[
+ ...filters,
+ {
+ key: "payoutId",
+ icon: MoneyBill2,
+ label: "Payout",
+ options: [],
+ },
+ ]}
+ activeFilters={activeFilters}
+ onRemove={onRemove}
+ onRemoveAll={onRemoveAll}
+ />
+ </div>
+ )}
+ </div>
+ </AnimatedSizeContainer>
</div>
- <AnimatedSizeContainer height>
- <div>
- {activeFilters.length > 0 && (
- <div className="pt-3">
- <Filter.List
- filters={[
- ...filters,
- {
- key: "payoutId",
- icon: MoneyBill2,
- label: "Payout",
- options: [],
- },
- ]}
- activeFilters={activeFilters}
- onRemove={onRemove}
- onRemoveAll={onRemoveAll}
- />
- </div>
+ {sales?.length !== 0 ? (
+ <Table {...table} />
+ ) : (
+ <AnimatedEmptyState
+ title="No sales found"
+ description={
+ isFiltered
+ ? "No sales found for the selected filters."
+ : "No sales have been made for this program yet."
+ }
+ cardContent={() => (
+ <>
+ <MoneyBill2 className="size-4 text-neutral-700" />
+ <div className="h-2.5 w-24 min-w-0 rounded-sm bg-neutral-200" />
+ </>
)}
- </div>
- </AnimatedSizeContainer>
+ />
+ )}
</div>
- {sales?.length !== 0 ? (
- <Table {...table} />
- ) : (
- <AnimatedEmptyState
- title="No sales found"
- description={
- isFiltered
- ? "No sales found for the selected filters."
- : "No sales have been made for this program yet."
- }
- cardContent={() => (
- <>
- <MoneyBill2 className="size-4 text-neutral-700" />
- <div className="h-2.5 w-24 min-w-0 rounded-sm bg-neutral-200" />
- </>
- )}
- />
- )}
- </div>
- );
-}
+ );
+ },
+);
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 75f7f88649..d811f46172 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -146,6 +146,7 @@ export const getSalesQuerySchema = z
interval: z.enum(intervals).default("30d"),
start: parseDateSchema.optional(),
end: parseDateSchema.optional(),
+ customerId: z.string().optional(),
payoutId: z.string().optional(),
partnerId: z.string().optional(),
})