import Link from "next/link"; import { redirect } from "next/navigation"; import { getTranslations } from "next-intl/server"; import { StatusCard } from "@/components/admin/dashboard"; import { formatDate } from "@/lib/format-date"; import { canAccess, getAdminContext, PERMS } from "@/lib/permissions"; import { prisma } from "@/lib/prisma"; type Campaign = { id: number; name: string; image: string; startTimestamp: number; totalDays: number; lockExpired: string; enabled: string; }; export default async function AdminCalendar() { const { session, permissions } = await getAdminContext(); if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) { redirect("/admin"); } const t = await getTranslations("pages.admin.calendar"); let campaigns: Campaign[] = []; let loadError = false; try { campaigns = await prisma.calendarCampaigns.findMany({ select: { id: true, name: true, image: true, startTimestamp: true, totalDays: true, lockExpired: true, enabled: true, }, orderBy: { id: "desc" }, take: 200, }); } catch { campaigns = []; loadError = true; } const rewardCounts = new Map(); let totalRewards = 0; try { const grouped = await prisma.calendarRewards.groupBy({ by: ["campaignId"], _count: { _all: true }, }); for (const g of grouped) { rewardCounts.set(g.campaignId, g._count._all); totalRewards += g._count._all; } } catch { loadError = true; } const enabledCount = campaigns.filter((c) => c.enabled === "1").length; return (
{loadError ? (
Could not load some calendar data (database unavailable).
) : null}
0 ? "ok" : "neutral"} icon="โœ…" />
{campaigns.length === 0 ? (
{t("noCampaigns")}
) : (
{campaigns.map((c) => { const enabled = c.enabled === "1"; const count = rewardCounts.get(c.id) ?? 0; return ( {c.image ? ( // eslint-disable-next-line @next/next/no-img-element {c.name} ) : (
)}

{c.name || `#${c.id}`}

{enabled ? t("statusEnabled") : t("statusDisabled")}

{t("campaignMeta", { id: c.id, days: c.totalDays, rewards: count, })}

{t("startsAt", { date: formatDate( new Date(c.startTimestamp * 1000), "date", ), })} {c.lockExpired === "1" ? ` ยท ${t("locksExpired")}` : ""}

); })}
)}
); }