"use client"; import { useState } from "react"; import { GenerateTab } from "@/components/import/GenerateTab"; import type { ArcadeGameKey, ArcadeImportBatchPreview } from "@/types/arcade"; export function ArcadeImportModal({ classId, gameType, onClose, onImported, }: { classId: string; gameType: ArcadeGameKey; onClose: () => void; onImported: () => void; }) { const [tab, setTab] = useState<"generate" | "import">("import"); const [rawJson, setRawJson] = useState(""); const [names, setNames] = useState([]); const [preview, setPreview] = useState(null); const [error, setError] = useState(""); const [details, setDetails] = useState([]); const [acknowledged, setAcknowledged] = useState(false); const [busy, setBusy] = useState(false); async function previewImport() { setBusy(true); setError(""); setDetails([]); setPreview(null); try { const response = await fetch("/api/arcade/import/preview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ gameType, rawJson }), }); const data = await response.json(); if (!response.ok) { setError(data.error ?? "Import preview failed"); setDetails(data.details ?? []); return; } setPreview(data); setNames(data.packs.map((pack: { name: string }) => pack.name)); } finally { setBusy(false); } } async function saveImport() { if (!preview || names.some((name) => !name.trim())) return; setBusy(true); setError(""); try { const response = await fetch("/api/arcade/packs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ classId, gameType, rawJson, ...(gameType === "connections" ? { names: names.map((name) => name.trim()) } : { name: names[0]?.trim() }), warningsAcknowledged: acknowledged, }), }); const data = await response.json(); if (!response.ok) { setError(data.error ?? "Import failed"); setDetails(data.details ?? []); return; } onImported(); } finally { setBusy(false); } } return (
{(["generate", "import"] as const).map((item) => ( ))}
{tab === "generate" ? : (

{gameType === "connections" ? "Paste one pack object or an array of up to ten packs." : "Paste one Crossword pack containing exactly 80 entries."} Every board is validated before anything is saved.