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.

Fixes + LTV

Tim Wilson0a6457997936
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 800bddc4a6..49ef115c3e 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -39,7 +39,7 @@ export const getCustomersQuerySchema = z
),
sortBy: z
- .enum(["createdAt"])
+ .enum(["createdAt", "saleAmount"])
.optional()
.default("createdAt")
.describe(
@@ -110,6 +110,8 @@ export const CustomerSchema = z.object({
email: z.string().nullish().describe("Email of the customer."),
avatar: z.string().nullish().describe("Avatar URL of the customer."),
country: z.string().nullish().describe("Country of the customer."),
+ sales: z.number().describe("Total number of sales for the customer."),
+ saleAmount: z.number().describe("Total amount of sales for the customer."),
createdAt: z.date().describe("The date the customer was created."),
});
diff --git a/apps/web/ui/customers/customer-table/customer-table.tsx b/apps/web/ui/customers/customer-table/customer-table.tsx
index 2f0ffe679b..7b54de0d3b 100644
--- a/apps/web/ui/customers/customer-table/customer-table.tsx
+++ b/apps/web/ui/customers/customer-table/customer-table.tsx
@@ -20,11 +20,17 @@ import {
useTable,
} from "@dub/ui";
import { Dots, User } from "@dub/ui/icons";
-import { COUNTRIES, fetcher, formatDate, getPrettyUrl } from "@dub/utils";
+import {
+ COUNTRIES,
+ currencyFormatter,
+ fetcher,
+ formatDate,
+ getPrettyUrl,
+} from "@dub/utils";
import { Row } from "@tanstack/react-table";
import { Command } from "cmdk";
import { useParams, useRouter } from "next/navigation";
-import { useState } from "react";
+import { useMemo, useState } from "react";
import useSWR from "swr";
import { customersColumns, useColumnVisibility } from "./use-column-visibility";
import { useCustomerFilters } from "./use-customer-filters";
@@ -33,7 +39,7 @@ export function CustomerTable() {
const { id: workspaceId } = useWorkspace();
const { queryParams, searchParams, getQueryString } = useRouterStuff();
- const sortBy = searchParams.get("sortBy") || "saleAmount";
+ const sortBy = searchParams.get("sortBy") || "createdAt";
const sortOrder = searchParams.get("sortOrder") === "asc" ? "asc" : "desc";
const {
@@ -65,112 +71,138 @@ export function CustomerTable() {
const { columnVisibility, setColumnVisibility } = useColumnVisibility();
const { pagination, setPagination } = usePagination();
- const { table, ...tableProps } = useTable({
- data: customers || [],
- columns: [
- {
- id: "customer",
- header: "Customer",
- enableHiding: false,
- minSize: 250,
- cell: ({ row }) => {
- return <CustomerRowItem customer={row.original} />;
+ const columns = useMemo(
+ () =>
+ [
+ {
+ id: "customer",
+ header: "Customer",
+ enableHiding: false,
+ minSize: 250,
+ cell: ({ row }) => {
+ return <CustomerRowItem customer={row.original} />;
+ },
},
- },
- {
- id: "createdAt",
- header: "Created date",
- accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
- },
- {
- id: "link",
- header: "Referral link",
- accessorKey: "link",
- meta: {
- filterParams: ({ getValue }) => ({
- linkId: getValue().id,
- }),
+ {
+ id: "createdAt",
+ header: "Created date",
+ accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
},
- cell: ({ row }) =>
- row.original.link ? (
- <div className="flex items-center gap-3">
- <LinkLogo
- apexDomain={row.original.link.domain}
- className="size-4 shrink-0 sm:size-4"
- />
+ {
+ id: "saleAmount",
+ header: "Lifetime value",
+ accessorKey: "saleAmount",
+ cell: ({ getValue }) => (
+ <div className="flex items-center gap-2">
+ <span>
+ {currencyFormatter(getValue() / 100, {
+ maximumFractionDigits: undefined,
+ // @ts-ignore – trailingZeroDisplay is a valid option but TS is outdated
+ trailingZeroDisplay: "stripIfInteger",
+ })}
+ </span>
+ <span className="text-neutral-400">USD</span>
+ </div>
+ ),
+ },
+ {
+ id: "link",
+ header: "Referral link",
+ accessorKey: "link",
+ meta: {
+ filterParams: ({ getValue }) => ({
+ linkId: getValue().id,
+ }),
+ },
+ cell: ({ row }) =>
+ row.original.link ? (
+ <div className="flex items-center gap-3">
+ <LinkLogo
+ apexDomain={row.original.link.domain}
+ className="size-4 shrink-0 sm:size-4"
+ />
+ <CopyText
+ value={row.original.link.shortLink}
+ successMessage="Copied link to clipboard!"
+ className="truncate"
+ >
+ <span
+ className="truncate"
+ title={row.original.link.shortLink}
+ >
+ {getPrettyUrl(row.original.link.shortLink)}
+ </span>
+ </CopyText>
+ </div>
+ ) : (
+ "-"
+ ),
+ size: 250,
+ },
+ {
+ id: "externalId",
+ header: "External ID",
+ accessorKey: "externalId",
+ cell: ({ row }) =>
+ row.original.externalId ? (
<CopyText
- value={row.original.link.shortLink}
- successMessage="Copied link to clipboard!"
+ value={row.original.externalId}
+ successMessage="Copied external ID to clipboard!"
className="truncate"
>
- <span className="truncate" title={row.original.link.shortLink}>
- {getPrettyUrl(row.original.link.shortLink)}
- </span>
+ {row.original.externalId}
</CopyText>
- </div>
- ) : (
- "-"
- ),
- size: 250,
- },
- {
- id: "externalId",
- header: "External ID",
- accessorKey: "externalId",
- cell: ({ row }) =>
- row.original.externalId ? (
- <CopyText
- value={row.original.externalId}
- successMessage="Copied external ID to clipboard!"
- className="truncate"
- >
- {row.original.externalId}
- </CopyText>
- ) : (
- "-"
+ ) : (
+ "-"
+ ),
+ },
+ {
+ id: "country",
+ header: "Country",
+ minSize: 150,
+ cell: ({ row }) => {
+ const country = row.original.country;
+ return (
+ <div className="flex items-center gap-2">
+ {country && (
+ <img
+ alt={`${country} flag`}
+ src={`https://hatscripts.github.io/circle-flags/flags/${country.toLowerCase()}.svg`}
+ className="size-4 shrink-0"
+ />
+ )}
+ <span className="min-w-0 truncate">
+ {(country ? COUNTRIES[country] : null) ?? "-"}
+ </span>
+ </div>
+ );
+ },
+ },
+ // Menu
+ {
+ id: "menu",
+ enableHiding: false,
+ minSize: 43,
+ size: 43,
+ maxSize: 43,
+ header: () => <EditColumnsButton table={table} />,
+ cell: ({ row }) => (
+ <RowMenuButton row={row} workspaceId={workspaceId!} />
),
- },
- {
- id: "country",
- header: "Country",
- minSize: 150,
- cell: ({ row }) => {
- const country = row.original.country;
- return (
- <div className="flex items-center gap-2">
- {country && (
- <img
- alt={`${country} flag`}
- src={`https://hatscripts.github.io/circle-flags/flags/${country.toLowerCase()}.svg`}
- className="size-4 shrink-0"
- />
- )}
- <span className="min-w-0 truncate">
- {(country ? COUNTRIES[country] : null) ?? "-"}
- </span>
- </div>
- );
},
- },
- // Menu
- {
- id: "menu",
- enableHiding: false,
- minSize: 43,
- size: 43,
- maxSize: 43,
- header: () => <EditColumnsButton table={table} />,
- cell: ({ row }) => (
- <RowMenuButton row={row} workspaceId={workspaceId!} />
- ),
- },
- ].filter((c) => c.id === "menu" || customersColumns.all.includes(c.id)),
+ ].filter((c) => c.id === "menu" || customersColumns.all.includes(c.id)),
+ [],
+ );
+
+ const { table, ...tableProps } = useTable({
+ data: customers || [],
+ columns,
onRowClick: (row) => {},
pagination,
onPaginationChange: setPagination,
columnVisibility,
onColumnVisibilityChange: setColumnVisibility,
- sortableColumns: ["createdAt"],
+ sortableColumns: ["createdAt", "saleAmount"],
sortBy,
sortOrder,
onSortChange: ({ sortBy, sortOrder }) =>
@@ -182,6 +214,7 @@ export function CustomerTable() {
del: "page",
scroll: false,
}),
+ columnPinning: { right: ["menu"] },
thClassName: "border-l-0",
tdClassName: "border-l-0",
resourceName: (p) => `customer${p ? "s" : ""}`,
diff --git a/apps/web/ui/customers/customer-table/use-column-visibility.ts b/apps/web/ui/customers/customer-table/use-column-visibility.ts
index 8a0ed59cc0..fe70b4ff88 100644
--- a/apps/web/ui/customers/customer-table/use-column-visibility.ts
+++ b/apps/web/ui/customers/customer-table/use-column-visibility.ts
@@ -2,11 +2,11 @@ import { useLocalStorage } from "@dub/ui";
import { VisibilityState } from "@tanstack/react-table";
export const customersColumns = {
- all: ["customer", "createdAt", "ltv", "link", "externalId", "country"],
+ all: ["customer", "createdAt", "saleAmount", "link", "externalId", "country"],
defaultVisible: [
"customer",
"createdAt",
- "ltv",
+ "saleAmount",
"link",
"externalId",
"country",
diff --git a/apps/web/ui/customers/customer-table/use-customer-filters.tsx b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
index 03d9262ca2..92b0156a1e 100644
--- a/apps/web/ui/customers/customer-table/use-customer-filters.tsx
+++ b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
@@ -3,7 +3,7 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { useRouterStuff } from "@dub/ui";
import { FlagWavy } from "@dub/ui/icons";
import { COUNTRIES, nFormatter } from "@dub/utils";
-import { useMemo } from "react";
+import { useCallback, useMemo } from "react";
export function useCustomerFilters(extraSearchParams: Record<string, string>) {
const { searchParamsObj, queryParams } = useRouterStuff();
@@ -54,23 +54,32 @@ export function useCustomerFilters(extraSearchParams: Record<string, string>) {
return [...(country ? [{ key: "country", value: country }] : [])];
}, [searchParamsObj]);
- const onSelect = (key: string, value: any) =>
- queryParams({
- set: {
- [key]: value,
- },
- del: "page",
- });
+ const onSelect = useCallback(
+ (key: string, value: any) =>
+ queryParams({
+ set: {
+ [key]: value,
+ },
+ del: "page",
+ }),
+ [queryParams],
+ );
- const onRemove = (key: string, value: any) =>
- queryParams({
- del: [key, "page"],
- });
+ const onRemove = useCallback(
+ (key: string, value: any) =>
+ queryParams({
+ del: [key, "page"],
+ }),
+ [queryParams],
+ );
- const onRemoveAll = () =>
- queryParams({
- del: ["country", "search"],
- });
+ const onRemoveAll = useCallback(
+ () =>
+ queryParams({
+ del: ["country", "search"],
+ }),
+ [queryParams],
+ );
const searchQuery = useMemo(
() =>
@@ -85,7 +94,10 @@ export function useCustomerFilters(extraSearchParams: Record<string, string>) {
[activeFilters, workspaceId, extraSearchParams],
);
- const isFiltered = activeFilters.length > 0 || searchParamsObj.search;
+ const isFiltered = useMemo(
+ () => activeFilters.length > 0 || searchParamsObj.search,
+ [activeFilters, searchParamsObj.search],
+ );
return {
filters,