import { useEffect, useState } from "react"; import { Layers, Loader2 } from "lucide-react"; import { api, asUiError, type RealmMode, type RealmProfiles } from "@/lib/api"; import { useT } from "@/i18n"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; export function RealmPicker({ serverId, running, disabled, onChanged, onBusy }: { serverId: string; running: boolean; disabled: boolean; onChanged: () => void; onBusy: (busy: boolean) => void; }) { const t = useT(); const [view, setView] = useState(null); const [selected, setSelected] = useState("coa"); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // More than one world starts together: the main realm cannot be switched then. const together = !!view && (view.simultaneous || view.with_reborn); useEffect(() => { let alive = true; void api.realmProfiles(serverId).then(v => { if (alive) { setView(v); setSelected(v.active); } }).catch(e => { if (alive) setError(asUiError(e).technical); }); return () => { alive = false; }; }, [serverId]); async function select() { setBusy(true); onBusy(true); setError(null); try { const next = await api.realmSelect(serverId, selected, running); setView(next); onChanged(); } catch (e) { setError(asUiError(e).technical); // Selection may have committed even if the subsequent server restart failed. try { setView(await api.realmProfiles(serverId)); onChanged(); } catch { /* keep the operation error */ } } finally { setBusy(false); onBusy(false); } } return
{together ? {[view.active, ...(view.simultaneous ? [view.active === "coa" ? "wildcard" : "coa"] : []), ...(view.with_reborn ? ["reborn"] : [])].map(m => m === "coa" ? "Conquest of Azeroth" : m === "reborn" ? "Warcraft Reborn" : "Wildcard").join(" + ")} : } {view && !together && (selected !== view.active || view.recovery_pending) && } {view && !together && {t("realm.active", { name: view.active === "coa" ? "CoA" : view.active === "reborn" ? "Warcraft Reborn" : "Wildcard" })}}

{t(together ? "realm.bothRunning" : "realm.separate")}

{selected === "reborn" &&

{t("realm.rebornModules")}

} {view && selected === "reborn" && !view.reborn_supported &&

{t("realm.rebornUpdate")}

} {selected === "wildcard" &&

{t("realm.modules")}

} {view && selected === "wildcard" && !view.supported &&

{t("realm.update")}

} {view?.recovery_pending &&

{t("realm.recovery")}

} {error &&

{error}

}
; }