import { useEffect, useState } from "react"; import { Loader2 } from "lucide-react"; import { api, asUiError, type ReportContext, type ReportTarget, type ReportTargetId, type UiError } from "@/lib/api"; import { MAX_LINK, issueUrl, reportBody } from "@/lib/report"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useHuman, useT, type Key } from "@/i18n"; const field = "mt-1 block w-full rounded-md border border-line bg-bg px-3 py-2.5 text-[15px] outline-none focus:border-gold"; const TARGET_LABEL: Record = { manager: "rep.target.manager", companions: "rep.target.companions", squid: "rep.target.squid", }; const FALLBACK: ReportTarget = { id: "manager", repo: "Corfirean/coa-server-manager", ours: true }; /** * A problem report that becomes a prefilled GitHub issue in the repository of whatever it is about. One button collects * the diagnostics (and shows the file in its folder) and opens the issue; the person reviews it and submits it on GitHub. */ export function ReportPage({ serverId }: { serverId: string }) { const t = useT(); const human = useHuman(); const [ctx, setCtx] = useState(null); const [targets, setTargets] = useState([]); const [targetId, setTargetId] = useState(null); const [title, setTitle] = useState(""); const [what, setWhat] = useState(""); const [expected, setExpected] = useState(""); const [steps, setSteps] = useState(""); const [withDiag, setWithDiag] = useState(true); const [file, setFile] = useState(null); const [busy, setBusy] = useState(false); const [note, setNote] = useState(null); const [error, setError] = useState(null); const [showPreview, setShowPreview] = useState(false); useEffect(() => { void api.reportContext(serverId).then(setCtx).catch(() => setCtx(null)); void api.reportTargets().then(setTargets).catch(() => setTargets([])); }, [serverId]); // start on the bot system that is switched on, until the person picks one const chosen: ReportTargetId = targetId ?? ctx?.suggested_target ?? "manager"; const shown = targets.length ? targets : [FALLBACK]; const target = shown.find((x) => x.id === chosen) ?? shown[0]; function bodyFor(hasFile: boolean): string { return reportBody({ managerVersion: ctx?.manager_version ?? "", windows: ctx?.windows ?? "", installKind: ctx?.install_kind ?? "new", serverVersion: ctx?.server_version ?? null, target: target.id, botsVersion: ctx?.bots_version ?? null, what, expected, steps, diagnostics: hasFile, }); } const issueTitle = target.ours ? `[Bug] ${title.trim()}` : title.trim(); const ready = title.trim().length > 0 && what.trim().length > 0; /** Collects the diagnostics once; the Manager shows the file in its folder. */ async function collect(): Promise { if (file) return file; const path = await api.exportDiagnostics(serverId); setFile(path); return path; } async function collectOnly() { setBusy(true); setError(null); try { await collect(); } catch (e) { setError(asUiError(e)); } finally { setBusy(false); } } /** One button: collect the diagnostics (if asked), then open the issue with everything filled in. */ async function openIssue() { setBusy(true); setError(null); setNote(null); try { const path = withDiag ? await collect() : null; const body = bodyFor(path !== null); const full = issueUrl(target, issueTitle, body); if (full.length <= MAX_LINK) { await api.openLink(full); setNote(t("rep.opened")); } else { await navigator.clipboard.writeText(body); await api.openLink(issueUrl(target, issueTitle, "Paste the report text here (it was copied to the clipboard by CoA Server Manager).")); setNote(t("rep.openedLong")); } } catch (e) { setError(asUiError(e)); } finally { setBusy(false); } } async function copy() { await navigator.clipboard.writeText(`${issueTitle}\n\n${bodyFor(file !== null)}`); setNote(t("rep.copied")); } return (

{t("rep.title")}

{t("q.report")}

{t("rep.intro")}

{t("rep.targetTitle")}

{shown.map((x) => ( ))}

{t("rep.targetPublic", { repo: target.repo })}

{t("rep.autoTitle")}

{t("rep.manager")}
{ctx?.manager_version ?? "…"}
{t("rep.windows")}
{ctx ? ctx.windows || "—" : "…"}
{t("rep.kind")}
{ctx ? (ctx.install_kind === "new" ? t("rep.kindNew") : t("rep.kindImported")) : "…"}
{ctx?.server_version && ( <>
{t("rep.server")}
{ctx.server_version}
)} {target.id !== "manager" && ctx?.bots_version && ( <>
{t("rep.botsVersion")}
{ctx.bots_version}
)}
setTitle(e.target.value)} placeholder={t("rep.summaryHint")} />