119 lines
4.1 KiB
TypeScript
119 lines
4.1 KiB
TypeScript
import type { Metadata } from "next";
|
|
import { DollarSign, TrendingUp, CreditCard } from "lucide-react";
|
|
import { listSubscriptions, SUBS_PAGE_SIZE, type AdminSubRow } from "@/lib/admin/billing";
|
|
import { getOverview } from "@/lib/admin/metrics";
|
|
import { formatPrice } from "@/lib/utils";
|
|
import { PageHeader } from "@/components/app/page-header";
|
|
import { StatCard } from "@/components/admin/ui/stat-card";
|
|
import { DataTable, TableToolbar, type Column } from "@/components/admin/ui/data-table";
|
|
import { SearchInput, FilterSelect, Pagination } from "@/components/admin/ui/table-controls";
|
|
import { SubscriptionRowActions } from "@/components/admin/subscription-row-actions";
|
|
import { Badge } from "@/components/ui/badge";
|
|
|
|
export const metadata: Metadata = { title: "Admin · Subscriptions" };
|
|
|
|
export default async function AdminSubscriptionsPage({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<Record<string, string | undefined>>;
|
|
}) {
|
|
const sp = await searchParams;
|
|
const page = Math.max(1, Number(sp.page ?? "1"));
|
|
const [m, { rows, total }] = await Promise.all([
|
|
getOverview("30d"),
|
|
listSubscriptions({ status: sp.status, provider: sp.provider, search: sp.q, page }),
|
|
]);
|
|
|
|
const columns: Column<AdminSubRow>[] = [
|
|
{
|
|
key: "customer",
|
|
header: "Customer",
|
|
cell: (s) => <span className="truncate font-medium">{s.customer}</span>,
|
|
},
|
|
{ key: "plan", header: "Plan", cell: (s) => <span className="capitalize">{s.plan}</span> },
|
|
{ key: "provider", header: "Provider", cell: (s) => <span className="capitalize">{s.provider}</span> },
|
|
{
|
|
key: "status",
|
|
header: "Status",
|
|
cell: (s) => (
|
|
<Badge variant={["active", "trialing"].includes(s.status) ? "success" : "secondary"}>
|
|
{s.cancelAtPeriodEnd ? "cancels soon" : s.status}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
key: "renews",
|
|
header: "Renews",
|
|
cell: (s) => (
|
|
<span className="text-muted-foreground">{s.periodEnd ? s.periodEnd.toLocaleDateString() : "—"}</span>
|
|
),
|
|
},
|
|
{
|
|
key: "actions",
|
|
header: "",
|
|
align: "right",
|
|
cell: (s) => (
|
|
<SubscriptionRowActions
|
|
sub={{
|
|
id: s.id,
|
|
referenceId: s.referenceId,
|
|
provider: s.provider,
|
|
status: s.status,
|
|
cancelAtPeriodEnd: s.cancelAtPeriodEnd,
|
|
}}
|
|
/>
|
|
),
|
|
},
|
|
];
|
|
|
|
return (
|
|
<>
|
|
<PageHeader title="Subscriptions" description="Revenue and active subscriptions." />
|
|
|
|
<div className="mb-6 grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
|
<StatCard label="MRR" value={formatPrice(m.mrr)} icon={DollarSign} />
|
|
<StatCard label="ARR" value={formatPrice(m.arr)} icon={TrendingUp} />
|
|
<StatCard label="Stripe" value={String(m.providerSplit.stripe)} icon={CreditCard} />
|
|
<StatCard
|
|
label="PayPal"
|
|
value={String(m.providerSplit.paypal)}
|
|
icon={CreditCard}
|
|
hint={m.providerSplit.comp ? `${m.providerSplit.comp} comped` : undefined}
|
|
/>
|
|
</div>
|
|
|
|
<TableToolbar>
|
|
<SearchInput placeholder="Search customer…" />
|
|
<div className="flex flex-wrap gap-2">
|
|
<FilterSelect
|
|
param="status"
|
|
placeholder="Status"
|
|
allLabel="All status"
|
|
options={[
|
|
{ value: "active", label: "Active" },
|
|
{ value: "trialing", label: "Trialing" },
|
|
{ value: "past_due", label: "Past due" },
|
|
{ value: "canceled", label: "Canceled" },
|
|
{ value: "paused", label: "Paused" },
|
|
]}
|
|
/>
|
|
<FilterSelect
|
|
param="provider"
|
|
placeholder="Provider"
|
|
allLabel="All providers"
|
|
options={[
|
|
{ value: "stripe", label: "Stripe" },
|
|
{ value: "paypal", label: "PayPal" },
|
|
{ value: "comp", label: "Comped" },
|
|
]}
|
|
/>
|
|
</div>
|
|
</TableToolbar>
|
|
<DataTable columns={columns} rows={rows} getRowKey={(s) => s.id} empty="No subscriptions match your filters." />
|
|
<div className="mt-4">
|
|
<Pagination page={page} pageSize={SUBS_PAGE_SIZE} total={total} />
|
|
</div>
|
|
</>
|
|
);
|
|
}
|