import { useEffect, useState } from "react"; import { api, asUiError, type RealmName } from "@/lib/api"; import { Card } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { useT } from "@/i18n"; const MAX_CHARS = 32; export function RealmNameSettings({ serverId }: { serverId: string }) { const t = useT(); const [realms, setRealms] = useState(null); const [drafts, setDrafts] = useState>({}); const [busy, setBusy] = useState(false); const [saved, setSaved] = useState(null); const [error, setError] = useState(null); function show(list: RealmName[]) { setRealms(list); setDrafts(Object.fromEntries(list.map(r => [r.id, r.name]))); } useEffect(() => { let alive = true; void api.realmNames(serverId).then(list => { if (alive) show(list); }).catch(e => { if (alive) setError(asUiError(e).technical); }); return () => { alive = false; }; }, [serverId]); async function rename(id: number, name: string | null) { setBusy(true); setError(null); setSaved(null); try { show(await api.realmRename(serverId, id, name)); setSaved(id); } catch (e) { setError(asUiError(e).technical); } finally { setBusy(false); } } return

{t("realm.namesTitle")}

{t("realm.namesHint")}

{realms?.map(r => { const draft = drafts[r.id] ?? r.name; const invalid = draft.trim().length === 0 || draft.trim().length > MAX_CHARS || draft.includes('"'); return
setDrafts(d => ({ ...d, [r.id]: e.target.value }))} /> {r.custom && }

{t("realm.nameDefault", { name: r.defaultName })}{saved === r.id ? ` ยท ${t("realm.nameSaved")}` : ""}

{draft.includes('"') &&

{t("realm.nameQuotes")}

}
; })} {error &&

{error}

}
; }