import { useEffect, useState } from "react"; import { ExternalLink, Loader2 } from "lucide-react"; import { api, asUiError, type AddonView, type ClientInfo } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { useI18n } from "@/i18n"; function megabytes(bytes: number): string { return (bytes / 1024 / 1024).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0); } /** Addons from the Manager's catalog: each pinned to a commit of its author's repository and checked file by file. */ export function AddonCatalog({ serverId, companions, onCompanionsInstalled }: { serverId: string; companions?: ClientInfo["addon"] | null; onCompanionsInstalled?: () => void }) { const { t, locale } = useI18n(); const [addons, setAddons] = useState(null); const [working, setWorking] = useState(null); const [error, setError] = useState(null); useEffect(() => { let alive = true; void api.addonsList(serverId).then(list => { if (alive) setAddons(list); }).catch(e => { if (alive) setError(asUiError(e).technical); }); return () => { alive = false; }; }, [serverId]); async function run(id: string, action: "install" | "remove") { setWorking(id); setError(null); try { setAddons(await (action === "install" ? api.addonsInstall : api.addonsRemove)(serverId, id)); } catch (e) { setError(asUiError(e).technical); } finally { setWorking(null); } } async function installCompanions() { setWorking("coa-companions"); setError(null); try { await api.installAddon(serverId); onCompanionsInstalled?.(); } catch (e) { setError(asUiError(e).technical); } finally { setWorking(null); } } return

{t("addons.title")}

{t("addons.hint")}

    {companions &&
  • CoA Companions (CoABotUI) {companions.installed && companions.up_to_date !== false && {companions.version ? t("client.installedVersion", { v: companions.version }) : t("addons.installed")}} {companions.up_to_date === false && {t("addons.update")}}

    {t("client.addonWhat")}

    {t("addons.withServer")}

    {(!companions.installed || companions.up_to_date === false) &&
    }
  • } {addons?.map(a =>
  • {a.name} {t("addons.by", { author: a.author })}{a.size > 0 ? ` ยท ${megabytes(a.size)} MB` : ""} {a.installed && !a.updateAvailable && {t("addons.installed")}} {a.updateAvailable && {t("addons.update")}}

    {a.description[locale] ?? a.description.en}

    {a.needs === "playerbots" &&

    {t("addons.needsSquid")}

    } {a.bundled === "custom-races" &&

    {a.installed ? t("addons.withRaces") : t("addons.withRacesMissing")}

    } {a.foreignFolders.length > 0 && !a.installed &&

    {t("addons.foreign", { folders: a.foreignFolders.join(", ") })}

    } {!a.bundled &&
    {(!a.installed || a.updateAvailable) && } {a.installed && }
    }
  • )}
{error &&

{error}

}
; }