mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53:13 +09:00
Add PluginsContext to web_ui
Add a React context to access the plugins loaded in the web interface. The info, /api/plugins metadata and webpack container is provided for each plugin.
This commit is contained in:
1 parent
d0befe5944
commit
3a3a7597bf
3 files changed
+28
-16
No files matched your search
@@ -25,7 +25,7 @@ async function loadScript(url) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadPluginInfos() {
|
async function loadPlugins() {
|
||||||
let response = await fetch(`${webRoot}api/plugins`);
|
let response = await fetch(`${webRoot}api/plugins`);
|
||||||
let pluginList;
|
let pluginList;
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@@ -36,22 +36,28 @@ async function loadPluginInfos() {
|
|||||||
pluginList = [];
|
pluginList = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
let pluginInfos = [];
|
let plugins = [];
|
||||||
await __webpack_init_sharing__("default");
|
await __webpack_init_sharing__("default");
|
||||||
for (let plugin of pluginList) {
|
for (let meta of pluginList) {
|
||||||
try {
|
try {
|
||||||
await loadScript(`${webRoot}plugin/${plugin.name}/remoteEntry.js`);
|
await loadScript(`${webRoot}plugin/${meta.name}/remoteEntry.js`);
|
||||||
let container = window[`plugin_${plugin.name}`];
|
let container = window[`plugin_${meta.name}`];
|
||||||
await container.init(__webpack_share_scopes__.default);
|
await container.init(__webpack_share_scopes__.default);
|
||||||
let info = (await container.get("./info"))();
|
let pluginInfo = (await container.get("./info"))();
|
||||||
info.container = container;
|
plugins.push({
|
||||||
pluginInfos.push(info);
|
meta,
|
||||||
|
info: pluginInfo,
|
||||||
|
container,
|
||||||
|
});
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(`Failed to load plugin info for ${plugin.name}`);
|
logger.error(`Failed to load plugin info for ${meta.name}`);
|
||||||
|
plugins.push({
|
||||||
|
meta,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pluginInfos;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
@@ -59,8 +65,8 @@ async function load() {
|
|||||||
level: "verbose",
|
level: "verbose",
|
||||||
format: new WebConsoleFormat(),
|
format: new WebConsoleFormat(),
|
||||||
}));
|
}));
|
||||||
let pluginInfos = await loadPluginInfos();
|
let plugins = await loadPlugins();
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(plugins.filter(p => p.info).map(p => p.info));
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
let wsUrl = new URL(window.webRoot, document.location);
|
let wsUrl = new URL(window.webRoot, document.location);
|
||||||
@@ -69,7 +75,7 @@ async function load() {
|
|||||||
let controlConnector = new ControlConnector(wsUrl, 10);
|
let controlConnector = new ControlConnector(wsUrl, 10);
|
||||||
let control = new Control(controlConnector, []);
|
let control = new Control(controlConnector, []);
|
||||||
|
|
||||||
ReactDOM.render(<App control={control}/>, document.getElementById("root"));
|
ReactDOM.render(<App control={control} plugins={plugins}/>, document.getElementById("root"));
|
||||||
}
|
}
|
||||||
|
|
||||||
load().catch((err) => logger.fatal(err.stack));
|
load().catch((err) => logger.fatal(err.stack));
|
||||||
@@ -7,6 +7,7 @@ import { logger } from "@clusterio/lib/logging";
|
|||||||
import basename from "../basename";
|
import basename from "../basename";
|
||||||
import SiteLayout from "./SiteLayout";
|
import SiteLayout from "./SiteLayout";
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
|
import PluginsContext from "./PluginsContext";
|
||||||
import LoginForm from "./LoginForm";
|
import LoginForm from "./LoginForm";
|
||||||
|
|
||||||
import { Card, Spin } from "antd";
|
import { Card, Spin } from "antd";
|
||||||
@@ -77,9 +78,11 @@ export default function App(props) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ControlContext.Provider value={props.control}>
|
<ControlContext.Provider value={props.control}>
|
||||||
<BrowserRouter basename={basename}>
|
<PluginsContext.Provider value={props.plugins}>
|
||||||
{page}
|
<BrowserRouter basename={basename}>
|
||||||
</BrowserRouter>
|
{page}
|
||||||
|
</BrowserRouter>
|
||||||
|
</PluginsContext.Provider>
|
||||||
</ControlContext.Provider>
|
</ControlContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
export default React.createContext();
|
||||||
Reference in new issue
Block a user