import { useEffect, useState } from "react"; import { api, asUiError, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { PasswordInput } from "@/components/ui/password-input"; import { AccountsCard } from "@/screens/AccountsCard"; import { useHuman, useT } from "@/i18n"; export function PlayersPage({ serverId }: { serverId: string }) { const t = useT(); const human = useHuman(); const [online, setOnline] = useState(null); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [admin, setAdmin] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [done, setDone] = useState(null); const [created, setCreated] = useState(0); useEffect(() => { let alive = true; const poll = () => api.status(serverId).then((s) => alive && setOnline(s.observed.world.state === "running")).catch(() => {}); void poll(); const t = setInterval(poll, 3000); return () => { alive = false; clearInterval(t); }; }, [serverId]); async function create() { setBusy(true); setError(null); setDone(null); try { await api.createAccount(serverId, username, password, admin); setDone(username); setCreated((n) => n + 1); setUsername(""); setPassword(""); } catch (e) { setError(asUiError(e)); } finally { setBusy(false); } } const input = "w-72 rounded-md border border-line bg-bg px-3 py-2.5 text-[15px] outline-none focus:border-gold"; return (

{t("players.title")}

{t("q.players")}

{t("players.createTitle")}

{t("players.createText")}

{online === false &&

{t("players.startFirst")}

}
setUsername(e.target.value)} autoComplete="off" />
setPassword(e.target.value)} autoComplete="new-password" />
{done &&

{t("players.created", { name: done })}

} {error && (

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

)}
); }