import { useCallback, useEffect, useState } from "react"; import { Loader2, RefreshCw } from "lucide-react"; import { api, asUiError, type AccountInfo, 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 { useHuman, useT, type Key } from "@/i18n"; const LEVELS = [0, 1, 2, 3] as const; const inputClass = "w-64 rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none focus:border-gold"; type Editor = { name: string; mode: "password" | "rename" | "delete"; characters?: number }; /** Everyone who can log in to the server, with the changes an owner usually needs: password, access level, name. */ export function AccountsCard({ serverId, online, reloadKey }: { serverId: string; online: boolean | null; reloadKey: number }) { const t = useT(); const human = useHuman(); const [accounts, setAccounts] = useState(null); const [listError, setListError] = useState(false); const [editor, setEditor] = useState(null); const [newName, setNewName] = useState(""); const [confirmName, setConfirmName] = useState(""); const [password, setPassword] = useState(""); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [note, setNote] = useState(null); const load = useCallback(async () => { try { setAccounts(await api.listAccounts(serverId)); setListError(false); } catch { setAccounts(null); setListError(true); } }, [serverId]); useEffect(() => { void load(); }, [load, reloadKey, online]); async function run(label: string, fn: () => Promise, ok: string) { setBusy(label); setError(null); setNote(null); try { await fn(); setNote(ok); setEditor(null); setPassword(""); setNewName(""); setConfirmName(""); await load(); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); } } const passOk = password.length >= 6 && password.length <= 16 && /^[\x21-\x7e]+$/.test(password) && !/["']/.test(password); const nameOk = /^[A-Za-z0-9]{3,17}$/.test(newName); return (

{t("acc.title")}

{t("acc.text")}

{listError &&

{t("acc.unavailable")}

} {accounts && accounts.length === 0 &&

{t("acc.empty")}

} {accounts && accounts.length > 0 && (
{accounts.map((a) => ( ))}
{t("acc.col.name")} {t("acc.col.chars")} {t("acc.col.access")} {t("acc.col.last")}
{a.name} {a.online && {t("acc.online")}} {a.characters} {a.last_login ?? t("acc.never")}
)} {online === false && accounts && accounts.length > 0 &&

{t("acc.needsRunning")}

} {editor && editor.mode === "delete" && (

{t("acc.deleteFor", { name: editor.name })}

{t("acc.deleteWarn", { n: editor.characters ?? 0 })}

setConfirmName(e.target.value)} autoComplete="off" />
)} {editor && editor.mode !== "delete" && (

{editor.mode === "password" ? t("acc.changeFor", { name: editor.name }) : t("acc.renameFor", { name: editor.name })}

{editor.mode === "rename" && (
setNewName(e.target.value)} autoComplete="off" />

{t("acc.nameRule")}

)}
setPassword(e.target.value)} autoComplete="new-password" />

{t("acc.passRule")}

{editor.mode === "rename" &&

{t("acc.renameNote")}

}
)} {note &&

{note}

} {error &&

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

}
); }