mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 11:25:56 +00:00
144 lines
7.1 KiB
TypeScript
144 lines
7.1 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { listen } from "@tauri-apps/api/event";
|
|
import { open } from "@tauri-apps/plugin-dialog";
|
|
import { api, asUiError, type ClientInfo, type ClientStatus, type UiError } from "@/lib/api";
|
|
import { checkClient } from "@/lib/clientUpdate";
|
|
import { ClientDialog, type ClientDialogMode } from "@/screens/ClientDialog";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Card } from "@/components/ui/card";
|
|
import { useHuman, useT } from "@/i18n";
|
|
import { useStartAfterUpdate } from "@/lib/updatePrefs";
|
|
|
|
export function ClientCard({ serverId, remote = false }: { serverId: string; remote?: boolean }) {
|
|
const t = useT();
|
|
const human = useHuman();
|
|
const [info, setInfo] = useState<ClientInfo | null | undefined>(undefined);
|
|
const [error, setError] = useState<UiError | null>(null);
|
|
const [note, setNote] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [status, setStatus] = useState<ClientStatus | null>(null);
|
|
const [patchProgress, setPatchProgress] = useState<string | null>(null);
|
|
const [dialog, setDialog] = useState<ClientDialogMode | null>(null);
|
|
const [playAfter, setPlayAfter] = useStartAfterUpdate("client", serverId);
|
|
|
|
const refresh = () => {
|
|
void api.clientStatus(serverId).then(setStatus).catch(() => setStatus(null));
|
|
return api.clientInfo(serverId).then(setInfo).catch((e) => setError(asUiError(e)));
|
|
};
|
|
useEffect(() => {
|
|
void refresh();
|
|
const unlisten = listen<{ id: string; step: string; percent: number }>("custom-races-progress", e => {
|
|
if (e.payload.id === serverId) setPatchProgress(`${e.payload.step} (${Math.round(e.payload.percent * 100)}%)`);
|
|
});
|
|
return () => { void unlisten.then(f => f()); };
|
|
}, [serverId]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
async function run(fn: () => Promise<string | void>) {
|
|
setBusy(true);
|
|
setError(null);
|
|
setNote(null);
|
|
try {
|
|
const msg = await fn();
|
|
if (msg) setNote(msg);
|
|
await refresh();
|
|
void checkClient(serverId);
|
|
} catch (e) {
|
|
setError(asUiError(e));
|
|
} finally {
|
|
setBusy(false);
|
|
setPatchProgress(null);
|
|
}
|
|
}
|
|
|
|
async function choose() {
|
|
const picked = await open({ directory: true, multiple: false, title: t("client.dialogTitle") });
|
|
if (typeof picked === "string") await run(async () => void (await api.setClient(serverId, picked)));
|
|
}
|
|
|
|
const hosts = info?.realmlists.map((r) => r.host) ?? [];
|
|
const pointsLocal = hosts.length > 0 && hosts.every((h) => h === "127.0.0.1" || h === "localhost");
|
|
|
|
return (
|
|
<Card className="mt-6 p-6">
|
|
<h2 className="font-semibold">{t("client.title")}</h2>
|
|
{info === undefined && <p className="mt-2 text-sm text-muted">{t("client.looking")}</p>}
|
|
{info === null && (
|
|
<>
|
|
<p className="mt-1 text-sm text-muted">{t(remote ? "remote.clientIntro" : "client.intro")}</p>
|
|
<Button className="mt-4" onClick={() => setDialog("setup")} disabled={busy}>{t("btn.setupClient")}</Button>
|
|
</>
|
|
)}
|
|
{info && (
|
|
<>
|
|
<p className="selectable mt-1 break-all text-sm text-muted">{info.path}</p>
|
|
<dl className="mt-4 grid grid-cols-[auto_1fr] gap-x-6 gap-y-2 text-sm">
|
|
<dt className="text-muted">{t("client.connectsTo")}</dt>
|
|
<dd>
|
|
{hosts.length ? hosts.filter(Boolean).join(", ") : "—"}
|
|
{!remote && (pointsLocal ? <span className="ml-2 text-ok">{t("client.thisComputer")}</span> : <span className="ml-2 text-warn">{t("client.notThisServer")}</span>)}
|
|
</dd>
|
|
<dt className="text-muted">{t("client.version")}</dt>
|
|
<dd>
|
|
{status?.managed
|
|
? status.installed_version ?? t("client.versionUnfinished")
|
|
: t("client.versionUntracked")}
|
|
{status?.update_available && <span className="ml-2 text-warn">{t("client.updateAvailable")}</span>}
|
|
</dd>
|
|
{!remote && <>
|
|
<dt className="text-muted">{t("client.addon")}</dt>
|
|
<dd>
|
|
{info.addon.installed ? (info.addon.version ? t("client.installedVersion", { v: info.addon.version }) : t("client.installed")) : t("client.addonNotInstalled")}
|
|
{info.addon.up_to_date === false && <span className="ml-2 text-warn">{t("client.updateAvailable")}</span>}
|
|
<span className="mt-1 block text-xs text-muted">{t("client.addonWhat")}</span>
|
|
</dd>
|
|
</>}
|
|
<dt className="text-muted">{t("client.otherAddons")}</dt>
|
|
<dd>{t("client.leftAsIs", { n: info.other_addons })}</dd>
|
|
</dl>
|
|
{!remote && (
|
|
<label className="mt-4 flex cursor-pointer items-center gap-2 text-sm">
|
|
<input type="checkbox" checked={playAfter} onChange={(e) => setPlayAfter(e.target.checked)} className="h-4 w-4 accent-[#c9a24a]" />
|
|
<span>{t("client.startAfter")}</span>
|
|
</label>
|
|
)}
|
|
<div className="mt-4 flex flex-wrap gap-2">
|
|
{!remote && !pointsLocal && (
|
|
<Button size="sm" disabled={busy} onClick={() => void run(async () => { await api.setRealmlist(serverId, "127.0.0.1"); return t("client.pointedNote"); })}>
|
|
{t("client.pointLocal")}
|
|
</Button>
|
|
)}
|
|
{!remote && (!info.addon.installed || info.addon.up_to_date === false) && (
|
|
<Button size="sm" disabled={busy} onClick={() => void run(async () => { await api.installAddon(serverId); return t("client.addonNote"); })}>
|
|
{info.addon.installed ? t("client.updateAddon") : t("client.installAddon")}
|
|
</Button>
|
|
)}
|
|
{(status?.update_available || (remote && status?.managed)) && (
|
|
<Button size="sm" variant="primary" disabled={busy} onClick={() => setDialog("update")}>{t("client.upd.start")}</Button>
|
|
)}
|
|
{status && !status.managed && (
|
|
<Button size="sm" disabled={busy} onClick={() => setDialog("track")}>{t("client.track.button")}</Button>
|
|
)}
|
|
{remote && <Button size="sm" disabled={busy} onClick={() => void run(() => api.customRacesInstall(serverId))}>{t("client.races.install")}</Button>}
|
|
<Button size="sm" variant="ghost" disabled={busy} onClick={() => void choose()}>{t("client.changeFolder")}</Button>
|
|
<Button size="sm" variant="ghost" disabled={busy} onClick={() => setDialog("setup")}>{t("client.downloadInstead")}</Button>
|
|
</div>
|
|
</>
|
|
)}
|
|
{patchProgress && <p className="mt-3 text-sm text-muted" role="status">{patchProgress}</p>}
|
|
{note && <p className="mt-3 text-sm text-ok" role="status">{note}</p>}
|
|
{error && <p className="mt-3 text-sm text-bad" role="alert">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>}
|
|
{dialog && (
|
|
<ClientDialog
|
|
serverId={serverId}
|
|
mode={dialog}
|
|
onClose={() => setDialog(null)}
|
|
onChanged={() => {
|
|
void refresh();
|
|
void checkClient(serverId);
|
|
}}
|
|
/>
|
|
)}
|
|
</Card>
|
|
);
|
|
}
|