1
0
Fork 0

feat(minigames): add an admin close/reopen control for periods

Phase 7 of the minigame periods feature:

- New admin component `PeriodCloseControl` renders above the minigame-periods
  list: it shows the recent periods with their status and a Close now / Reopen
  button that drives POST /api/minigame-periods/close (gated on
  minigame-periods:close; a 403 is shown inline rather than swallowed).
- Registered via `admin.components.beforeListTable` and the import map
  regenerated.

Registry CRUD and the period/entry/achievement review lists already exist as
permission-gated admin collection views, so this was the only missing surface.
Verified end to end in the running admin panel: an open period closed, its
status flipped and closedAt stamped, then reopened.
This commit is contained in:
Jason Fraley 2026-10-11 16:48:26 -04:00
parent a1ab247b7b
commit 08f2630442
3 changed files with 178 additions and 0 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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<PeriodRow[] | null>(null);
const [busy, setBusy] = useState<number | null>(null);
const [message, setMessage] = useState<string | null>(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 (
<div
style={{
border: "1px solid var(--theme-elevation-150)",
borderRadius: 8,
padding: "12px 16px",
marginBottom: 16,
}}
>
<div style={{ display: "flex", alignItems: "baseline", gap: 8 }}>
<strong>Period control</strong>
<span style={{ opacity: 0.7, fontSize: 13 }}>
Close the open period early, or reopen a closed one. The period tick usually does this on
schedule.
</span>
</div>
{message && (
<p style={{ marginTop: 8, fontSize: 13 }} role="status">
{message}
</p>
)}
<div style={{ marginTop: 12, display: "flex", flexDirection: "column", gap: 8 }}>
{rows.length === 0 && (
<span style={{ opacity: 0.7, fontSize: 13 }}>
No periods yet. The first one appears when a Core run is recorded or the period tick runs.
</span>
)}
{rows.slice(0, 6).map((row) => (
<div
key={row.id}
style={{ display: "flex", alignItems: "center", gap: 12, flexWrap: "wrap" }}
>
<span style={{ minWidth: 220 }}>{row.label}</span>
<span style={{ opacity: 0.7, fontSize: 12 }}>
{dateOnly(row.startDate)} to {dateOnly(row.endDate)} UTC
</span>
<span
style={{
fontSize: 11,
textTransform: "uppercase",
letterSpacing: 0.5,
opacity: 0.8,
}}
>
{row.status}
</span>
<span style={{ marginLeft: "auto" }}>
{row.status === "open" ? (
<Button
type="button"
buttonStyle="secondary"
size="small"
disabled={busy !== null}
onClick={() => void act("close", row.id)}
>
{busy === row.id ? "Closing..." : "Close now"}
</Button>
) : (
<Button
type="button"
buttonStyle="subtle"
size="small"
disabled={busy !== null}
onClick={() => void act("reopen", row.id)}
>
{busy === row.id ? "Reopening..." : "Reopen"}
</Button>
)}
</span>
</div>
))}
</div>
</div>
);
}