diff --git a/src/app/(payload)/admin/importMap.js b/src/app/(payload)/admin/importMap.js index cd2ed75a..822a0acb 100644 --- a/src/app/(payload)/admin/importMap.js +++ b/src/app/(payload)/admin/importMap.js @@ -35,6 +35,7 @@ import { default as default_0edde40a6ccd30c893b1227360f69360 } from '@/component import { default as default_2d468cc5cf0b647ffca3201592c20825 } from '@/components/static/WarningMessage' import { AudioPreviewField as AudioPreviewField_6bccef9bf4ea41ed573ab9f36fa579b2 } from '@/components/admin/voiceover/AudioPreviewField' import { default as default_7fa52484aec46e988e9cfb5e86c0c759 } from '@/components/admin/narrative-flow/NarrativeFlowEditor' +import { default as default_b7f86fb8e028bca592f57e6d387a0380 } from '@/components/admin/minigames/PeriodCloseControl' import { SelectField as SelectField_4490b89d4413c1ffaecdacfe72efaf73 } from '@ai-stack/payloadcms/client' import { PromptEditorField as PromptEditorField_4490b89d4413c1ffaecdacfe72efaf73 } from '@ai-stack/payloadcms/client' import { default as default_b16af798585cda7cfed92f7b1dd0c707 } from '@/components/admin/shims/ShimContentField' @@ -87,6 +88,7 @@ export const importMap = { "@/components/static/WarningMessage#default": default_2d468cc5cf0b647ffca3201592c20825, "@/components/admin/voiceover/AudioPreviewField#AudioPreviewField": AudioPreviewField_6bccef9bf4ea41ed573ab9f36fa579b2, "@/components/admin/narrative-flow/NarrativeFlowEditor#default": default_7fa52484aec46e988e9cfb5e86c0c759, + "@/components/admin/minigames/PeriodCloseControl#default": default_b7f86fb8e028bca592f57e6d387a0380, "@ai-stack/payloadcms/client#SelectField": SelectField_4490b89d4413c1ffaecdacfe72efaf73, "@ai-stack/payloadcms/client#PromptEditorField": PromptEditorField_4490b89d4413c1ffaecdacfe72efaf73, "@/components/admin/shims/ShimContentField#default": default_b16af798585cda7cfed92f7b1dd0c707, diff --git a/src/collections/minigames/MinigamePeriods.ts b/src/collections/minigames/MinigamePeriods.ts index fad28c47..3c3fd9d8 100644 --- a/src/collections/minigames/MinigamePeriods.ts +++ b/src/collections/minigames/MinigamePeriods.ts @@ -23,6 +23,9 @@ export const MinigamePeriods: CollectionConfig = { defaultColumns: ["label", "startDate", "endDate", "status", "closedAt"], description: "Anchored scoring periods. Read-only here; the period tick and the close/reopen control manage them.", + components: { + beforeListTable: ["@/components/admin/minigames/PeriodCloseControl"], + }, }, access: { read: () => true, diff --git a/src/components/admin/minigames/PeriodCloseControl.tsx b/src/components/admin/minigames/PeriodCloseControl.tsx new file mode 100644 index 00000000..aab7b4d9 --- /dev/null +++ b/src/components/admin/minigames/PeriodCloseControl.tsx @@ -0,0 +1,173 @@ +"use client"; + +import { useCallback, useEffect, useState } from "react"; +import { useRouter } from "next/navigation"; +import { Button } from "@payloadcms/ui"; + +/** + * Admin close/reopen control for minigame scoring periods. + * + * Rendered above the minigame-periods list. It reads periods through the REST + * API (admin session cookie) and drives POST /api/minigame-periods/close, which + * is gated on the `minigame-periods:close` permission; a 403 is surfaced inline. + * The scheduled sweep lives in the period-tick bin, so this is the manual + * override for closing early or reopening a closed period. + */ + +interface PeriodRow { + id: number; + label: string; + status: string; + startDate: string; + endDate: string; +} + +const dateOnly = (iso: string): string => (iso ? iso.slice(0, 10) : ""); + +export default function PeriodCloseControl() { + const router = useRouter(); + const [rows, setRows] = useState(null); + const [busy, setBusy] = useState(null); + const [message, setMessage] = useState(null); + + const load = useCallback(async () => { + try { + const res = await fetch("/api/minigame-periods?limit=20&sort=-startDate&depth=0", { + credentials: "include", + }); + if (!res.ok) throw new Error("load failed"); + const json = (await res.json()) as { docs?: PeriodRow[] }; + setRows( + (json.docs ?? []).map((doc) => ({ + id: doc.id, + label: doc.label, + status: doc.status, + startDate: doc.startDate, + endDate: doc.endDate, + })), + ); + } catch { + setRows([]); + } + }, []); + + useEffect(() => { + void load(); + }, [load]); + + const act = async (action: "close" | "reopen", periodId: number) => { + setBusy(periodId); + setMessage(null); + try { + const res = await fetch("/api/minigame-periods/close", { + method: "POST", + headers: { "content-type": "application/json" }, + credentials: "include", + body: JSON.stringify({ action, periodId }), + }); + const json = (await res.json().catch(() => ({}))) as { + ok?: boolean; + error?: string; + closed?: { label?: string }[]; + }; + if (!res.ok || json.ok === false) { + setMessage( + res.status === 403 + ? "You do not have permission to close or reopen periods." + : (json.error ?? `Request failed (${res.status}).`), + ); + return; + } + setMessage( + action === "close" + ? `Closed "${json.closed?.[0]?.label ?? "period"}".` + : "Period reopened.", + ); + await load(); + router.refresh(); + } catch { + setMessage("Request failed."); + } finally { + setBusy(null); + } + }; + + if (rows === null) return null; + + return ( +
+
+ Period control + + Close the open period early, or reopen a closed one. The period tick usually does this on + schedule. + +
+ + {message && ( +

+ {message} +

+ )} + +
+ {rows.length === 0 && ( + + No periods yet. The first one appears when a Core run is recorded or the period tick runs. + + )} + {rows.slice(0, 6).map((row) => ( +
+ {row.label} + + {dateOnly(row.startDate)} to {dateOnly(row.endDate)} UTC + + + {row.status} + + + {row.status === "open" ? ( + + ) : ( + + )} + +
+ ))} +
+
+ ); +}