import { useEffect, useState } from "react"; import { Check, Copy, Eye, EyeOff, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { useI18n, type Key } from "@/i18n"; import { asControlError, control, type AccountView, type Credentials, type JoinOutcome, type RemoteCharacter } from "@/lib/control"; import { REMOTE_CLIENT_ID } from "@/lib/api"; import { ClientDialog, type ClientDialogMode } from "@/screens/ClientDialog"; import { Notes } from "@/screens/PortablePage"; const STATUS_TEXT: Record = { launched: "join.status.launched", ready: "join.status.ready", needs_relay: "join.status.needsRelay", needs_client: "join.status.needsClient", needs_transfer: "join.status.needsTransfer", incompatible: "join.status.incompatible", needs_link: "join.status.needsLink", }; function ErrorLine({ code, message }: { code: string; message: string }) { const { t } = useI18n(); const key = `join.err.${code}` as Key; const known = t(key) !== key; return

{known ? t(key) : message}

; } /** The player's account on one server: shown as a name; the password appears only when asked for and goes to the clipboard, never to the screen by default. */ function AccountBox({ realm, account, onForget }: { realm: string; account: AccountView; onForget: () => void }) { const { t } = useI18n(); const [creds, setCreds] = useState(null); const [shown, setShown] = useState(false); const [copied, setCopied] = useState<"name" | "password" | null>(null); useEffect(() => { setCreds(null); setShown(false); }, [realm, account.username]); async function load(): Promise { if (creds) return creds; const c = await control.credentials(realm); setCreds(c); return c; } async function copy(what: "name" | "password") { const c = await load(); if (!c) return; try { await navigator.clipboard.writeText(what === "name" ? c.username : c.password); setCopied(what); setTimeout(() => setCopied(null), 4000); } catch { /* the clipboard is not available */ } } return

{t("join.account")}

{account.username}

{shown && creds &&

{creds.password}

}

{t("join.accountHint")}

; } function LinkForm({ realm, onLinked }: { realm: string; onLinked: () => void }) { const { t } = useI18n(); const [login, setLogin] = useState(""); const [password, setPassword] = useState(""); const [busy, setBusy] = useState(false); const [err, setErr] = useState<{ code: string; message: string } | null>(null); async function submit() { setBusy(true); setErr(null); try { await control.link(realm, login.trim(), password); setPassword(""); onLinked(); } catch (e) { setErr(asControlError(e)); } finally { setBusy(false); } } return
{ e.preventDefault(); void submit(); }}>

{t("join.link.title")}

{t("join.link.text")}

setLogin(e.target.value)} className="mt-1 w-full rounded-md border border-line bg-card px-3 py-2" /> setPassword(e.target.value)} className="mt-1 w-full rounded-md border border-line bg-card px-3 py-2" /> {err && } ; } const CLASS_NAMES: Record = { 1: "Warrior", 2: "Paladin", 3: "Hunter", 4: "Rogue", 5: "Priest", 6: "Death Knight", 7: "Shaman", 8: "Mage", 9: "Warlock", 11: "Druid", 12: "Barbarian", 13: "Witch Doctor", 14: "Felsworn", 15: "Witch Hunter", 16: "Stormbringer", 17: "Knight of Xoroth", 18: "Guardian", 19: "Templar", 20: "Blood Mage", 21: "Ranger", 22: "Chronomancer", 23: "Necromancer", 24: "Pyromancer", 25: "Cultist", 26: "Starcaller", 27: "Sun Cleric", 28: "Tinker", 29: "Venomancer", 30: "Reaper", 31: "Primalist", 32: "Runemaster", }; function ServerCharacters({ realm, onClaimed }: { realm: string; onClaimed: () => void }) { const { t } = useI18n(); const [list, setList] = useState(null); const [busy, setBusy] = useState(null); const [err, setErr] = useState<{ code: string; message: string } | null>(null); const [done, setDone] = useState(null); async function load() { setBusy("*"); setErr(null); try { setList(await control.characters(realm)); } catch (e) { setErr(asControlError(e)); setList(null); } finally { setBusy(null); } } async function claim(name: string) { setBusy(name); setErr(null); setDone(null); try { const c = await control.claim(realm, name); setDone(t("join.chars.done", { name: c.name, revision: c.revision })); onClaimed(); await load(); } catch (e) { setErr(asControlError(e)); } finally { setBusy(null); } } return

{t("join.chars.title")}

{t("portable.experimental")}

{t("join.chars.text")}

{list && list.length === 0 &&

{t("join.chars.none")}

} {list && list.length > 0 &&
    {list.map((c) =>
  • {c.name} — {CLASS_NAMES[c.class] ?? `#${c.class}`}, {t("portable.level", { level: c.level })} {!c.eligible && {t("join.chars.notEligible")} ({c.reasons.join(", ")})} {c.yours ? {t("join.chars.portable")} : }
  • )}
} {done &&

{done}

} {err && }
; } /** Joining one server: the account, the game, the character-claim and the honest "not yet" cases. */ export function JoinPanel({ realm, character, automatic, onClaimed }: { realm: string; character: string; automatic: boolean; onClaimed: () => void }) { const { t } = useI18n(); const [account, setAccount] = useState(null); const [busy, setBusy] = useState(false); const [outcome, setOutcome] = useState(null); const [err, setErr] = useState<{ code: string; message: string } | null>(null); const [linking, setLinking] = useState(false); const [bring, setBring] = useState(false); const [clientDialog, setClientDialog] = useState(null); const refresh = () => control.account(realm).then(setAccount).catch(() => setAccount(null)); useEffect(() => { setOutcome(null); setErr(null); setLinking(false); void refresh(); }, [realm]); // eslint-disable-line react-hooks/exhaustive-deps async function join() { setBusy(true); setErr(null); setOutcome(null); try { const o = await control.join(realm, bring && character ? character : null, true); setOutcome(o); if (o.status === "needs_link") setLinking(true); if (o.status === "needs_client") setClientDialog("setup"); await refresh(); } catch (e) { setErr(asControlError(e)); } finally { setBusy(false); } } const text = outcome ? STATUS_TEXT[outcome.status] : undefined; const bad = outcome && ["incompatible", "needs_relay", "needs_client", "needs_transfer", "needs_link"].includes(outcome.status); return
{account && void control.forget(realm).then(() => { setAccount(null); setOutcome(null); })} />} {(linking || (!account && !automatic)) && { setLinking(false); void refresh(); setOutcome(null); }} />}
{character && } {!account && automatic && !linking && } {outcome && text &&

{t(text)}

{outcome.status === "needs_client" && ( )} {outcome.account_created &&

{t("join.created", { name: outcome.username ?? "" })}

} {outcome.password_reset &&

{t("join.reset")}

} {outcome.notes.length > 0 && }
} {err && }
{clientDialog && ( setClientDialog(null)} onChanged={() => { setClientDialog(null); void join(); }} /> )}
; }