mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Merge pull request #1010 from bbassie/feature/plugin-browser
Plugin management through web interface
This commit is contained in:
1 parent
e36a76acc5
commit
ffe82f81d8
48 files changed
+4275
-536
No files matched your search
@@ -2,62 +2,68 @@ import React, { useEffect, useContext, useState } from "react";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { Alert, Descriptions, Spin } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import type { PluginWebApi } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PageHeader from "./PageHeader";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import notify from "../util/notify";
|
||||
import { usePluginInstaller } from "./PluginInstall";
|
||||
import { PluginMachinesTable, PluginUpdateButton, useLatestVersions, useMachinePlugins } from "./PluginClusterStatus";
|
||||
import { PluginStatus, computePluginStatuses, pluginStatusKey } from "../util/plugin_status";
|
||||
|
||||
export default function PluginViewPage() {
|
||||
let params = useParams();
|
||||
const control = useContext(ControlContext);
|
||||
let [pluginList, setPluginList] = useState<PluginWebApi[]>();
|
||||
let pluginName = String(params.name);
|
||||
type PluginMachinesProps = {
|
||||
status: PluginStatus,
|
||||
title: string,
|
||||
refresh: (addresses: lib.AddressShorthand[]) => void,
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
let response = await fetch(`${webRoot}api/plugins`);
|
||||
if (response.ok) {
|
||||
setPluginList(await response.json());
|
||||
} else {
|
||||
notify("Failed to load plugin list");
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
function PluginMachines(props: PluginMachinesProps) {
|
||||
const installer = usePluginInstaller(props.refresh);
|
||||
const { npmPackage } = props.status;
|
||||
const latestVersions = useLatestVersions(npmPackage ? [npmPackage] : []);
|
||||
const latest = npmPackage ? latestVersions[npmPackage] : undefined;
|
||||
|
||||
const loadedPlugin = control.loadedPlugins.get(pluginName);
|
||||
const pluginInfo = control.pluginInfos.get(pluginName);
|
||||
const pluginTitle = pluginInfo ? pluginInfo.title : pluginName;
|
||||
let nav = [{ name: "Plugins", path: "/plugins" }, { name: pluginTitle }];
|
||||
if (!pluginList) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle} />
|
||||
<Spin size="large" />
|
||||
</PageLayout>;
|
||||
}
|
||||
return <>
|
||||
<Descriptions bordered size="small" style={{ marginTop: "1em" }}>
|
||||
<Descriptions.Item label="Package" span={npmPackage ? 1 : 3}>
|
||||
{npmPackage ?? "Not published to npm"}
|
||||
</Descriptions.Item>
|
||||
{npmPackage && <Descriptions.Item label="Latest version" span={2}>
|
||||
{latest ?? "Unknown"}
|
||||
</Descriptions.Item>}
|
||||
</Descriptions>
|
||||
<SectionHeader
|
||||
title="Machines"
|
||||
extra={<PluginUpdateButton
|
||||
status={props.status} title={props.title} latest={latest} installer={installer}
|
||||
/>}
|
||||
/>
|
||||
<PluginMachinesTable status={props.status} latest={latest} installer={installer} />
|
||||
</>;
|
||||
}
|
||||
|
||||
let pluginMeta = pluginList.find(p => p.name === pluginName);
|
||||
if (!pluginMeta) {
|
||||
return <PageLayout nav={nav}>
|
||||
<PageHeader title="Plugin not found" />
|
||||
<p>Plugin with name {pluginName} was not found on the controller.</p>
|
||||
</PageLayout>;
|
||||
}
|
||||
type ControllerPluginProps = {
|
||||
pluginMeta: PluginWebApi,
|
||||
pluginInfo?: lib.PluginWebpackEnvInfo,
|
||||
loadedPlugin?: lib.PluginWebpackEnvInfo,
|
||||
title: string,
|
||||
};
|
||||
|
||||
/** What the controller and web UI have loaded of the plugin */
|
||||
function ControllerPlugin(props: ControllerPluginProps) {
|
||||
const { pluginMeta, pluginInfo, loadedPlugin } = props;
|
||||
const pluginError = pluginMeta.web.error ?? pluginInfo?.error;
|
||||
const hasWebModule = Boolean(pluginMeta.web.main || pluginMeta.web.error);
|
||||
if (!loadedPlugin) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle}>
|
||||
return <>
|
||||
<Descriptions bordered size="small" title={props.title}>
|
||||
<Descriptions.Item label="Version">{pluginMeta.version}</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded in Web UI" span={2}>
|
||||
{hasWebModule ? "No" : "No web module"}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded on controller">{pluginMeta.loaded ? "Yes" : "No"}</Descriptions.Item>
|
||||
<Descriptions.Item label="Enabled on controller" span={2}>
|
||||
{pluginMeta.enabled ? "Yes" : "No"}
|
||||
</Descriptions.Item>
|
||||
</Descriptions>
|
||||
{pluginError && <Alert
|
||||
style={{
|
||||
@@ -73,20 +79,19 @@ export default function PluginViewPage() {
|
||||
type="error"
|
||||
showIcon
|
||||
/>}
|
||||
</PageLayout>;
|
||||
</>;
|
||||
}
|
||||
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle}>
|
||||
const homepageUrl = lib.safeExternalUrl(loadedPlugin.package.homepage);
|
||||
return <>
|
||||
<Descriptions bordered size="small" title={props.title}>
|
||||
<Descriptions.Item label="Version">{pluginMeta.version}</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded in Web UI" span={2}>Yes</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded on controller">{pluginMeta.loaded ? "Yes" : "No"}</Descriptions.Item>
|
||||
<Descriptions.Item label="Enabled on controller" span={2}>
|
||||
{pluginMeta.enabled ? "Yes" : "No"}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Description" span={3}>{loadedPlugin.description}</Descriptions.Item>
|
||||
{loadedPlugin.package.homepage ? <Descriptions.Item label="Homepage" span={3}>
|
||||
<a href={loadedPlugin.package.homepage}>{loadedPlugin.package.homepage}</a>
|
||||
{homepageUrl
|
||||
? <a href={homepageUrl}>{loadedPlugin.package.homepage}</a>
|
||||
: loadedPlugin.package.homepage}
|
||||
</Descriptions.Item> : null}
|
||||
{loadedPlugin.package.author ? <Descriptions.Item label="Author" span={3}>
|
||||
{loadedPlugin.package.author}
|
||||
@@ -107,5 +112,60 @@ export default function PluginViewPage() {
|
||||
type="warning"
|
||||
showIcon
|
||||
/> : null}
|
||||
</>;
|
||||
}
|
||||
|
||||
export default function PluginViewPage() {
|
||||
let params = useParams();
|
||||
const control = useContext(ControlContext);
|
||||
let [pluginList, setPluginList] = useState<PluginWebApi[]>();
|
||||
let pluginName = String(params.name);
|
||||
const { machines, unknown, refresh } = useMachinePlugins();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
let response = await fetch(`${webRoot}api/plugins`);
|
||||
if (response.ok) {
|
||||
setPluginList(await response.json());
|
||||
} else {
|
||||
notify("Failed to load plugin list");
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const pluginMeta = pluginList?.find(p => p.name === pluginName);
|
||||
// Plugins the controller hasn't loaded are linked to by their npm package or name on the hosts
|
||||
const status = machines
|
||||
? computePluginStatuses(machines, unknown).get(pluginMeta ? pluginStatusKey(pluginMeta) : pluginName)
|
||||
: undefined;
|
||||
const pluginInfo = control.pluginInfos.get(pluginName);
|
||||
const pluginTitle = pluginInfo?.title ?? status?.title ?? pluginName;
|
||||
let nav = [{ name: "Plugins", path: "/plugins" }, { name: pluginTitle }];
|
||||
if (!pluginList || (!pluginMeta && !machines)) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle} />
|
||||
<Spin size="large" />
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
if (!pluginMeta && !status) {
|
||||
return <PageLayout nav={nav}>
|
||||
<PageHeader title="Plugin not found" />
|
||||
<p>Plugin with name {pluginName} was not found on the controller or any host.</p>
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
return <PageLayout nav={nav}>
|
||||
{pluginMeta
|
||||
? <ControllerPlugin
|
||||
pluginMeta={pluginMeta}
|
||||
pluginInfo={pluginInfo}
|
||||
loadedPlugin={control.loadedPlugins.get(pluginName)}
|
||||
title={pluginTitle}
|
||||
/>
|
||||
: <Descriptions bordered size="small" title={pluginTitle}>
|
||||
<Descriptions.Item label="Loaded on controller">No</Descriptions.Item>
|
||||
</Descriptions>}
|
||||
{status && <PluginMachines status={status} title={pluginTitle} refresh={refresh} />}
|
||||
</PageLayout>;
|
||||
}
|
||||
Reference in new issue
Block a user