mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-21 19:25:56 +00:00
feat(ui): one search, tabs and save bar on the Server page; no module legend
The collection switches and every other documented setting become tabs of the same panel, are searched together with the curated settings and are saved by the same bar. The Modules page drops the status legend; Playerbots is named SQUID's Playerbots. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1f54fbf909
commit
89be57b4e4
12 files changed
+324
-316
No files matched your search
@@ -1,149 +0,0 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { ChevronDown, ChevronRight, Loader2, Search } from "lucide-react";
|
||||
import { api, asUiError, type AllSetting, type UiError } from "@/lib/api";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { useHuman, useT } from "@/i18n";
|
||||
|
||||
/** How many matches are drawn at once; the rest is reached by searching. */
|
||||
const SHOWN = 40;
|
||||
|
||||
/** Every documented setting of the world server, searchable. For people who want more than the curated list above. */
|
||||
export function AllSettingsCard({ serverId }: { serverId: string }) {
|
||||
const t = useT();
|
||||
const human = useHuman();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [items, setItems] = useState<AllSetting[] | null>(null);
|
||||
const [query, setQuery] = useState("");
|
||||
const [onlyChanged, setOnlyChanged] = useState(false);
|
||||
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(() => {
|
||||
if (!open || items) return;
|
||||
void api.allSettings(serverId).then(setItems).catch((e) => setError(asUiError(e)));
|
||||
}, [open, items, serverId]);
|
||||
|
||||
const matches = useMemo(() => {
|
||||
const q = query.trim().toLowerCase();
|
||||
return (items ?? []).filter((s) => (!onlyChanged || s.changed) && (!q || s.key.toLowerCase().includes(q) || s.doc.toLowerCase().includes(q)));
|
||||
}, [items, query, onlyChanged]);
|
||||
|
||||
const dirty = Object.keys(edits);
|
||||
|
||||
async function save() {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setNote(null);
|
||||
try {
|
||||
const done = await api.allSettingsSave(serverId, edits);
|
||||
setEdits({});
|
||||
setItems(await api.allSettings(serverId));
|
||||
setNote(done.length ? t("all.saved") : t("set.nothingChanged"));
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mt-8 p-5">
|
||||
<button onClick={() => setOpen((v) => !v)} aria-expanded={open} className="flex w-full cursor-pointer items-center gap-2 text-left">
|
||||
{open ? <ChevronDown className="h-4 w-4" aria-hidden /> : <ChevronRight className="h-4 w-4" aria-hidden />}
|
||||
<span className="font-semibold">{t("all.title")}</span>
|
||||
</button>
|
||||
<p className="mt-2 text-sm text-muted">{t("all.intro")}</p>
|
||||
|
||||
{open && (
|
||||
<div className="mt-4">
|
||||
{items === null && !error && <Loader2 className="h-4 w-4 animate-spin text-muted" aria-hidden />}
|
||||
{items && (
|
||||
<>
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<label className="relative block min-w-[16rem] flex-1">
|
||||
<Search className="pointer-events-none absolute left-2.5 top-2.5 h-4 w-4 text-muted" aria-hidden />
|
||||
<input
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder={t("all.search")}
|
||||
aria-label={t("all.search")}
|
||||
className="selectable w-full rounded-md border border-line bg-bg py-2 pl-8 pr-3 text-sm outline-none focus:border-gold"
|
||||
/>
|
||||
</label>
|
||||
<label className="flex cursor-pointer items-center gap-2 text-sm">
|
||||
<input type="checkbox" checked={onlyChanged} onChange={(e) => setOnlyChanged(e.target.checked)} />
|
||||
{t("all.onlyChanged")}
|
||||
</label>
|
||||
</div>
|
||||
<p className="mt-2 text-xs text-muted">{t("all.count", { shown: Math.min(matches.length, SHOWN), total: matches.length })}</p>
|
||||
|
||||
{matches.length === 0 && <p className="mt-4 text-sm text-muted">{t("all.none")}</p>}
|
||||
<ul className="mt-2 divide-y divide-line">
|
||||
{matches.slice(0, SHOWN).map((s) => {
|
||||
const value = edits[s.key] ?? s.value;
|
||||
return (
|
||||
<li key={s.key} className="py-3">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<label htmlFor={`all-${s.key}`} className="selectable text-sm font-medium">
|
||||
{s.key}
|
||||
{s.changed && <span className="ml-2 rounded bg-gold/15 px-1.5 py-0.5 text-xs text-gold">{t("all.changed")}</span>}
|
||||
</label>
|
||||
<input
|
||||
id={`all-${s.key}`}
|
||||
value={value}
|
||||
onChange={(e) => {
|
||||
const v = e.target.value;
|
||||
setEdits((x) => {
|
||||
const n = { ...x };
|
||||
if (v === s.value) delete n[s.key];
|
||||
else n[s.key] = v;
|
||||
return n;
|
||||
});
|
||||
}}
|
||||
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>
|
||||
<p className="selectable mt-1 text-xs text-muted">{s.doc}</p>
|
||||
{value !== s.default && (
|
||||
<button
|
||||
className="mt-1 cursor-pointer text-xs text-muted underline hover:text-ink"
|
||||
onClick={() =>
|
||||
setEdits((x) => {
|
||||
const n = { ...x };
|
||||
if (s.default === s.value) delete n[s.key];
|
||||
else n[s.key] = s.default;
|
||||
return n;
|
||||
})
|
||||
}
|
||||
>
|
||||
{t("all.useDefault", { v: s.default })}
|
||||
</button>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
|
||||
<div className="mt-4 flex items-center gap-3">
|
||||
<Button size="sm" variant="primary" disabled={dirty.length === 0 || busy} onClick={() => void save()}>
|
||||
{busy && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
|
||||
{t("all.save", { n: dirty.length })}
|
||||
</Button>
|
||||
{dirty.length > 0 && (
|
||||
<Button size="sm" variant="ghost" onClick={() => setEdits({})}>
|
||||
{t("set.discard")}
|
||||
</Button>
|
||||
)}
|
||||
{note && <span className="text-sm text-ok" role="status">{note}</span>}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{error && <p className="mt-3 text-sm text-bad" role="alert">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { api, asUiError, type ModuleSetting, type UiError } from "@/lib/api";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { useHuman, useT, type Key } from "@/i18n";
|
||||
|
||||
/** The collection and convenience switches of the client compatibility part (the same file holds them under either name). */
|
||||
const SWITCHES: { name: string; label: Key; text: Key }[] = [
|
||||
{ name: "UnlockLocalAppearanceCatalog", label: "col.appearances", text: "col.appearances.text" },
|
||||
{ name: "UnlockAllVanity", label: "col.vanity", text: "col.vanity.text" },
|
||||
{ name: "AutoCollectAppearances", label: "col.auto", text: "col.auto.text" },
|
||||
{ name: "LearnOwnedCompanions", label: "col.companions", text: "col.companions.text" },
|
||||
{ name: "MaxRidingFromStart", label: "col.riding", text: "col.riding.text" },
|
||||
];
|
||||
|
||||
/** Appearances (transmog), vanity and a few solo conveniences: plain switches for what lives in the compatibility file. */
|
||||
export function CollectionsCard({ serverId }: { serverId: string }) {
|
||||
const t = useT();
|
||||
const human = useHuman();
|
||||
const [items, setItems] = useState<ModuleSetting[] | null>(null);
|
||||
const [error, setError] = useState<UiError | null>(null);
|
||||
const [changed, setChanged] = useState(false);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
setItems(await api.moduleSettings(serverId, "client-compat"));
|
||||
} catch {
|
||||
setItems([]); // an older build without the file: nothing to offer
|
||||
}
|
||||
}, [serverId]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
const find = (name: string) => items?.find((i) => i.key.endsWith("." + name));
|
||||
const shown = SWITCHES.filter((s) => find(s.name));
|
||||
if (!items || shown.length === 0) return null;
|
||||
|
||||
async function toggle(name: string) {
|
||||
const item = find(name);
|
||||
if (!item) return;
|
||||
setError(null);
|
||||
try {
|
||||
await api.moduleSaveSettings(serverId, "client-compat", { [item.key]: item.value.trim() === "1" ? "0" : "1" });
|
||||
setChanged(true);
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError(asUiError(e));
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="mt-8 p-5">
|
||||
<h2 className="font-semibold">{t("col.title")}</h2>
|
||||
<p className="mt-1 text-sm text-muted">{t("col.intro")}</p>
|
||||
<ul className="mt-3 divide-y divide-line">
|
||||
{shown.map((s) => {
|
||||
const on = find(s.name)?.value.trim() === "1";
|
||||
return (
|
||||
<li key={s.name} className="flex items-center justify-between gap-4 py-3">
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-medium">{t(s.label)}</p>
|
||||
<p className="mt-0.5 text-xs text-muted">{t(s.text)}</p>
|
||||
</div>
|
||||
<button
|
||||
role="switch"
|
||||
aria-checked={on}
|
||||
aria-label={t(s.label)}
|
||||
onClick={() => void toggle(s.name)}
|
||||
className={cn("relative h-6 w-11 shrink-0 cursor-pointer rounded-full transition-colors", on ? "bg-gold" : "bg-white/15")}
|
||||
>
|
||||
<span className={cn("absolute top-0.5 h-5 w-5 rounded-full bg-white transition-all", on ? "left-[22px]" : "left-0.5")} />
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
{changed && <p className="mt-3 text-sm text-muted" role="status">{t("mod.restart")}</p>}
|
||||
{error && <p className="mt-2 text-sm text-bad" role="alert">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import { RotateCcw } from "lucide-react";
|
||||
import type { AllSetting, ModuleSetting } from "@/lib/api";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useT, type Key } from "@/i18n";
|
||||
|
||||
/** The collection and convenience switches of the client compatibility part (the same file holds them under either name). */
|
||||
const SWITCHES: { name: string; label: Key; text: Key }[] = [
|
||||
{ name: "UnlockLocalAppearanceCatalog", label: "col.appearances", text: "col.appearances.text" },
|
||||
{ name: "UnlockAllVanity", label: "col.vanity", text: "col.vanity.text" },
|
||||
{ name: "AutoCollectAppearances", label: "col.auto", text: "col.auto.text" },
|
||||
{ name: "LearnOwnedCompanions", label: "col.companions", text: "col.companions.text" },
|
||||
{ name: "MaxRidingFromStart", label: "col.riding", text: "col.riding.text" },
|
||||
];
|
||||
|
||||
export interface Collection {
|
||||
item: ModuleSetting;
|
||||
label: Key;
|
||||
text: Key;
|
||||
}
|
||||
|
||||
/** The switches the compatibility file actually has; an older build without the file offers none. */
|
||||
export function collectionsOf(items: ModuleSetting[]): Collection[] {
|
||||
return SWITCHES.flatMap((s) => {
|
||||
const item = items.find((i) => i.key.endsWith("." + s.name));
|
||||
return item ? [{ item, label: s.label, text: s.text }] : [];
|
||||
});
|
||||
}
|
||||
|
||||
const ROW = "grid grid-cols-[1fr_auto] items-start gap-x-6 gap-y-1 py-3.5";
|
||||
|
||||
/** Appearances (transmog), vanity and a few solo conveniences, in the look of every other setting row. */
|
||||
export function CollectionRows(props: { items: Collection[]; edits: Record<string, string>; onChange: (key: string, value: string | null) => void }) {
|
||||
const t = useT();
|
||||
return (
|
||||
<div className="divide-y divide-line">
|
||||
{props.items.map(({ item, label, text }) => {
|
||||
const value = props.edits[item.key] ?? item.value;
|
||||
const on = value.trim() === "1";
|
||||
return (
|
||||
<div key={item.key} className={ROW}>
|
||||
<div>
|
||||
<label htmlFor={`col-${item.key}`} className="flex flex-wrap items-center gap-2 font-medium">
|
||||
{t(label)}
|
||||
{item.key in props.edits && <span className="rounded bg-gold/15 px-1.5 py-0.5 text-[11px] font-normal text-gold">{t("set.changed")}</span>}
|
||||
</label>
|
||||
<p className="mt-0.5 max-w-xl text-sm text-muted">{t(text)}</p>
|
||||
</div>
|
||||
<div className="pt-0.5">
|
||||
<button
|
||||
id={`col-${item.key}`}
|
||||
role="switch"
|
||||
aria-checked={on}
|
||||
onClick={() => {
|
||||
const next = on ? "0" : "1";
|
||||
props.onChange(item.key, next === item.value.trim() ? null : next);
|
||||
}}
|
||||
className={cn("relative h-7 w-14 cursor-pointer rounded-full transition-colors", on ? "bg-gold" : "bg-white/15")}
|
||||
>
|
||||
<span className={cn("absolute left-1 top-1 h-5 w-5 rounded-full bg-white transition-transform", on && "translate-x-7")} />
|
||||
<span className="sr-only">{on ? t("set.on") : t("set.off")}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Documented world server settings that have no friendlier control: the name, its explanation, a text box. */
|
||||
export function RawRows(props: { items: AllSetting[]; edits: Record<string, string>; onChange: (key: string, value: string | null) => void }) {
|
||||
const t = useT();
|
||||
return (
|
||||
<div className="divide-y divide-line">
|
||||
{props.items.map((s) => {
|
||||
const value = props.edits[s.key] ?? s.value;
|
||||
const set = (v: string) => props.onChange(s.key, v === s.value ? null : v);
|
||||
return (
|
||||
<div key={s.key} className={ROW}>
|
||||
<div className="min-w-0">
|
||||
<label htmlFor={`all-${s.key}`} className="selectable flex flex-wrap items-center gap-2 font-medium">
|
||||
{s.key}
|
||||
{(s.changed || s.key in props.edits) && <span className="rounded bg-gold/15 px-1.5 py-0.5 text-[11px] font-normal text-gold">{t("set.changed")}</span>}
|
||||
</label>
|
||||
<p className="selectable mt-0.5 max-w-xl text-sm text-muted">{s.doc}</p>
|
||||
<p className="mt-1 text-xs text-muted/80">{t("set.default", { v: s.default })}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 pt-0.5">
|
||||
<input
|
||||
id={`all-${s.key}`}
|
||||
value={value}
|
||||
onChange={(e) => set(e.target.value)}
|
||||
className="selectable w-56 rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none focus:border-gold"
|
||||
/>
|
||||
<button
|
||||
title={t("set.reset")}
|
||||
aria-label={t("set.resetItem", { name: s.key })}
|
||||
onClick={() => set(s.default)}
|
||||
disabled={value === s.default}
|
||||
className="cursor-pointer rounded p-1.5 text-muted hover:bg-white/5 hover:text-ink disabled:opacity-25"
|
||||
>
|
||||
<RotateCcw className="h-4 w-4" aria-hidden />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -104,15 +104,6 @@ export function ModulesPage({ serverId, onChanged }: { serverId: string; onChang
|
||||
<p className="mt-1 text-muted">{t("q.modules")}</p>
|
||||
<p className="mt-3 text-sm text-muted">{t("mod.intro")}</p>
|
||||
|
||||
<div className="mt-3 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-muted" aria-label={t("mod.legend")}>
|
||||
{(["soon", "early", "beta", "release"] as const).map((s) => (
|
||||
<span key={s} className="flex items-center gap-2">
|
||||
<StatusBadge status={s} />
|
||||
{t(STATUS[s].hint)}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{changed && <p className="mt-4 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>}
|
||||
|
||||
|
||||
+181
-71
@@ -1,10 +1,12 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { AlertTriangle, ChevronDown, ChevronRight, RotateCcw } from "lucide-react";
|
||||
import { AlertTriangle, ChevronDown, ChevronRight, RotateCcw, Search } from "lucide-react";
|
||||
import {
|
||||
api,
|
||||
asUiError,
|
||||
type AllSetting,
|
||||
type FieldError,
|
||||
type JsonValue,
|
||||
type ModuleSetting,
|
||||
type PresetInfo,
|
||||
type PresetPreview,
|
||||
type SaveReport,
|
||||
@@ -16,8 +18,7 @@ import {
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { AllSettingsCard } from "@/screens/AllSettingsCard";
|
||||
import { CollectionsCard } from "@/screens/CollectionsCard";
|
||||
import { CollectionRows, RawRows, collectionsOf } from "@/screens/ExtraRows";
|
||||
import { CompanionsCard } from "@/screens/CompanionsCard";
|
||||
import { useHuman, useI18n, useSchemaText, useT, type Key } from "@/i18n";
|
||||
|
||||
@@ -164,6 +165,14 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
const [showAdvanced, setShowAdvanced] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [running, setRunning] = useState(false);
|
||||
// Beside the curated settings the server page offers the collection switches and every other documented setting. They are
|
||||
// searched together and saved with the rest, so the page has one search box, one set of tabs and one save bar.
|
||||
const [query, setQuery] = useState("");
|
||||
const [raw, setRaw] = useState<AllSetting[] | null>(null);
|
||||
const [rawEdits, setRawEdits] = useState<Record<string, string>>({});
|
||||
const [onlyChanged, setOnlyChanged] = useState(false);
|
||||
const [cols, setCols] = useState<ModuleSetting[]>([]);
|
||||
const [colEdits, setColEdits] = useState<Record<string, string>>({});
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
@@ -181,13 +190,40 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
useEffect(() => {
|
||||
setView(null);
|
||||
setSaved(null);
|
||||
setCategory("");
|
||||
void load();
|
||||
void api.presets(scope).then(setPresets);
|
||||
void api.status(serverId).then((s) => setRunning(s.observed.world.state === "running"));
|
||||
setQuery("");
|
||||
setRaw(null);
|
||||
setRawEdits({});
|
||||
setColEdits({});
|
||||
setCols([]);
|
||||
if (scope === "server") void api.moduleSettings(serverId, "client-compat").then(setCols).catch(() => setCols([]));
|
||||
}, [load, scope, serverId]);
|
||||
|
||||
const needRaw = scope === "server" && (category === "@all" || query.trim() !== "");
|
||||
useEffect(() => {
|
||||
if (needRaw && !raw) void api.allSettings(serverId).then(setRaw).catch(() => setRaw([]));
|
||||
}, [needRaw, raw, serverId]);
|
||||
|
||||
const valueOf = (s: SettingView): JsonValue => (s.key in draft ? draft[s.key] : s.value);
|
||||
const dirtyKeys = useMemo(() => Object.keys(draft).filter((k) => view && draft[k] !== view.settings.find((s) => s.key === k)?.value), [draft, view]);
|
||||
const editRaw = (key: string, value: string | null) =>
|
||||
setRawEdits((e) => {
|
||||
const n = { ...e };
|
||||
if (value === null) delete n[key];
|
||||
else n[key] = value;
|
||||
return n;
|
||||
});
|
||||
const editCol = (key: string, value: string | null) =>
|
||||
setColEdits((e) => {
|
||||
const n = { ...e };
|
||||
if (value === null) delete n[key];
|
||||
else n[key] = value;
|
||||
return n;
|
||||
});
|
||||
const pending = dirtyKeys.length + Object.keys(rawEdits).length + Object.keys(colEdits).length;
|
||||
|
||||
if (fatal) {
|
||||
return (
|
||||
@@ -208,7 +244,34 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
const advancedCount = (id: string) => view.settings.filter((s) => s.category === id && s.advanced).length;
|
||||
const basicCount = (id: string) => view.settings.filter((s) => s.category === id && !s.advanced).length;
|
||||
|
||||
async function save(changes: Record<string, JsonValue>) {
|
||||
const SHOWN = 40; // the long list of other settings is drawn in part; searching narrows it
|
||||
const q = query.trim().toLowerCase();
|
||||
const searching = q !== "";
|
||||
const hit = (...parts: string[]) => parts.some((x) => x.toLowerCase().includes(q));
|
||||
const curatedKeys = new Set(view.settings.map((s) => s.key));
|
||||
const collections = collectionsOf(cols);
|
||||
const curatedHits = searching ? view.settings.filter((s) => hit(s.key, sx.title(s), sx.description(s))) : [];
|
||||
const collectionHits = searching ? collections.filter((c) => hit(c.item.key, t(c.label), t(c.text))) : [];
|
||||
const rawHits = (raw ?? []).filter(
|
||||
(s) => !curatedKeys.has(s.key) && (!onlyChanged || s.changed || s.key in rawEdits) && (!searching || hit(s.key, s.doc)),
|
||||
);
|
||||
const rowOf = (s: SettingView) => (
|
||||
<Row
|
||||
key={s.key}
|
||||
s={s}
|
||||
value={valueOf(s)}
|
||||
error={errors[s.key] ?? null}
|
||||
onChange={(v) => setDraft((d) => ({ ...d, [s.key]: v }))}
|
||||
onReset={() => setDraft((d) => ({ ...d, [s.key]: s.default }))}
|
||||
/>
|
||||
);
|
||||
const tabs = [
|
||||
...view.categories.filter((c) => basicCount(c.id) + advancedCount(c.id) > 0).map((c) => ({ id: c.id, label: sx.category(scope, c) })),
|
||||
...(collections.length ? [{ id: "@collections", label: t("set.tab.collections") }] : []),
|
||||
...(scope === "server" ? [{ id: "@all", label: t("set.tab.all") }] : []),
|
||||
];
|
||||
|
||||
async function save(changes: Record<string, JsonValue>): Promise<boolean> {
|
||||
const local: Record<string, string> = {};
|
||||
for (const [k, v] of Object.entries(changes)) {
|
||||
const s = view!.settings.find((x) => x.key === k)!;
|
||||
@@ -216,9 +279,9 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
if (m) local[k] = m;
|
||||
}
|
||||
setErrors(local);
|
||||
if (Object.keys(local).length) return;
|
||||
if (Object.keys(local).length) return false;
|
||||
const dangerous = Object.keys(changes).filter((k) => view!.settings.find((s) => s.key === k)?.dangerous);
|
||||
if (dangerous.length && !window.confirm(t("set.confirmCareful", { n: dangerous.length }))) return;
|
||||
if (dangerous.length && !window.confirm(t("set.confirmCareful", { n: dangerous.length }))) return false;
|
||||
setBusy(true);
|
||||
setSaveErr(null);
|
||||
try {
|
||||
@@ -227,12 +290,42 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
setPreview(null);
|
||||
await load();
|
||||
void api.status(serverId).then((s) => setRunning(s.observed.world.state === "running"));
|
||||
return true;
|
||||
} catch (e) {
|
||||
const ui = asUiError(e);
|
||||
setSaveErr(ui);
|
||||
const map: Record<string, string> = {};
|
||||
(ui.fields ?? ([] as FieldError[])).forEach((f) => (map[f.key] = f.message));
|
||||
setErrors(map);
|
||||
return false;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
/** The save bar saves all three kinds of edits: the curated settings, the collection switches and the other documented ones. */
|
||||
async function saveEverything() {
|
||||
if (dirtyKeys.length && !(await save(Object.fromEntries(dirtyKeys.map((k) => [k, draft[k]]))))) return;
|
||||
const extra = [...Object.keys(rawEdits), ...Object.keys(colEdits)];
|
||||
if (!extra.length) return;
|
||||
setBusy(true);
|
||||
setSaveErr(null);
|
||||
try {
|
||||
if (Object.keys(rawEdits).length) {
|
||||
await api.allSettingsSave(serverId, rawEdits);
|
||||
setRawEdits({});
|
||||
setRaw(await api.allSettings(serverId));
|
||||
}
|
||||
if (Object.keys(colEdits).length) {
|
||||
await api.moduleSaveSettings(serverId, "client-compat", colEdits);
|
||||
setColEdits({});
|
||||
setCols(await api.moduleSettings(serverId, "client-compat"));
|
||||
}
|
||||
const more = extra.map((key) => ({ key, title: key, restart: "world" as const, dangerous: false }));
|
||||
setSaved((prev) => ({ changed: [...(prev?.changed ?? []), ...more], restart: prev?.restart ?? "world", snapshot: prev?.snapshot ?? null }));
|
||||
void api.status(serverId).then((st) => setRunning(st.observed.world.state === "running"));
|
||||
} catch (e) {
|
||||
setSaveErr(asUiError(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
@@ -363,83 +456,100 @@ export function SettingsPage(props: { serverId: string; scope: Scope; title: str
|
||||
</Card>
|
||||
)}
|
||||
|
||||
<div className="mt-6 flex flex-wrap gap-1 border-b border-line" role="tablist">
|
||||
{view.categories
|
||||
.filter((c) => basicCount(c.id) + advancedCount(c.id) > 0)
|
||||
.map((c) => (
|
||||
<button
|
||||
key={c.id}
|
||||
role="tab"
|
||||
aria-selected={category === c.id}
|
||||
onClick={() => setCategory(c.id)}
|
||||
className={cn(
|
||||
"-mb-px cursor-pointer border-b-2 px-3 py-2 text-sm",
|
||||
category === c.id ? "border-gold text-ink" : "border-transparent text-muted hover:text-ink",
|
||||
<Card className="mt-6 p-0">
|
||||
<div className="border-b border-line px-5 pb-0 pt-4">
|
||||
<label className="relative block">
|
||||
<Search className="pointer-events-none absolute left-3 top-2.5 h-4 w-4 text-muted" aria-hidden />
|
||||
<input
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder={t(scope === "server" ? "set.searchAll" : "set.search")}
|
||||
aria-label={t(scope === "server" ? "set.searchAll" : "set.search")}
|
||||
className="selectable w-full rounded-md border border-line bg-bg py-2 pl-9 pr-3 text-sm outline-none focus:border-gold"
|
||||
/>
|
||||
</label>
|
||||
<div className="mt-3 flex flex-wrap gap-1" role="tablist">
|
||||
{tabs.map((c) => (
|
||||
<button
|
||||
key={c.id}
|
||||
role="tab"
|
||||
aria-selected={!searching && category === c.id}
|
||||
onClick={() => {
|
||||
setQuery("");
|
||||
setCategory(c.id);
|
||||
}}
|
||||
className={cn(
|
||||
"-mb-px cursor-pointer border-b-2 px-3 py-2 text-sm",
|
||||
!searching && category === c.id ? "border-gold text-ink" : "border-transparent text-muted hover:text-ink",
|
||||
)}
|
||||
>
|
||||
{c.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="px-5 pb-5 pt-2">
|
||||
{searching ? (
|
||||
<>
|
||||
{curatedHits.length + collectionHits.length + rawHits.length === 0 && <p className="py-4 text-sm text-muted">{t("all.none")}</p>}
|
||||
<div className="divide-y divide-line">{curatedHits.map(rowOf)}</div>
|
||||
{collectionHits.length > 0 && <CollectionRows items={collectionHits} edits={colEdits} onChange={editCol} />}
|
||||
<RawRows items={rawHits.slice(0, SHOWN)} edits={rawEdits} onChange={editRaw} />
|
||||
{rawHits.length > SHOWN && <p className="pt-3 text-xs text-muted">{t("all.count", { shown: SHOWN, total: rawHits.length })}</p>}
|
||||
</>
|
||||
) : category === "@collections" ? (
|
||||
<>
|
||||
<p className="py-3 text-sm text-muted">{t("col.intro")}</p>
|
||||
<CollectionRows items={collections} edits={colEdits} onChange={editCol} />
|
||||
</>
|
||||
) : category === "@all" ? (
|
||||
<>
|
||||
<p className="py-3 text-sm text-muted">{t("all.intro")}</p>
|
||||
<label className="flex cursor-pointer items-center gap-2 pb-2 text-sm">
|
||||
<input type="checkbox" checked={onlyChanged} onChange={(e) => setOnlyChanged(e.target.checked)} />
|
||||
{t("all.onlyChanged")}
|
||||
</label>
|
||||
{raw === null ? <p className="py-3 text-sm text-muted">{t("set.loading")}</p> : <RawRows items={rawHits.slice(0, SHOWN)} edits={rawEdits} onChange={editRaw} />}
|
||||
{rawHits.length > SHOWN && <p className="pt-3 text-xs text-muted">{t("all.count", { shown: SHOWN, total: rawHits.length })}</p>}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="divide-y divide-line">{basic.map(rowOf)}</div>
|
||||
|
||||
{advanced.length > 0 && (
|
||||
<div className="mt-2">
|
||||
{/* A tab whose settings are all advanced has nothing else to show, so its settings are listed directly. */}
|
||||
{basic.length > 0 && (
|
||||
<button
|
||||
onClick={() => setShowAdvanced((v) => !v)}
|
||||
aria-expanded={showAdvanced}
|
||||
className="flex cursor-pointer items-center gap-1 py-2 text-sm text-muted hover:text-ink"
|
||||
>
|
||||
{showAdvanced ? <ChevronDown className="h-4 w-4" aria-hidden /> : <ChevronRight className="h-4 w-4" aria-hidden />}
|
||||
{t("set.advanced", { n: advanced.length })}
|
||||
</button>
|
||||
)}
|
||||
{(showAdvanced || basic.length === 0) && <div className="divide-y divide-line">{advanced.map(rowOf)}</div>}
|
||||
</div>
|
||||
)}
|
||||
>
|
||||
{sx.category(scope, c)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-line">
|
||||
{basic.map((s) => (
|
||||
<Row
|
||||
key={s.key}
|
||||
s={s}
|
||||
value={valueOf(s)}
|
||||
error={errors[s.key] ?? null}
|
||||
onChange={(v) => setDraft((d) => ({ ...d, [s.key]: v }))}
|
||||
onReset={() => setDraft((d) => ({ ...d, [s.key]: s.default }))}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{advanced.length > 0 && (
|
||||
<div className="mt-4">
|
||||
{/* A tab whose settings are all advanced has nothing else to show, so its settings are listed directly. */}
|
||||
{basic.length > 0 && (
|
||||
<button
|
||||
onClick={() => setShowAdvanced((v) => !v)}
|
||||
aria-expanded={showAdvanced}
|
||||
className="flex cursor-pointer items-center gap-1 text-sm text-muted hover:text-ink"
|
||||
>
|
||||
{showAdvanced ? <ChevronDown className="h-4 w-4" aria-hidden /> : <ChevronRight className="h-4 w-4" aria-hidden />}
|
||||
{t("set.advanced", { n: advanced.length })}
|
||||
</button>
|
||||
)}
|
||||
{(showAdvanced || basic.length === 0) && (
|
||||
<div className="divide-y divide-line">
|
||||
{advanced.map((s) => (
|
||||
<Row
|
||||
key={s.key}
|
||||
s={s}
|
||||
value={valueOf(s)}
|
||||
error={errors[s.key] ?? null}
|
||||
onChange={(v) => setDraft((d) => ({ ...d, [s.key]: v }))}
|
||||
onReset={() => setDraft((d) => ({ ...d, [s.key]: s.default }))}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
|
||||
{view.unknown_keys > 0 && (
|
||||
<p className="mt-6 text-xs text-muted">{t("set.unknownKept", { n: view.unknown_keys })}</p>
|
||||
)}
|
||||
|
||||
{scope === "server" && <CollectionsCard serverId={serverId} />}
|
||||
{scope === "server" && <AllSettingsCard serverId={serverId} />}
|
||||
|
||||
{dirtyKeys.length > 0 && (
|
||||
{pending > 0 && (
|
||||
<div className="fixed bottom-0 left-60 right-0 flex items-center justify-between border-t border-line bg-[#0b0c0e]/95 px-10 py-3">
|
||||
<span className="text-sm text-muted">{tn("set.unsaved", dirtyKeys.length)}</span>
|
||||
<span className="text-sm text-muted">{tn("set.unsaved", pending)}</span>
|
||||
<div className="flex gap-2">
|
||||
<Button variant="ghost" size="sm" onClick={() => { setDraft({}); setErrors({}); }}>
|
||||
<Button variant="ghost" size="sm" onClick={() => { setDraft({}); setErrors({}); setRawEdits({}); setColEdits({}); }}>
|
||||
{t("set.discard")}
|
||||
</Button>
|
||||
<Button variant="primary" size="sm" disabled={busy} onClick={() => void save(Object.fromEntries(dirtyKeys.map((k) => [k, draft[k]])))}>
|
||||
<Button variant="primary" size="sm" disabled={busy} onClick={() => void saveEverything()}>
|
||||
{t("set.save")}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user