184 lines
5.5 KiB
TypeScript
184 lines
5.5 KiB
TypeScript
import { Store } from "lucide-react";
|
|
import Link from "next/link";
|
|
import { redirect } from "next/navigation";
|
|
import { getTranslations } from "next-intl/server";
|
|
import { cancelMarketplaceListing } from "@/actions/admin-marketplace";
|
|
import { AdminPageShell } from "@/components/admin/admin-page-shell";
|
|
import { AdminSimplePager } from "@/components/admin/admin-simple-pager";
|
|
import { StatusCard } from "@/components/admin/dashboard";
|
|
import { Button } from "@/components/ui/button";
|
|
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
|
import { formatDate } from "@/lib/format-date";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
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 (
|
|
<AdminPageShell icon={Store} title={t("title")} subtitle={t("subtitle")}>
|
|
<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">
|
|
<Button
|
|
variant={filter === "active" ? "default" : "ghost"}
|
|
size="sm"
|
|
asChild
|
|
>
|
|
<Link href="/admin/marketplace?filter=active">
|
|
{t("filterActive")}
|
|
</Link>
|
|
</Button>
|
|
<Button
|
|
variant={filter === "sold" ? "default" : "ghost"}
|
|
size="sm"
|
|
asChild
|
|
>
|
|
<Link href="/admin/marketplace?filter=sold">{t("filterSold")}</Link>
|
|
</Button>
|
|
</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">
|
|
{formatDate(new Date(row.timestamp * 1000))}
|
|
</td>
|
|
{filter === "sold" ? (
|
|
<td className="muted">
|
|
{formatDate(
|
|
row.soldTimestamp
|
|
? new Date(row.soldTimestamp * 1000)
|
|
: null,
|
|
)}
|
|
</td>
|
|
) : null}
|
|
{canEdit && filter === "active" ? (
|
|
<td>
|
|
<form action={cancelMarketplaceListing}>
|
|
<input type="hidden" name="id" value={row.id} />
|
|
<Button type="submit" variant="ghost" size="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>
|
|
</AdminPageShell>
|
|
);
|
|
}
|