import { useCallback, useEffect, useRef, useState } from "react"; import { api, asUiError, type ConsoleLine, type ConsoleSource, type UiError } from "@/lib/api"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useHuman, useT, type Key } from "@/i18n"; const TABS: { id: ConsoleSource; label: Key }[] = [ { id: "world", label: "con.tab.world" }, { id: "auth", label: "con.tab.auth" }, { id: "database", label: "con.tab.database" }, { id: "manager", label: "con.tab.manager" }, ]; const TONE = { info: "text-ink/80", warn: "text-warn", error: "text-bad" } as const; export function ConsolePage({ serverId }: { serverId: string }) { const t = useT(); const human = useHuman(); const [tab, setTab] = useState("world"); const [filter, setFilter] = useState(""); const [lines, setLines] = useState([]); const [live, setLive] = useState(true); const [missing, setMissing] = useState(null); const [cmd, setCmd] = useState(""); const [reply, setReply] = useState(null); const [error, setError] = useState(null); const [copied, setCopied] = useState(false); const box = useRef(null); const load = useCallback(async () => { try { setLines(await api.consoleTail(serverId, tab, filter || undefined)); setMissing(null); } catch (e) { setLines([]); setMissing(asUiError(e).technical); } }, [serverId, tab, filter]); useEffect(() => { void load(); if (!live) return; const iv = setInterval(load, 2000); return () => clearInterval(iv); }, [load, live]); useEffect(() => { if (live && box.current) box.current.scrollTop = box.current.scrollHeight; }, [lines, live]); async function send() { setError(null); setReply(null); try { const risk = await api.consoleRisk(cmd); if (risk === "dangerous" && !window.confirm(t("con.confirmRisky", { cmd }))) return; setReply((await api.consoleCommand(serverId, cmd, risk === "dangerous")) || t("common.noOutput")); setCmd(""); } catch (e) { setError(asUiError(e)); } } const errors = lines.filter((l) => l.level === "error").map((l) => l.text); return (

{t("con.title")}

{t("q.console")}

{TABS.map((tb) => ( ))}
setFilter(e.target.value)} className="ml-auto w-56 rounded-md border border-line bg-bg px-3 py-1.5 text-sm outline-none focus:border-gold" />
{missing ?

{missing}

: lines.length === 0 ?

{t("con.empty")}

: lines.map((l, i) => (
{l.level !== "info" && {l.level}: } {l.text}
))}
{tab === "world" && (

{t("con.commandsNote")}

{ e.preventDefault(); void send(); }}> setCmd(e.target.value)} placeholder="server info" className="selectable flex-1 rounded-md border border-line bg-bg px-3 py-2 font-mono text-sm outline-none focus:border-gold" />
{reply &&
{reply}
} {error &&

{error.human.code === "unknown" ? error.technical : human(error.human).message}

}
)}
); }