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.

improve sale table

Steven Tey95188dca9c74
diff --git a/apps/web/app/api/embed/sales/route.ts b/apps/web/app/api/embed/sales/route.ts
index 720b209116..7ea7b25dd9 100644
--- a/apps/web/app/api/embed/sales/route.ts
+++ b/apps/web/app/api/embed/sales/route.ts
@@ -1,11 +1,16 @@
import { withEmbedToken } from "@/lib/embed/auth";
+import { SALES_PAGE_SIZE } from "@/lib/partners/constants";
import z from "@/lib/zod";
import { PartnerSaleResponseSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
// GET /api/embed/sales – get sales for a link from an embed token
-export const GET = withEmbedToken(async ({ link }) => {
+export const GET = withEmbedToken(async ({ link, searchParams }) => {
+ const { page } = z
+ .object({ page: z.coerce.number().optional().default(1) })
+ .parse(searchParams);
+
const sales = await prisma.sale.findMany({
where: {
linkId: link.id,
@@ -25,7 +30,8 @@ export const GET = withEmbedToken(async ({ link }) => {
},
},
},
- take: 3,
+ take: SALES_PAGE_SIZE,
+ skip: (page - 1) * SALES_PAGE_SIZE,
orderBy: {
createdAt: "desc",
},
diff --git a/apps/web/app/app.dub.co/embed/inline/page-client.tsx b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
index 4e8752e9d0..d8027d7b81 100644
--- a/apps/web/app/app.dub.co/embed/inline/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
@@ -20,11 +20,9 @@ import { LinkToken } from "../token";
export function EmbedInlinePageClient({
program,
link,
- hasPartnerProfile,
}: {
program: Program;
link: Link;
- hasPartnerProfile: boolean;
}) {
const [copied, copyToClipboard] = useCopyToClipboard();
@@ -104,7 +102,7 @@ export function EmbedInlinePageClient({
}}
className="w-full rounded-lg"
/>
- <EmbedWidgetSales hasPartnerProfile={hasPartnerProfile} />
+ <EmbedWidgetSales salesCount={link.sales} />
</div>
<LinkToken />
</div>
diff --git a/apps/web/app/app.dub.co/embed/inline/page.tsx b/apps/web/app/app.dub.co/embed/inline/page.tsx
index f1c87a2a00..a0740c7d16 100644
--- a/apps/web/app/app.dub.co/embed/inline/page.tsx
+++ b/apps/web/app/app.dub.co/embed/inline/page.tsx
@@ -8,14 +8,7 @@ export default async function EmbedInlinePage({
}) {
const { token } = searchParams;
- const { link, program, hasPartnerProfile, earnings } =
- await getEmbedData(token);
+ const { link, program, hasPartnerProfile } = await getEmbedData(token);
- return (
- <EmbedInlinePageClient
- program={program}
- link={link}
- hasPartnerProfile={hasPartnerProfile}
- />
- );
+ return <EmbedInlinePageClient program={program} link={link} />;
}
diff --git a/apps/web/app/app.dub.co/embed/sales.tsx b/apps/web/app/app.dub.co/embed/sales.tsx
index 8decec0cc9..02386aceaa 100644
--- a/apps/web/app/app.dub.co/embed/sales.tsx
+++ b/apps/web/app/app.dub.co/embed/sales.tsx
@@ -1,81 +1,82 @@
+import { SALES_PAGE_SIZE } from "@/lib/partners/constants";
import { PartnerSaleResponse } from "@/lib/types";
-import { Gift, LoadingSpinner } from "@dub/ui";
-import { cn, currencyFormatter, fetcher } from "@dub/utils";
+import { Gift, Table, usePagination, useTable } from "@dub/ui";
+import { currencyFormatter, fetcher, formatDate } from "@dub/utils";
import useSWR from "swr";
-export function EmbedWidgetSales({
- hasPartnerProfile,
-}: {
- hasPartnerProfile: boolean;
-}) {
+export function EmbedWidgetSales({ salesCount }: { salesCount: number }) {
+ const { pagination, setPagination } = usePagination(SALES_PAGE_SIZE);
const { data: sales, isLoading } = useSWR<PartnerSaleResponse[]>(
- "/api/embed/sales",
+ `/api/embed/sales?page=${pagination.pageIndex}`,
fetcher,
{
keepPreviousData: true,
},
);
- return sales ? (
- sales.length ? (
- <div className="mt-2.5 rounded-md border border-neutral-200">
- <div
- className={cn(
- "grid grid-cols-1",
- !hasPartnerProfile &&
- "[mask-image:linear-gradient(black,transparent)]",
- )}
- >
- {sales.slice(0, hasPartnerProfile ? 3 : 1).map((sale, idx) => (
- <div
- key={sale.id}
- className={cn(
- "flex items-center justify-between gap-4 border-neutral-200 px-3 py-2.5",
- idx > 0 && "border-t",
- )}
- >
- <div className="flex min-w-0 flex-col">
- <span className="truncate text-sm font-medium text-neutral-600">
- {sale.customer.email}
- </span>
- </div>
- <div className="flex flex-col">
- <span className="text-sm font-medium text-neutral-600">
- {currencyFormatter(sale.earnings / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- </div>
- </div>
- ))}
- </div>
- {!hasPartnerProfile && (
- <p className="px-8 py-4 text-center text-xs text-neutral-500">
- To withdraw your earnings or view all of your sales, create a free
- Dub partner account below.
- </p>
- )}
+
+ const { table, ...tableProps } = useTable({
+ data: sales || [],
+ loading: isLoading,
+ columns: [
+ {
+ id: "customer",
+ header: "Customer",
+ cell: ({ row }) => {
+ return row.original.customer.email;
+ },
+ },
+ {
+ id: "createdAt",
+ header: "Date",
+ cell: ({ row }) => {
+ return formatDate(row.original.createdAt, { month: "short" });
+ },
+ },
+ {
+ id: "saleAmount",
+ header: "Amount",
+ cell: ({ row }) => {
+ return currencyFormatter(row.original.amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+ },
+ },
+ {
+ id: "earnings",
+ header: "Earnings",
+ accessorKey: "earnings",
+ cell: ({ row }) => {
+ return currencyFormatter(row.original.earnings / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+ },
+ },
+ ],
+ pagination,
+ onPaginationChange: setPagination,
+ rowCount: salesCount,
+ emptyState: (
+ <div className="flex w-full flex-col items-center justify-center gap-2">
+ <Gift className="size-6 text-neutral-400" />
+ <p className="max-w-sm text-balance text-center text-xs text-neutral-400">
+ No sales yet. When you refer a friend and they make a purchase,
+ they'll show up here.
+ </p>
</div>
- ) : (
- <EmptyState />
- )
- ) : isLoading ? (
- <div className="mt-6 flex items-center justify-center">
- <LoadingSpinner className="size-4" />
- </div>
- ) : (
- <EmptyState />
- );
-}
+ ),
+ thClassName: "border-l-0",
+ tdClassName: "border-l-0",
+ resourceName: (plural) => `sale${plural ? "s" : ""}`,
+ });
-const EmptyState = () => {
return (
- <div className="mt-2.5 flex h-40 w-full flex-col items-center justify-center gap-2 rounded-lg border border-neutral-200 bg-neutral-50">
- <Gift className="size-6 text-neutral-400" />
- <p className="max-w-60 text-pretty text-center text-xs text-neutral-400">
- No sales yet. When you refer a friend and they make a purchase, they'll
- show up here.
- </p>
- </div>
+ <Table
+ {...tableProps}
+ table={table}
+ containerClassName="rounded-md border border-neutral-200 my-4"
+ scrollWrapperClassName="min-h-fit"
+ />
);
-};
+}
diff --git a/apps/web/lib/partners/constants.ts b/apps/web/lib/partners/constants.ts
index c47af9e234..1a9059d10d 100644
--- a/apps/web/lib/partners/constants.ts
+++ b/apps/web/lib/partners/constants.ts
@@ -1,3 +1,4 @@
export const DUB_PARTNERS_PAYOUT_FEE = 0.03;
export const MIN_PAYOUT_AMOUNT = 10000; // min payout is $100
export const SHEET_MAX_ITEMS = 6;
+export const SALES_PAGE_SIZE = 4;
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index 3f7aebdc90..df4b4b5f61 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -14,7 +14,7 @@ import { DubInitResult, DubOptions, IframeMessage } from "./types";
const INLINE_CONTAINER_STYLES = {
position: "relative",
maxWidth: "1024px",
- height: "760px",
+ height: "960px",
margin: "0 auto",
};