mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-22 11:25:56 +00:00
Realmlist switcher next to PLAY, and a Modules page
- Named realmlists (e.g. Solo, PTR): a drop-down by the PLAY button switches the game client by writing the chosen text into every locale's realmlist.wtf (the replaced file is saved first); add / edit / remove with a name and the realmlist text (or just an address). First use offers 'Solo' (127.0.0.1) and, if the client held another realmlist, 'Previous'. - Modules page: bundled catalog (modules/catalog.json) of the optional modules built into the server; switch each on or off (its Enable setting), open its page on GitHub, edit its configuration with the module's own documentation. Nine modules to start with. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a79c26ed49
commit
81aacdf5bb
17 files changed
+1208
-5
No files matched your search
@@ -0,0 +1,157 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { ExternalLink, Loader2 } from "lucide-react";
|
||||
import { api, asUiError, type ModuleSetting, type ModuleView, type UiError } from "@/lib/api";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { useHuman, useI18n } from "@/i18n";
|
||||
|
||||
/** The server's optional modules: switch each on or off, open its page on GitHub, change its settings. */
|
||||
export function ModulesPage({ serverId }: { serverId: string }) {
|
||||
const { t, locale } = useI18n();
|
||||
const human = useHuman();
|
||||
const [modules, setModules] = useState<ModuleView[] | null>(null);
|
||||
const [open, setOpen] = useState<string | null>(null);
|
||||
const [error, setError] = useState<UiError | null>(null);
|
||||
const [changed, setChanged] = useState(false);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
setModules(await api.modulesList(serverId));
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}, [serverId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
async function toggle(m: ModuleView) {
|
||||
setError(null);
|
||||
try {
|
||||
await api.moduleSetEnabled(serverId, m.id, !m.enabled);
|
||||
setChanged(true);
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="max-w-3xl">
|
||||
<h1 className="text-2xl font-semibold">{t("mod.title")}</h1>
|
||||
<p className="mt-1 text-muted">{t("q.modules")}</p>
|
||||
<p className="mt-3 text-sm text-muted">{t("mod.intro")}</p>
|
||||
{changed && <p className="mt-3 rounded-md border border-gold/40 bg-gold/5 px-3 py-2 text-sm" role="status">{t("mod.restart")}</p>}
|
||||
{error && <p className="mt-3 text-sm text-bad" role="alert">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>}
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
{modules === null && <p className="text-muted">{t("overview.checking")}</p>}
|
||||
{modules?.map((m) => (
|
||||
<Card key={m.id} className={cn("p-5", !m.installed && "opacity-60")}>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div className="min-w-0">
|
||||
<h2 className="font-semibold">{m.name}</h2>
|
||||
<p className="mt-1 text-sm text-muted">{m.description[locale] ?? m.description.en}</p>
|
||||
</div>
|
||||
{m.installed ? (
|
||||
<button
|
||||
role="switch"
|
||||
aria-checked={m.enabled}
|
||||
aria-label={t("mod.switch", { name: m.name })}
|
||||
onClick={() => void toggle(m)}
|
||||
className={cn("relative mt-1 h-6 w-11 shrink-0 cursor-pointer rounded-full transition-colors", m.enabled ? "bg-gold" : "bg-white/15")}
|
||||
>
|
||||
<span className={cn("absolute top-0.5 h-5 w-5 rounded-full bg-white transition-all", m.enabled ? "left-[22px]" : "left-0.5")} />
|
||||
</button>
|
||||
) : (
|
||||
<span className="mt-1 shrink-0 text-xs text-muted">{t("mod.notHere")}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="mt-3 flex flex-wrap gap-2">
|
||||
<Button size="sm" variant="ghost" onClick={() => void api.openLink(m.repo)}>
|
||||
<ExternalLink className="h-3.5 w-3.5" aria-hidden /> {t("mod.github")}
|
||||
</Button>
|
||||
{m.installed && (
|
||||
<Button size="sm" variant="ghost" aria-expanded={open === m.id} onClick={() => setOpen(open === m.id ? null : m.id)}>
|
||||
{t("mod.settings")}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
{open === m.id && <ModuleSettings serverId={serverId} module={m} onSaved={() => setChanged(true)} />}
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ModuleSettings({ serverId, module, onSaved }: { serverId: string; module: ModuleView; onSaved: () => void }) {
|
||||
const { t } = useI18n();
|
||||
const human = useHuman();
|
||||
const [items, setItems] = useState<ModuleSetting[] | null>(null);
|
||||
const [edits, setEdits] = useState<Record<string, string>>({});
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<UiError | null>(null);
|
||||
const [note, setNote] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
void api.moduleSettings(serverId, module.id).then((s) => setItems(s)).catch((e) => setError(asUiError(e)));
|
||||
}, [serverId, module.id]);
|
||||
|
||||
async function save() {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setNote(null);
|
||||
try {
|
||||
const done = await api.moduleSaveSettings(serverId, module.id, edits);
|
||||
setEdits({});
|
||||
setItems(await api.moduleSettings(serverId, module.id));
|
||||
if (done.length) {
|
||||
onSaved();
|
||||
setNote(t("mod.saved", { n: done.length }));
|
||||
}
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const dirty = Object.keys(edits).length > 0;
|
||||
return (
|
||||
<div className="mt-4 border-t border-line pt-4">
|
||||
{items === null && !error && <Loader2 className="h-4 w-4 animate-spin text-muted" aria-hidden />}
|
||||
<ul className="divide-y divide-line">
|
||||
{items?.map((s) => (
|
||||
<li key={s.key} className="py-2.5">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<label htmlFor={`${module.id}-${s.key}`} className="selectable text-sm font-medium">{s.key}</label>
|
||||
<input
|
||||
id={`${module.id}-${s.key}`}
|
||||
value={edits[s.key] ?? s.value}
|
||||
onChange={(e) => setEdits((x) => ({ ...x, [s.key]: e.target.value }))}
|
||||
className="selectable w-56 rounded-md border border-line bg-bg px-2.5 py-1.5 text-sm outline-none focus:border-gold"
|
||||
/>
|
||||
</div>
|
||||
{s.doc && <p className="mt-1 text-xs text-muted">{s.doc}</p>}
|
||||
{s.default !== null && (edits[s.key] ?? s.value) !== s.default && (
|
||||
<button className="mt-1 cursor-pointer text-xs text-muted underline hover:text-ink" onClick={() => setEdits((x) => ({ ...x, [s.key]: s.default ?? "" }))}>
|
||||
{t("mod.default", { v: s.default })}
|
||||
</button>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div className="mt-3 flex items-center gap-3">
|
||||
<Button size="sm" variant="primary" disabled={!dirty || busy} onClick={() => void save()}>
|
||||
{busy && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
{t("acc.save")}
|
||||
</Button>
|
||||
{note && <span className="text-sm text-ok" role="status">{note}</span>}
|
||||
</div>
|
||||
{error && <p className="mt-2 text-sm text-bad" role="alert">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { useHuman, useT } from "@/i18n";
|
||||
import { applyServerUpdate, useServerUpdate } from "@/lib/serverUpdate";
|
||||
import { checkClient, useClientStatus } from "@/lib/clientUpdate";
|
||||
import { ClientDialog, type ClientDialogMode } from "@/screens/ClientDialog";
|
||||
import { RealmlistMenu } from "@/screens/RealmlistMenu";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
|
||||
@@ -276,12 +277,13 @@ export function Overview({ server, onForget, onOpenUpdates }: { server: ServerSu
|
||||
{t("btn.stopSmall")}
|
||||
</Button>
|
||||
)}
|
||||
{client && <RealmlistMenu serverId={server.id} className="ml-auto" />}
|
||||
{client && clientStatus?.update_available ? (
|
||||
<Button variant="primary" size="xl" disabled={playing} onClick={() => setDialog("update")} className="ml-auto min-w-40">
|
||||
<Button variant="primary" size="xl" disabled={playing} onClick={() => setDialog("update")} className="min-w-40">
|
||||
{t("btn.updateClient")}
|
||||
</Button>
|
||||
) : client ? (
|
||||
<Button variant={running ? "primary" : "secondary"} size="xl" disabled={transitioning || playing} onClick={() => void play()} className="ml-auto min-w-40">
|
||||
<Button variant={running ? "primary" : "secondary"} size="xl" disabled={transitioning || playing} onClick={() => void play()} className="min-w-40">
|
||||
{playing && <Loader2 className="h-5 w-5 animate-spin" aria-hidden />}
|
||||
{running ? t("btn.play") : t("btn.startPlay")}
|
||||
</Button>
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { Check, ChevronDown, Pencil, Plus, Trash2 } from "lucide-react";
|
||||
import { api, asUiError, type RealmProfile, type UiError } from "@/lib/api";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { useHuman, useT } from "@/i18n";
|
||||
|
||||
/** The first `set realmlist` value of a realmlist text, for the small caption under a name. */
|
||||
function host(data: string): string {
|
||||
const m = data.match(/^\s*set\s+realmlist\s+(\S+)/im);
|
||||
return m ? m[1].replace(/"/g, "") : "";
|
||||
}
|
||||
|
||||
/** Named realmlists for the game client, switched with one click: "Solo" for your own server, "PTR" for the shared one, and so on. */
|
||||
export function RealmlistMenu({ serverId, className }: { serverId: string; className?: string }) {
|
||||
const t = useT();
|
||||
const human = useHuman();
|
||||
const [profiles, setProfiles] = useState<RealmProfile[]>([]);
|
||||
const [active, setActive] = useState<string | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [editing, setEditing] = useState<{ id: string | null; name: string; data: string } | null>(null);
|
||||
const [error, setError] = useState<UiError | null>(null);
|
||||
const [note, setNote] = useState<string | null>(null);
|
||||
const box = useRef<HTMLDivElement>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
const v = await api.realmlistProfiles(serverId);
|
||||
setProfiles(v.profiles);
|
||||
setActive(v.active);
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}, [serverId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const away = (e: MouseEvent) => {
|
||||
if (box.current && !box.current.contains(e.target as Node)) setOpen(false);
|
||||
};
|
||||
document.addEventListener("mousedown", away);
|
||||
return () => document.removeEventListener("mousedown", away);
|
||||
}, [open]);
|
||||
|
||||
async function choose(p: RealmProfile) {
|
||||
setError(null);
|
||||
setNote(null);
|
||||
try {
|
||||
await api.realmlistActivate(serverId, p.id);
|
||||
setActive(p.id);
|
||||
setNote(t("realm.switched", { name: p.name }));
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(p: RealmProfile) {
|
||||
if (!window.confirm(t("realm.confirmDelete", { name: p.name }))) return;
|
||||
setError(null);
|
||||
try {
|
||||
await api.realmlistDelete(serverId, p.id);
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
if (!editing) return;
|
||||
setError(null);
|
||||
try {
|
||||
await api.realmlistSave(serverId, editing.id, editing.name, editing.data);
|
||||
setEditing(null);
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}
|
||||
|
||||
const current = profiles.find((p) => p.id === active);
|
||||
const message = error ? (error.human.code === "unknown" ? error.technical : human(error.human).message) : null;
|
||||
|
||||
return (
|
||||
<div ref={box} className={cn("relative", className)}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={open}
|
||||
className="flex h-16 cursor-pointer items-center gap-3 rounded-md border border-line bg-card-2 px-4 text-left transition-colors hover:border-gold/50"
|
||||
>
|
||||
<span>
|
||||
<span className="block text-[11px] uppercase tracking-wide text-muted">{t("realm.label")}</span>
|
||||
<span className="block max-w-40 truncate text-[15px] font-medium">{current ? current.name : t("realm.other")}</span>
|
||||
</span>
|
||||
<ChevronDown className="h-4 w-4 text-muted" aria-hidden />
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div role="menu" className="absolute right-0 top-full z-30 mt-2 w-80 rounded-md border border-line bg-card shadow-2xl">
|
||||
<ul className="max-h-72 overflow-y-auto py-1">
|
||||
{profiles.map((p) => (
|
||||
<li key={p.id} className="group flex items-center">
|
||||
<button role="menuitemradio" aria-checked={p.id === active} onClick={() => void choose(p)} className="flex min-w-0 flex-1 cursor-pointer items-center gap-3 px-3 py-2 text-left hover:bg-white/5">
|
||||
<span className="flex h-4 w-4 shrink-0 items-center justify-center text-gold">{p.id === active && <Check className="h-4 w-4" aria-hidden />}</span>
|
||||
<span className="min-w-0">
|
||||
<span className="block truncate text-sm">{p.name}</span>
|
||||
<span className="selectable block truncate text-xs text-muted">{host(p.data)}</span>
|
||||
</span>
|
||||
</button>
|
||||
<button aria-label={t("realm.edit", { name: p.name })} title={t("realm.edit", { name: p.name })} onClick={() => setEditing({ id: p.id, name: p.name, data: p.data.trim() })} className="cursor-pointer p-2 text-muted hover:text-ink">
|
||||
<Pencil className="h-3.5 w-3.5" aria-hidden />
|
||||
</button>
|
||||
<button aria-label={t("realm.delete", { name: p.name })} title={t("realm.delete", { name: p.name })} disabled={profiles.length <= 1} onClick={() => void remove(p)} className="cursor-pointer p-2 text-muted hover:text-bad disabled:cursor-default disabled:opacity-30">
|
||||
<Trash2 className="h-3.5 w-3.5" aria-hidden />
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div className="border-t border-line p-1">
|
||||
<button role="menuitem" onClick={() => { setEditing({ id: null, name: "", data: "" }); setError(null); }} className="flex w-full cursor-pointer items-center gap-2 rounded px-3 py-2 text-sm hover:bg-white/5">
|
||||
<Plus className="h-4 w-4 text-gold" aria-hidden /> {t("realm.add")}
|
||||
</button>
|
||||
</div>
|
||||
{note && <p className="border-t border-line px-3 py-2 text-xs text-ok" role="status">{note}</p>}
|
||||
{message && !editing && <p className="border-t border-line px-3 py-2 text-xs text-bad" role="alert">{message}</p>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{editing && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-6" role="presentation">
|
||||
<Card className="w-full max-w-md p-6 shadow-2xl" role="dialog" aria-modal="true" aria-label={editing.id ? t("realm.editTitle") : t("realm.addTitle")}>
|
||||
<h2 className="text-lg font-semibold">{editing.id ? t("realm.editTitle") : t("realm.addTitle")}</h2>
|
||||
<label htmlFor="realm-name" className="mt-4 block text-sm text-muted">{t("realm.name")}</label>
|
||||
<input id="realm-name" autoFocus maxLength={32} value={editing.name} onChange={(e) => setEditing({ ...editing, name: e.target.value })} placeholder={t("realm.nameHint")} className="mt-1 block w-full rounded-md border border-line bg-bg px-3 py-2.5 text-[15px] outline-none focus:border-gold" />
|
||||
<label htmlFor="realm-data" className="mt-4 block text-sm text-muted">{t("realm.data")}</label>
|
||||
<textarea id="realm-data" rows={3} value={editing.data} onChange={(e) => setEditing({ ...editing, data: e.target.value })} placeholder="set realmlist play.example.com" className="selectable mt-1 block w-full rounded-md border border-line bg-bg px-3 py-2.5 font-mono text-sm outline-none focus:border-gold" />
|
||||
<p className="mt-1 text-xs text-muted">{t("realm.dataHint")}</p>
|
||||
{message && <p className="mt-3 text-sm text-bad" role="alert">{message}</p>}
|
||||
<div className="mt-5 flex justify-end gap-2">
|
||||
<Button variant="ghost" onClick={() => { setEditing(null); setError(null); }}>{t("common.cancel")}</Button>
|
||||
<Button variant="primary" disabled={!editing.name.trim() || !editing.data.trim()} onClick={() => void save()}>{t("acc.save")}</Button>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Archive, Bot, Bug, Gauge, Globe, Server, Settings, Terminal, Users, type LucideIcon } from "lucide-react";
|
||||
import { Archive, Bot, Bug, Gauge, Globe, Puzzle, Server, Settings, Terminal, Users, type LucideIcon } from "lucide-react";
|
||||
import { type ServerSummary } from "@/lib/api";
|
||||
import { useT, type Key } from "@/i18n";
|
||||
import logo from "@/assets/logo.png";
|
||||
@@ -12,17 +12,19 @@ import { PlayersPage } from "@/screens/PlayersPage";
|
||||
import { SettingsHome } from "@/screens/SettingsHome";
|
||||
import { FriendsPage } from "@/screens/FriendsPage";
|
||||
import { ReportPage } from "@/screens/ReportPage";
|
||||
import { ModulesPage } from "@/screens/ModulesPage";
|
||||
import { ConsolePage } from "@/screens/ConsolePage";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { startServerUpdatePolling, useServerUpdate } from "@/lib/serverUpdate";
|
||||
import { startClientPolling } from "@/lib/clientUpdate";
|
||||
|
||||
type Page = "overview" | "bots" | "server" | "players" | "friends" | "backups" | "console" | "report" | "settings";
|
||||
type Page = "overview" | "bots" | "server" | "modules" | "players" | "friends" | "backups" | "console" | "report" | "settings";
|
||||
|
||||
const NAV: { id: Page; label: Key; icon: LucideIcon; question: Key }[] = [
|
||||
{ id: "overview", label: "nav.overview", icon: Gauge, question: "q.overview" },
|
||||
{ id: "bots", label: "nav.bots", icon: Bot, question: "q.bots" },
|
||||
{ id: "server", label: "nav.server", icon: Server, question: "q.server" },
|
||||
{ id: "modules", label: "nav.modules", icon: Puzzle, question: "q.modules" },
|
||||
{ id: "players", label: "nav.players", icon: Users, question: "q.players" },
|
||||
{ id: "friends", label: "nav.friends", icon: Globe, question: "q.friends" },
|
||||
{ id: "backups", label: "nav.backups", icon: Archive, question: "q.backups" },
|
||||
@@ -110,6 +112,8 @@ export function Shell(props: {
|
||||
<SettingsPage key={`${server.id}-${page}`} serverId={server.id} scope={page} title={t(current.label)} question={t(current.question)} />
|
||||
) : page === "console" ? (
|
||||
<ConsolePage key={server.id} serverId={server.id} />
|
||||
) : page === "modules" ? (
|
||||
<ModulesPage key={server.id} serverId={server.id} />
|
||||
) : page === "report" ? (
|
||||
<ReportPage key={server.id} serverId={server.id} />
|
||||
) : page === "friends" ? (
|
||||
|
||||
Reference in new issue
Block a user