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 filtering ability

Steven Tey835ff39f207e
diff --git a/apps/web/app/(ee)/admin.dub.co/(dashboard)/programs/recent/page.tsx b/apps/web/app/(ee)/admin.dub.co/(dashboard)/programs/recent/page.tsx
index 4427790a48..5e2e351a75 100644
--- a/apps/web/app/(ee)/admin.dub.co/(dashboard)/programs/recent/page.tsx
+++ b/apps/web/app/(ee)/admin.dub.co/(dashboard)/programs/recent/page.tsx
@@ -1,22 +1,26 @@
"use client";
-import { PlanProps } from "@/lib/types";
+import { PlanProps, plans } from "@/lib/types";
import {
ADMIN_RECENT_PROGRAMS_PAGE_SIZE,
adminRecentProgramSchema,
} from "@/lib/zod/schemas/admin";
import { useConfirmModal } from "@/ui/modals/confirm-modal";
+import { FilterButtonTableRow } from "@/ui/shared/filter-button-table-row";
import PlanBadge from "@/ui/workspaces/plan-badge";
import {
+ CreditCard,
+ Filter,
GridIcon,
- LoadingSpinner,
- PaginationControls,
+ Table,
TimestampTooltip,
Tooltip,
usePagination,
useRouterStuff,
+ useTable,
} from "@dub/ui";
import {
+ capitalize,
cn,
currencyFormatter,
fetcher,
@@ -27,15 +31,13 @@ import {
OG_AVATAR_URL,
PARTNERS_DOMAIN,
} from "@dub/utils";
-import { Suspense, useState } from "react";
+import { Suspense, useCallback, useMemo, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";
import * as z from "zod/v4";
type AdminRecentProgram = z.infer<typeof adminRecentProgramSchema>;
-const GRID_COLS = "grid-cols-[minmax(0,1fr)_140px_120px_160px_140px]";
-
export default function RecentProgramsPage() {
return (
<Suspense>
@@ -45,7 +47,8 @@ export default function RecentProgramsPage() {
}
function RecentProgramsPageClient() {
- const { getQueryString } = useRouterStuff();
+ const { queryParams, getQueryString, searchParamsObj } = useRouterStuff();
+ const { plan } = searchParamsObj;
const { pagination, setPagination } = usePagination(
ADMIN_RECENT_PROGRAMS_PAGE_SIZE,
);
@@ -53,10 +56,55 @@ function RecentProgramsPageClient() {
null,
);
+ const filters = useMemo(
+ () => [
+ {
+ key: "plan",
+ icon: CreditCard,
+ label: "Plan",
+ singleSelect: true,
+ options: plans.map((value) => ({
+ value,
+ label: capitalize(value) ?? value,
+ })),
+ },
+ ],
+ [],
+ );
+
+ const activeFilters = useMemo(
+ () => [...(plan ? [{ key: "plan", value: plan }] : [])],
+ [plan],
+ );
+
+ const onSelect = useCallback(
+ (key: string, value: any) =>
+ queryParams({
+ set: { [key]: value },
+ del: "page",
+ }),
+ [queryParams],
+ );
+
+ const onRemove = useCallback(
+ (key: string) =>
+ queryParams({
+ del: [key, "page"],
+ }),
+ [queryParams],
+ );
+
+ const onRemoveAll = useCallback(
+ () =>
+ queryParams({
+ del: ["plan", "page"],
+ }),
+ [queryParams],
+ );
+
const {
data: { programs, total } = {},
isLoading,
- isValidating,
mutate,
} = useSWR<{
programs: AdminRecentProgram[];
@@ -117,67 +165,122 @@ function RecentProgramsPageClient() {
},
});
+ const { table, ...tableProps } = useTable({
+ data: programs ?? [],
+ columns: [
+ {
+ id: "program",
+ header: "Program",
+ minSize: 280,
+ cell: ({ row }) => (
+ <ProgramCell
+ program={row.original}
+ onAddToMarketplace={() => {
+ setProgramToAdd(row.original);
+ setShowConfirmModal(true);
+ }}
+ />
+ ),
+ },
+ {
+ id: "plan",
+ header: "Plan",
+ minSize: 140,
+ cell: ({ row }) => <PlanBadge plan={row.original.plan as PlanProps} />,
+ meta: {
+ filterParams: ({ row }) => ({
+ plan: row.original.plan,
+ }),
+ },
+ },
+ {
+ id: "partners",
+ header: "Partners",
+ minSize: 120,
+ cell: ({ row }) => (
+ <span className="tabular-nums">
+ {nFormatter(row.original.partners, { full: true })}
+ </span>
+ ),
+ },
+ {
+ id: "commissions",
+ header: "Commissions (30D)",
+ minSize: 160,
+ cell: ({ row }) => (
+ <span className="tabular-nums">
+ {currencyFormatter(row.original.commissions, {
+ trailingZeroDisplay: "stripIfInteger",
+ })}
+ </span>
+ ),
+ },
+ {
+ id: "createdAt",
+ header: "Created",
+ minSize: 140,
+ cell: ({ row }) => (
+ <TimestampTooltip
+ timestamp={row.original.createdAt}
+ rows={["local", "utc", "unix"]}
+ side="left"
+ >
+ <span className="cursor-default whitespace-nowrap tabular-nums text-neutral-600 underline decoration-neutral-300 decoration-dotted underline-offset-2">
+ {formatDateSmart(row.original.createdAt, { month: "short" })}
+ </span>
+ </TimestampTooltip>
+ ),
+ },
+ ],
+ pagination,
+ onPaginationChange: setPagination,
+ resourceName: (plural) => `program${plural ? "s" : ""}`,
+ rowCount: total ?? 0,
+ loading: isLoading,
+ thClassName: "border-l-0",
+ tdClassName: "border-l-0",
+ cellRight: (cell) => {
+ const meta = cell.column.columnDef.meta as
+ | {
+ filterParams?: any;
+ }
+ | undefined;
+
+ return (
+ meta?.filterParams && (
+ <FilterButtonTableRow set={meta.filterParams(cell)} />
+ )
+ );
+ },
+ });
+
return (
<>
{confirmModal}
- <div className="overflow-hidden rounded-xl border border-neutral-200 bg-white">
- {isLoading && !programs ? (
- <div className="flex items-center gap-2 px-5 py-6 text-sm text-neutral-500">
- <LoadingSpinner className="size-4" />
- Loading recent programs...
- </div>
- ) : !programs?.length ? (
- <div className="px-5 py-10 text-center text-sm text-neutral-500">
- No active programs found.
- </div>
- ) : (
- <>
- <div className="overflow-x-auto">
- <div
- className={cn("min-w-[1010px]", isValidating && "opacity-50")}
- >
- <div
- className={cn(
- "grid items-center gap-3 border-b border-neutral-200 bg-neutral-50 px-4 py-2 text-xs font-medium uppercase tracking-wide text-neutral-500",
- GRID_COLS,
- )}
- >
- <div>Program</div>
- <div>Plan</div>
- <div>Partners</div>
- <div>Commissions (30D)</div>
- <div>Created</div>
- </div>
- <div className="divide-y divide-neutral-200">
- {programs.map((program) => (
- <RecentProgramRow
- key={program.id}
- program={program}
- onAddToMarketplace={() => {
- setProgramToAdd(program);
- setShowConfirmModal(true);
- }}
- />
- ))}
- </div>
- </div>
- </div>
- <div className="border-t border-neutral-200 px-4 py-2">
- <PaginationControls
- pagination={pagination}
- setPagination={setPagination}
- totalCount={total ?? 0}
- unit={(plural) => `program${plural ? "s" : ""}`}
- />
- </div>
- </>
+ <div className="flex flex-col gap-3">
+ <Filter.Select
+ className="w-full md:w-fit"
+ filters={filters}
+ activeFilters={activeFilters}
+ onSelect={onSelect}
+ onRemove={onRemove}
+ />
+ {activeFilters.length > 0 && (
+ <Filter.List
+ filters={filters}
+ activeFilters={activeFilters}
+ onSelect={onSelect}
+ onRemove={onRemove}
+ onRemoveAll={onRemoveAll}
+ />
)}
+ <Table {...tableProps} table={table} />
</div>
</>
);
}
-function RecentProgramRow({
+function ProgramCell({
program,
onAddToMarketplace,
}: {
@@ -195,99 +298,76 @@ function RecentProgramRow({
);
return (
- <div className={cn("grid items-center gap-3 px-4 py-2.5", GRID_COLS)}>
- <div className="flex min-w-0 items-center gap-1.5 whitespace-nowrap text-sm font-medium">
- <img
- src={program.logo || `${OG_AVATAR_URL}${program.name}`}
- alt={program.name}
- width={20}
- height={20}
- className="size-4 rounded-full"
- />
- <span className="text-sm font-medium">{program.name}</span>•
- <a
- className="text-sm font-medium text-neutral-500 transition-colors hover:text-neutral-800"
- href={programUrl}
- target="_blank"
- rel="noopener noreferrer"
+ <div className="flex min-w-0 items-center gap-1.5 text-sm font-medium">
+ <img
+ src={program.logo || `${OG_AVATAR_URL}${program.name}`}
+ alt={program.name}
+ width={20}
+ height={20}
+ className="size-4 rounded-full"
+ />
+ <span className="truncate text-sm font-medium">{program.name}</span>•
+ <a
+ className="truncate text-sm font-medium text-neutral-500 transition-colors hover:text-neutral-800"
+ href={programUrl}
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ {getDomainWithoutWWW(programUrl)}
+ </a>
+ {listedAt ? (
+ <Tooltip
+ content={
+ <div className="max-w-[220px] px-3 py-2.5 text-center">
+ <p className="text-sm font-medium text-neutral-900">
+ Dub Marketplace
+ </p>
+ <p className="mt-1 text-xs leading-snug text-neutral-500">
+ Listed since{" "}
+ {new Intl.DateTimeFormat("en-US", {
+ month: "short",
+ day: "numeric",
+ year: "numeric",
+ }).format(new Date(listedAt))}
+ </p>
+ <p className="mt-2 text-xs font-medium text-teal-700">
+ Click to open listing ↗
+ </p>
+ </div>
+ }
>
- {getDomainWithoutWWW(programUrl)}
- </a>
- {listedAt ? (
- <Tooltip
- content={
- <div className="max-w-[220px] px-3 py-2.5 text-center">
- <p className="text-sm font-medium text-neutral-900">
- Dub Marketplace
- </p>
- <p className="mt-1 text-xs leading-snug text-neutral-500">
- Listed since{" "}
- {new Intl.DateTimeFormat("en-US", {
- month: "short",
- day: "numeric",
- year: "numeric",
- }).format(new Date(listedAt))}
- </p>
- <p className="mt-2 text-xs font-medium text-teal-700">
- Click to open listing ↗
- </p>
- </div>
- }
- >
- <a
- href={`https://partners.dub.co/marketplace/${program.slug}`}
- target="_blank"
- rel="noopener noreferrer"
- className={cn(
- "ml-1 inline-flex size-6 shrink-0 items-center justify-center rounded-md",
- "border border-teal-200/90 bg-gradient-to-br from-white to-teal-50/90",
- "text-teal-700 shadow-sm ring-1 ring-inset ring-white",
- "transition-[box-shadow,transform] duration-150 hover:shadow-md active:scale-[0.97]",
- "outline-none focus-visible:ring-2 focus-visible:ring-teal-400/35 focus-visible:ring-offset-2 focus-visible:ring-offset-white",
- )}
- aria-label={`View ${program.name} on Dub Marketplace (opens in new tab)`}
- >
- {marketplaceIcon}
- </a>
- </Tooltip>
- ) : (
- <button
- type="button"
- onClick={onAddToMarketplace}
+ <a
+ href={`https://partners.dub.co/marketplace/${program.slug}`}
+ target="_blank"
+ rel="noopener noreferrer"
className={cn(
"ml-1 inline-flex size-6 shrink-0 items-center justify-center rounded-md",
- "border border-neutral-200 bg-white text-neutral-400",
- "transition-[box-shadow,transform,color] duration-150",
- "hover:bg-neutral-50 hover:text-neutral-600 hover:shadow-sm active:scale-[0.97]",
- "outline-none focus-visible:ring-2 focus-visible:ring-neutral-400/35 focus-visible:ring-offset-2 focus-visible:ring-offset-white",
+ "border border-teal-200/90 bg-gradient-to-br from-white to-teal-50/90",
+ "text-teal-700 shadow-sm ring-1 ring-inset ring-white",
+ "transition-[box-shadow,transform] duration-150 hover:shadow-md active:scale-[0.97]",
+ "outline-none focus-visible:ring-2 focus-visible:ring-teal-400/35 focus-visible:ring-offset-2 focus-visible:ring-offset-white",
)}
- aria-label={`Add ${program.name} to Dub Marketplace`}
+ aria-label={`View ${program.name} on Dub Marketplace (opens in new tab)`}
>
{marketplaceIcon}
- </button>
- )}
- </div>
-
- <PlanBadge plan={program.plan as PlanProps} />
- <div className="text-sm tabular-nums text-neutral-700">
- {nFormatter(program.partners, { full: true })}
- </div>
-
- <div className="text-sm tabular-nums text-neutral-700">
- {currencyFormatter(program.commissions, {
- trailingZeroDisplay: "stripIfInteger",
- })}
- </div>
-
- <TimestampTooltip
- timestamp={program.createdAt}
- rows={["local", "utc", "unix"]}
- side="left"
- >
- <span className="cursor-default whitespace-nowrap text-sm tabular-nums text-neutral-600 underline decoration-neutral-300 decoration-dotted underline-offset-2">
- {formatDateSmart(program.createdAt, { month: "short" })}
- </span>
- </TimestampTooltip>
+ </a>
+ </Tooltip>
+ ) : (
+ <button
+ type="button"
+ onClick={onAddToMarketplace}
+ className={cn(
+ "ml-1 inline-flex size-6 shrink-0 items-center justify-center rounded-md",
+ "border border-neutral-200 bg-white text-neutral-400",
+ "transition-[box-shadow,transform,color] duration-150",
+ "hover:bg-neutral-50 hover:text-neutral-600 hover:shadow-sm active:scale-[0.97]",
+ "outline-none focus-visible:ring-2 focus-visible:ring-neutral-400/35 focus-visible:ring-offset-2 focus-visible:ring-offset-white",
+ )}
+ aria-label={`Add ${program.name} to Dub Marketplace`}
+ >
+ {marketplaceIcon}
+ </button>
+ )}
</div>
);
}
diff --git a/apps/web/app/(ee)/api/admin/programs/recent/route.ts b/apps/web/app/(ee)/api/admin/programs/recent/route.ts
index c2e09165a9..788473c308 100644
--- a/apps/web/app/(ee)/api/admin/programs/recent/route.ts
+++ b/apps/web/app/(ee)/api/admin/programs/recent/route.ts
@@ -11,7 +11,7 @@ import { NextResponse } from "next/server";
// GET /api/admin/programs/recent
export const GET = withAdmin(async ({ searchParams }) => {
- const { page = 1, pageSize } =
+ const { page = 1, pageSize, plan } =
adminRecentProgramsQuerySchema.parse(searchParams);
const where = {
@@ -24,6 +24,11 @@ export const GET = withAdmin(async ({ searchParams }) => {
endsWith: "-staging",
},
},
+ ...(plan && {
+ workspace: {
+ plan,
+ },
+ }),
} satisfies Prisma.ProgramWhereInput;
const { startDate, endDate } = getStartEndDates({ interval: "30d" });
diff --git a/apps/web/lib/zod/schemas/admin.ts b/apps/web/lib/zod/schemas/admin.ts
index 1c32f998bd..37c2d5b04a 100644
--- a/apps/web/lib/zod/schemas/admin.ts
+++ b/apps/web/lib/zod/schemas/admin.ts
@@ -5,7 +5,7 @@ import {
ProgramEnrollmentStatus,
} from "@prisma/client";
import * as z from "zod/v4";
-import { getPaginationQuerySchema } from "./misc";
+import { getPaginationQuerySchema, planSchema } from "./misc";
import { partnerProfileChangeHistoryLogSchema } from "./partner-profile";
import {
EnrolledPartnerSchemaExtended,
@@ -87,9 +87,13 @@ export const adminNetworkPartnerSchema = EnrolledPartnerSchemaExtended.pick({
export const ADMIN_RECENT_PROGRAMS_PAGE_SIZE = 100;
-export const adminRecentProgramsQuerySchema = z.object(
- getPaginationQuerySchema({ pageSize: ADMIN_RECENT_PROGRAMS_PAGE_SIZE }),
-);
+export const adminRecentProgramsQuerySchema = z
+ .object({
+ plan: planSchema.optional(),
+ })
+ .extend(
+ getPaginationQuerySchema({ pageSize: ADMIN_RECENT_PROGRAMS_PAGE_SIZE }),
+ );
export const adminRecentProgramSchema = z.object({
id: z.string(),