From 221f0e4afcd3023c9f84ceb9f6ae56e1afd0c3fa Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Mon, 23 Nov 2026 18:03:53 +1100 Subject: [PATCH 1/2] feat(waitlist): filter the queue by the Guard's advice, with counts; newest first, 100 at a time --- .../admin/waitlist/PendingApplications.tsx | 52 ++++++++++-- .../api/routers/admin-waitlist.trpc.test.ts | 49 ++++++++---- apps/web/src/server/api/routers/admin.ts | 79 +++++++++++++------ 3 files changed, 134 insertions(+), 46 deletions(-) diff --git a/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx b/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx index b6942458..f509a444 100644 --- a/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx +++ b/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx @@ -5,6 +5,7 @@ import { Button } from "@usesend/ui/src/button"; import Spinner from "@usesend/ui/src/spinner"; import { toast } from "@usesend/ui/src/toaster"; import { formatDistanceToNow } from "date-fns"; +import { useState } from "react"; import { api } from "~/trpc/react"; @@ -17,6 +18,15 @@ const VERDICT: Record< reject: { label: "Guard: reject", variant: "destructive" }, }; +type Filter = "approve" | "unsure" | "reject" | "none"; + +const FILTERS: Array<[Filter, string]> = [ + ["approve", "Guard says let in"], + ["unsure", "Unsure"], + ["reject", "Guard says reject"], + ["none", "Not checked yet"], +]; + /** Pending applications with the Guard's advice, and a button for each decision. */ export function PendingApplications({ onDecided, @@ -25,7 +35,8 @@ export function PendingApplications({ onDecided?: (userId: number) => void; }) { const utils = api.useUtils(); - const pending = api.admin.pendingApplications.useQuery(); + const [filter, setFilter] = useState("approve"); + const pending = api.admin.pendingApplications.useQuery({ verdict: filter }); const letIn = api.admin.updateUserWaitlist.useMutation({ onSuccess: (user) => { @@ -80,22 +91,49 @@ export function PendingApplications({ ); } - const applications = pending.data ?? []; + const applications = pending.data?.items ?? []; + const counts = pending.data?.counts; + const total = counts + ? counts.approve + counts.unsure + counts.reject + counts.none + : 0; + const inFilter = counts?.[filter] ?? 0; return (

Pending applications

+

{total} waiting

+
+ +
+ {FILTERS.map(([key, label]) => ( + + ))} +
+ + {inFilter > applications.length ? (

- {applications.length >= 200 - ? "Oldest 200 shown" - : `${applications.length} waiting`} + Newest {applications.length} of {inFilter} shown. Decide some and the + next ones appear.

-
+ ) : null} {applications.length === 0 ? (
- Nobody is waiting. + {filter === "none" ? "Everything has been checked." : "Nothing here."}
) : null} diff --git a/apps/web/src/server/api/routers/admin-waitlist.trpc.test.ts b/apps/web/src/server/api/routers/admin-waitlist.trpc.test.ts index 490d3a33..e60a9377 100644 --- a/apps/web/src/server/api/routers/admin-waitlist.trpc.test.ts +++ b/apps/web/src/server/api/routers/admin-waitlist.trpc.test.ts @@ -13,6 +13,7 @@ const { mockDb, mockSendMail } = vi.hoisted(() => { waitlistApplication: { findMany: vi.fn(), findUnique: vi.fn(), + groupBy: vi.fn(), updateMany: vi.fn(), }, }; @@ -198,23 +199,43 @@ describe("rejecting someone", () => { }); describe("pendingApplications", () => { - it("shows the Guard's suggested approvals first", async () => { - mockDb.waitlistApplication.findMany.mockResolvedValue([ - { id: "a", guardVerdict: null }, - { id: "b", guardVerdict: "reject" }, - { id: "c", guardVerdict: "approve" }, - { id: "d", guardVerdict: "unsure" }, - ]); - const list = await caller.pendingApplications(); - expect(list.map((application) => application.id)).toEqual([ - "c", - "d", - "b", - "a", + it("shows one kind of advice at a time, newest first, with counts for all", async () => { + mockDb.waitlistApplication.groupBy.mockResolvedValue([ + { guardVerdict: "approve", _count: { _all: 3 } }, + { guardVerdict: null, _count: { _all: 800 } }, + { guardVerdict: "reject", _count: { _all: 2 } }, ]); + mockDb.waitlistApplication.findMany.mockResolvedValue([{ id: "c" }]); + + const result = await caller.pendingApplications({ verdict: "none" }); + + expect(result.counts).toEqual({ + approve: 3, + unsure: 0, + reject: 2, + none: 800, + }); + expect(result.items).toEqual([{ id: "c" }]); + expect(mockDb.waitlistApplication.findMany).toHaveBeenCalledWith( + expect.objectContaining({ + where: { + status: "PENDING", + user: { isWaitlisted: true }, + guardVerdict: null, + }, + orderBy: [{ createdAt: "desc" }, { id: "desc" }], + take: 100, + }), + ); + }); + + it("opens on the Guard's suggested approvals", async () => { + mockDb.waitlistApplication.groupBy.mockResolvedValue([]); + mockDb.waitlistApplication.findMany.mockResolvedValue([]); + await caller.pendingApplications(); expect(mockDb.waitlistApplication.findMany).toHaveBeenCalledWith( expect.objectContaining({ - where: { status: "PENDING", user: { isWaitlisted: true } }, + where: expect.objectContaining({ guardVerdict: "approve" }), }), ); }); diff --git a/apps/web/src/server/api/routers/admin.ts b/apps/web/src/server/api/routers/admin.ts index 87715400..579caad8 100644 --- a/apps/web/src/server/api/routers/admin.ts +++ b/apps/web/src/server/api/routers/admin.ts @@ -22,6 +22,9 @@ import { } from "~/server/service/guard-service"; import { TRPCError } from "@trpc/server"; +/** Applications shown per filter on the admin waitlist page, newest first. */ +const PENDING_PAGE = 100; + const waitlistUserSelection = { id: true, email: true, @@ -175,31 +178,57 @@ export const adminRouter = createTRPCRouter({ }), /** Pending applications with the Guard's advice: suggested approvals first. */ - pendingApplications: adminProcedure.query(async () => { - const applications = await db.waitlistApplication.findMany({ - where: { status: "PENDING", user: { isWaitlisted: true } }, - orderBy: { createdAt: "asc" }, - take: 200, - select: { - id: true, - userId: true, - email: true, - website: true, - emailTypes: true, - emailVolume: true, - listSource: true, - description: true, - guardVerdict: true, - guardNote: true, - createdAt: true, - }, - }); - const order = (verdict: string | null) => - ["approve", "unsure", "reject"].indexOf(verdict ?? "") + 1 || 4; - return applications.sort( - (a, b) => order(a.guardVerdict) - order(b.guardVerdict), - ); - }), + pendingApplications: adminProcedure + .input( + z + .object({ + /** The Guard's advice to show; "none" is not checked yet. */ + verdict: z + .enum(["approve", "unsure", "reject", "none"]) + .default("approve"), + }) + .default({}), + ) + .query(async ({ input }) => { + const pending = { + status: "PENDING" as const, + user: { isWaitlisted: true }, + }; + const [groups, applications] = await Promise.all([ + db.waitlistApplication.groupBy({ + by: ["guardVerdict"], + where: pending, + _count: { _all: true }, + }), + db.waitlistApplication.findMany({ + where: { + ...pending, + guardVerdict: input.verdict === "none" ? null : input.verdict, + }, + orderBy: [{ createdAt: "desc" }, { id: "desc" }], + take: PENDING_PAGE, + select: { + id: true, + userId: true, + email: true, + website: true, + emailTypes: true, + emailVolume: true, + listSource: true, + description: true, + guardVerdict: true, + guardNote: true, + createdAt: true, + }, + }), + ]); + const counts = { approve: 0, unsure: 0, reject: 0, none: 0 }; + for (const group of groups) { + const key = (group.guardVerdict ?? "none") as keyof typeof counts; + if (key in counts) counts[key] += group._count._all; + } + return { counts, items: applications, pageSize: PENDING_PAGE }; + }), findUserByEmail: adminProcedure .input( From 07ea454e45d0a4d1c86da36fa8c03ea2bc104fa9 Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Mon, 23 Nov 2026 18:42:55 +1100 Subject: [PATCH 2/2] fix(waitlist): keep the filters on screen while loading; plain toggle buttons instead of tabs --- .../admin/waitlist/PendingApplications.tsx | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx b/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx index f509a444..1ab3a498 100644 --- a/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx +++ b/apps/web/src/app/(dashboard)/admin/waitlist/PendingApplications.tsx @@ -36,7 +36,11 @@ export function PendingApplications({ }) { const utils = api.useUtils(); const [filter, setFilter] = useState("approve"); - const pending = api.admin.pendingApplications.useQuery({ verdict: filter }); + // Keep the last filter's data while the next loads, so counts don't blink. + const pending = api.admin.pendingApplications.useQuery( + { verdict: filter }, + { placeholderData: (previous) => previous }, + ); const letIn = api.admin.updateUserWaitlist.useMutation({ onSuccess: (user) => { @@ -68,29 +72,6 @@ export function PendingApplications({ ? reject.variables?.userId : undefined; - if (pending.isLoading) { - return ( -
- Loading applications... -
- ); - } - - if (pending.isError) { - return ( -
-

Couldn't load the applications: {pending.error.message}

- -
- ); - } - const applications = pending.data?.items ?? []; const counts = pending.data?.counts; const total = counts @@ -105,14 +86,16 @@ export function PendingApplications({

{total} waiting

-
+
{FILTERS.map(([key, label]) => (
+ {pending.isLoading ? ( +
+ Loading applications... +
+ ) : null} + + {pending.isError ? ( +
+

Couldn't load the applications: {pending.error.message}

+ +
+ ) : null} + {inFilter > applications.length ? (

Newest {applications.length} of {inFilter} shown. Decide some and the @@ -131,7 +133,7 @@ export function PendingApplications({

) : null} - {applications.length === 0 ? ( + {!pending.isLoading && !pending.isError && applications.length === 0 ? (
{filter === "none" ? "Everything has been checked." : "Nothing here."}