feat(admin): marketplace console + analytics degraded banners
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
f150aea8b9
commit
025af147cd
9 files changed
+373
-57
No files matched your search
@@ -0,0 +1,171 @@
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { cancelMarketplaceListing } from "@/actions/admin-marketplace";
|
||||
import { AdminSimplePager } from "@/components/admin/admin-simple-pager";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
function fmt(ts: number): string {
|
||||
if (!ts) return "—";
|
||||
return new Date(ts * 1000).toISOString().slice(0, 16).replace("T", " ");
|
||||
}
|
||||
|
||||
export default async function AdminMarketplacePage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<Record<string, string>>;
|
||||
}) {
|
||||
const { session, permissions } = await getAdminContext();
|
||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||
redirect("/admin");
|
||||
}
|
||||
|
||||
const t = await getTranslations("pages.admin.marketplace");
|
||||
const canEdit = canAccess(permissions, PERMS.SHOP_EDIT, session.user.rank);
|
||||
const raw = await searchParams;
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
const filter = raw.filter === "sold" ? "sold" : "active";
|
||||
|
||||
const where = filter === "sold" ? { state: 2 } : { state: 1 };
|
||||
|
||||
let total = 0;
|
||||
let rows: Awaited<ReturnType<typeof prisma.marketplaceItems.findMany>> = [];
|
||||
let activeCount = 0;
|
||||
let soldCount = 0;
|
||||
let loadError = false;
|
||||
|
||||
try {
|
||||
[total, activeCount, soldCount] = await Promise.all([
|
||||
prisma.marketplaceItems.count({ where }),
|
||||
prisma.marketplaceItems.count({ where: { state: 1 } }),
|
||||
prisma.marketplaceItems.count({ where: { state: 2 } }),
|
||||
]);
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
rows = await prisma.marketplaceItems.findMany({
|
||||
where,
|
||||
orderBy: { timestamp: "desc" },
|
||||
skip: pagination.offset,
|
||||
take: pagination.perPage,
|
||||
});
|
||||
} catch {
|
||||
loadError = true;
|
||||
}
|
||||
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
const userIds = [
|
||||
...new Set(rows.map((r) => r.userId).filter((id) => id > 0)),
|
||||
];
|
||||
const users =
|
||||
userIds.length > 0
|
||||
? await prisma.user
|
||||
.findMany({
|
||||
where: { id: { in: userIds } },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const usernameById = new Map(users.map((u) => [u.id, u.username]));
|
||||
|
||||
return (
|
||||
<main>
|
||||
{loadError ? (
|
||||
<div className="mb-4 rounded-lg bg-[var(--admin-error-subtle)] p-3 text-xs text-[var(--admin-error)]">
|
||||
{t("loadError")}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div className="mb-4 flex flex-wrap gap-2">
|
||||
<Link
|
||||
href="/admin/marketplace?filter=active"
|
||||
className={`btn btn-sm ${filter === "active" ? "btn-primary" : "btn-ghost"}`}
|
||||
>
|
||||
{t("filterActive")}
|
||||
</Link>
|
||||
<Link
|
||||
href="/admin/marketplace?filter=sold"
|
||||
className={`btn btn-sm ${filter === "sold" ? "btn-primary" : "btn-ghost"}`}
|
||||
>
|
||||
{t("filterSold")}
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
||||
<StatusCard label={t("activeListings")} value={activeCount} icon="🏪" />
|
||||
<StatusCard label={t("soldListings")} value={soldCount} icon="✅" />
|
||||
</div>
|
||||
|
||||
<section>
|
||||
<h2 className="admin-section-title">
|
||||
{filter === "sold" ? t("soldListings") : t("activeListings")} ({total}
|
||||
)
|
||||
</h2>
|
||||
{rows.length === 0 ? (
|
||||
<div className="admin-empty">{t("empty")}</div>
|
||||
) : (
|
||||
<div className="admin-card overflow-x-auto" style={{ padding: 0 }}>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{t("colId")}</th>
|
||||
<th>{t("colItem")}</th>
|
||||
<th>{t("colSeller")}</th>
|
||||
<th>{t("colPrice")}</th>
|
||||
<th>{t("colListed")}</th>
|
||||
{filter === "sold" ? <th>{t("colSold")}</th> : null}
|
||||
{canEdit && filter === "active" ? (
|
||||
<th>{t("colActions")}</th>
|
||||
) : null}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((row) => (
|
||||
<tr key={row.id}>
|
||||
<td className="muted">{row.id}</td>
|
||||
<td>
|
||||
<code>{row.itemId}</code>
|
||||
</td>
|
||||
<td>
|
||||
<Link href={`/admin/users/show/${row.userId}`}>
|
||||
{usernameById.get(row.userId) ?? `#${row.userId}`}
|
||||
</Link>
|
||||
</td>
|
||||
<td>{row.price}</td>
|
||||
<td className="muted">{fmt(row.timestamp)}</td>
|
||||
{filter === "sold" ? (
|
||||
<td className="muted">{fmt(row.soldTimestamp)}</td>
|
||||
) : null}
|
||||
{canEdit && filter === "active" ? (
|
||||
<td>
|
||||
<form action={cancelMarketplaceListing}>
|
||||
<input type="hidden" name="id" value={row.id} />
|
||||
<button
|
||||
type="submit"
|
||||
className="btn btn-ghost btn-sm"
|
||||
>
|
||||
{t("cancel")}
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
) : null}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
<AdminSimplePager
|
||||
page={pagination.page}
|
||||
lastPage={pagination.lastPage}
|
||||
total={total}
|
||||
basePath="/admin/marketplace"
|
||||
params={filter === "sold" ? { filter: "sold" } : undefined}
|
||||
/>
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user