mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 23:04:52 +09:00
In order to import a path inside packages/lib/ in the webpack shared
module environment it has to be declared as shared in both the web_ui
webpack config and the plugin's webpack config. This is rather error
prone and easy to miss. Move the burden of specifying which entrypoints
are valid in lib to lib itself by changing all imports in the web
interface code to import from @clusterio/lib instead sub paths.
For web code this means imports is done like this from now on:
import { libConfig, libLink } from "@clusterio/lib";
For info.js which has to be loadable by both Node.js and web code the
imports are done as follows:
const { libConfig, libLink } = require("@clusterio/lib");
While the Node.js only code can either continue to import sub paths
directly or use the info.js style.
82 lines
1.9 KiB
React
82 lines
1.9 KiB
React
import React, { useEffect, useContext, useState } from "react";
|
|
import ControlContext from "../components/ControlContext";
|
|
|
|
import { libLink, libLogging } from "@clusterio/lib";
|
|
const { logger } = libLogging;
|
|
|
|
|
|
export function useInstance(id) {
|
|
let control = useContext(ControlContext);
|
|
let [instance, setInstance] = useState({ loading: true });
|
|
|
|
function updateInstance() {
|
|
libLink.messages.getInstance.send(control, { id }).then(result => {
|
|
setInstance({ ...result, present: true });
|
|
}).catch(err => {
|
|
logger.error(`Failed to get instance: ${err}`);
|
|
setInstance({ missing: true });
|
|
});
|
|
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (!Number.isInteger(id)) {
|
|
setInstance({ missing: true });
|
|
return undefined;
|
|
}
|
|
updateInstance();
|
|
|
|
function updateHandler(newInstance) {
|
|
setInstance({ ...newInstance, present: true });
|
|
}
|
|
|
|
control.onInstanceUpdate(id, updateHandler);
|
|
return () => {
|
|
control.offInstanceUpdate(id, updateHandler);
|
|
};
|
|
}, [id]);
|
|
|
|
return [instance, updateInstance];
|
|
}
|
|
|
|
export function useInstanceList() {
|
|
let control = useContext(ControlContext);
|
|
let [instanceList, setInstanceList] = useState([]);
|
|
|
|
function updateInstanceList() {
|
|
libLink.messages.listInstances.send(control).then(result => {
|
|
setInstanceList(result.list);
|
|
}).catch(err => {
|
|
logger.error(`Failed to list instances:\n${err}`);
|
|
});
|
|
}
|
|
|
|
useEffect(() => {
|
|
updateInstanceList();
|
|
|
|
function updateHandler(newInstance) {
|
|
setInstanceList(oldList => {
|
|
let newList = oldList.concat();
|
|
let index = newList.findIndex(s => s.id === newInstance.id);
|
|
if (newInstance.status !== "deleted") {
|
|
if (index !== -1) {
|
|
newList[index] = newInstance;
|
|
} else {
|
|
newList.push(newInstance);
|
|
}
|
|
} else if (index !== -1) {
|
|
newList.splice(index, 1);
|
|
}
|
|
return newList;
|
|
});
|
|
}
|
|
|
|
control.onInstanceUpdate(null, updateHandler);
|
|
return () => {
|
|
control.offInstanceUpdate(null, updateHandler);
|
|
};
|
|
}, []);
|
|
|
|
return [instanceList];
|
|
}
|