import { useEffect, useState } from "react"; 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(undefined); const [error, setError] = useState(null); const [note, setNote] = useState(null); const [busy, setBusy] = useState(false); const [status, setStatus] = useState(null); const [dialog, setDialog] = useState(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(); }, [serverId]); // eslint-disable-line react-hooks/exhaustive-deps async function run(fn: () => Promise) { 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); } } 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 (

{t("client.title")}

{info === undefined &&

{t("client.looking")}

} {info === null && ( <>

{t(remote ? "remote.clientIntro" : "client.intro")}

)} {info && ( <>

{info.path}

{t("client.connectsTo")}
{hosts.length ? hosts.filter(Boolean).join(", ") : "—"} {!remote && (pointsLocal ? {t("client.thisComputer")} : {t("client.notThisServer")})}
{t("client.version")}
{status?.managed ? status.installed_version ?? t("client.versionUnfinished") : t("client.versionUntracked")} {status?.update_available && {t("client.updateAvailable")}}
{!remote && <>
{t("client.addon")}
{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 && {t("client.updateAvailable")}} {t("client.addonWhat")}
}
{t("client.otherAddons")}
{t("client.leftAsIs", { n: info.other_addons })}
{!remote && ( )}
{!remote && !pointsLocal && ( )} {!remote && (!info.addon.installed || info.addon.up_to_date === false) && ( )} {(status?.update_available || (remote && status?.managed)) && ( )} {status && !status.managed && ( )}
)} {note &&

{note}

} {error &&

{error.human.code === "unknown" ? error.technical : human(error.human).message}

} {dialog && ( setDialog(null)} onChanged={() => { void refresh(); void checkClient(serverId); }} /> )}
); }