mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Port packages/web_ui to TypeScript (#508)
* .jsx to .tsx * controller: tsc --build before webpack-cli * Type Annotation * disabled move into the <Input/> and <Checkbox/> * cleanup debug left over * documentation * default ModPack * modPackUpdateHandlers no undefined * PluginInfo changes proposition * PluginInfo changes proposition miss * ModRecord & ModInfo feedback loop at const mods = [ ? * ["connect", "drop", "resume", "close"] as const * .tsx to .ts when possible
This commit is contained in:
1 parent
a4893751e9
commit
838170a9ff
92 files changed
+1104
-707
No files matched your search
@@ -1,24 +1,17 @@
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
|
||||
/**
|
||||
* @typedef {Object} Account
|
||||
* @property {?string} name - Name of the currently logged in account.
|
||||
* @property {?Array<object>} roles - Roles of the corrently logged in account.
|
||||
* @property {function(string)} hasPermission -
|
||||
* Check if the currently logged in account has the given permission.
|
||||
* @property {function()} logOut - Logs out of the web interface.
|
||||
*/
|
||||
|
||||
export function useAccount() {
|
||||
export function useAccount(): lib.UserAccount {
|
||||
let control = useContext(ControlContext);
|
||||
let [name, setName] = useState(control.accountName);
|
||||
let [roles, setRoles] = useState(control.accountRoles);
|
||||
let [name, setName] = useState<string>(control.accountName || "");
|
||||
let [roles, setRoles] = useState<any[]>(control.accountRoles || []);
|
||||
|
||||
useEffect(() => {
|
||||
function onAccountUpdate(account) {
|
||||
function onAccountUpdate(account: lib.AccountDetails) {
|
||||
setName(account.name);
|
||||
setRoles(account.roles);
|
||||
}
|
||||
@@ -28,7 +21,7 @@ export function useAccount() {
|
||||
};
|
||||
}, [control]);
|
||||
|
||||
function hasPermission(permission) {
|
||||
function hasPermission(permission: string): boolean {
|
||||
for (let role of roles) {
|
||||
if (role.permissions.includes("core.admin") || role.permissions.includes(permission)) {
|
||||
return true;
|
||||
@@ -40,13 +33,13 @@ export function useAccount() {
|
||||
return {
|
||||
name,
|
||||
roles,
|
||||
hasPermission(permission) {
|
||||
hasPermission(permission: string): boolean | null {
|
||||
if (!roles) {
|
||||
return null;
|
||||
}
|
||||
return hasPermission(permission);
|
||||
},
|
||||
hasAnyPermission(...permissions) {
|
||||
hasAnyPermission(...permissions: string[]): boolean | null {
|
||||
if (!roles) {
|
||||
return null;
|
||||
}
|
||||
@@ -57,7 +50,7 @@ export function useAccount() {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
hasAllPermission(...permissions) {
|
||||
hasAllPermission(...permissions: string[]): boolean | null {
|
||||
if (!roles) {
|
||||
return null;
|
||||
}
|
||||
@@ -70,7 +63,8 @@ export function useAccount() {
|
||||
},
|
||||
logOut() {
|
||||
control.loggingOut = true;
|
||||
control.shutdown(notifyErrorHandler("Error logging out"));
|
||||
notifyErrorHandler("Error logging out");
|
||||
control.shutdown();
|
||||
},
|
||||
};
|
||||
}
|
||||
+4
-5
@@ -3,12 +3,11 @@ import { useEffect, useContext, useState } from "react";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
|
||||
|
||||
let exportManifestCache = null;
|
||||
let emptyCache = {};
|
||||
export function useExportManifest() {
|
||||
let exportManifestCache: lib.ExportManifest|null = null;
|
||||
let emptyCache: any = {};
|
||||
export function useExportManifest(): lib.ExportManifest|null {
|
||||
const control = useContext(ControlContext);
|
||||
let [exportManifest, setExportManifest] = useState(exportManifestCache || emptyCache);
|
||||
let [exportManifest, setExportManifest] = useState<lib.ExportManifest|null>(exportManifestCache || emptyCache);
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
let modPack = await control.send(new lib.ModPackGetDefaultRequest());
|
||||
@@ -4,13 +4,20 @@ import ControlContext from "../components/ControlContext";
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
|
||||
export function useHost(id) {
|
||||
export type HostState = Partial<lib.HostDetails> & {
|
||||
loading?: boolean,
|
||||
present?: boolean,
|
||||
missing?: boolean,
|
||||
}
|
||||
|
||||
export function useHost(id: number): [ HostState, () => void ] {
|
||||
let control = useContext(ControlContext);
|
||||
let [host, setHost] = useState({ loading: true });
|
||||
let [host, setHost] = useState<HostState>({ loading: true });
|
||||
|
||||
function updateHost() {
|
||||
// XXX optimize by requesting only the host in question
|
||||
control.send(new lib.HostListRequest()).then(hosts => {
|
||||
control.send(new lib.HostListRequest())
|
||||
.then((hosts: lib.HostDetails[]) => {
|
||||
let match = hosts.find(i => i.id === id);
|
||||
if (!match) {
|
||||
setHost({ missing: true });
|
||||
@@ -27,8 +34,8 @@ export function useHost(id) {
|
||||
}
|
||||
updateHost();
|
||||
|
||||
function updateHandler(newHost) {
|
||||
setHost({ ...newHost, present: true });
|
||||
function updateHandler(newHost: lib.HostDetails) {
|
||||
setHost({ ...newHost, loading:false, missing:false, present:true });
|
||||
}
|
||||
|
||||
control.onHostUpdate(id, updateHandler);
|
||||
@@ -42,10 +49,11 @@ export function useHost(id) {
|
||||
|
||||
export function useHostList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [hostList, setHostList] = useState([]);
|
||||
let [hostList, setHostList] = useState<lib.HostDetails[]>([]);
|
||||
|
||||
function updateHostList() {
|
||||
control.send(new lib.HostListRequest()).then(hosts => {
|
||||
control.send(new lib.HostListRequest())
|
||||
.then((hosts: lib.HostDetails[]) => {
|
||||
setHostList(hosts);
|
||||
});
|
||||
}
|
||||
@@ -53,7 +61,7 @@ export function useHostList() {
|
||||
useEffect(() => {
|
||||
updateHostList();
|
||||
|
||||
function updateHandler(newHost) {
|
||||
function updateHandler(newHost: lib.HostDetails) {
|
||||
setHostList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
let index = newList.findIndex(s => s.id === newHost.id);
|
||||
@@ -5,18 +5,23 @@ import * as lib from "@clusterio/lib";
|
||||
|
||||
const { logger } = lib;
|
||||
|
||||
export function useInstance(id) {
|
||||
export type InstanceState = Partial<lib.InstanceDetails> & {
|
||||
loading?: boolean,
|
||||
present?: boolean,
|
||||
missing?: boolean,
|
||||
}
|
||||
|
||||
export function useInstance(id: number): [InstanceState, ()=>void] {
|
||||
let control = useContext(ControlContext);
|
||||
let [instance, setInstance] = useState({ loading: true });
|
||||
let [instance, setInstance] = useState<InstanceState>({ loading: true });
|
||||
|
||||
function updateInstance() {
|
||||
control.send(new lib.InstanceDetailsGetRequest(id)).then(updatedInstance => {
|
||||
setInstance({ ...updatedInstance, present: true });
|
||||
}).catch(err => {
|
||||
}).catch((err: any) => {
|
||||
logger.error(`Failed to get instance: ${err}`);
|
||||
setInstance({ missing: true });
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
@@ -26,7 +31,7 @@ export function useInstance(id) {
|
||||
}
|
||||
updateInstance();
|
||||
|
||||
function updateHandler(newInstance) {
|
||||
function updateHandler(newInstance: lib.InstanceDetails) {
|
||||
setInstance({ ...newInstance, present: true });
|
||||
}
|
||||
|
||||
@@ -41,7 +46,7 @@ export function useInstance(id) {
|
||||
|
||||
export function useInstanceList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [instanceList, setInstanceList] = useState([]);
|
||||
let [instanceList, setInstanceList] = useState<lib.InstanceDetails[]>([]);
|
||||
|
||||
function updateInstanceList() {
|
||||
control.send(new lib.InstanceDetailsListRequest()).then(instances => {
|
||||
@@ -54,7 +59,7 @@ export function useInstanceList() {
|
||||
useEffect(() => {
|
||||
updateInstanceList();
|
||||
|
||||
function updateHandler(newInstance) {
|
||||
function updateHandler(newInstance: lib.InstanceDetails) {
|
||||
setInstanceList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
let index = newList.findIndex(s => s.id === newInstance.id);
|
||||
+15
-12
@@ -3,10 +3,15 @@ import { useEffect, useState } from "react";
|
||||
import { useExportManifest } from "./export_manifest";
|
||||
|
||||
|
||||
let itemMetadataCache = null;
|
||||
type Metadata = {
|
||||
x: number;
|
||||
y: number;
|
||||
size: number;
|
||||
};
|
||||
let itemMetadataCache: Map<string, Metadata> | null = null;
|
||||
export function useItemMetadata() {
|
||||
let exportManifest = useExportManifest();
|
||||
let [itemMetadata, setItemMetadata] = useState(itemMetadataCache || new Map());
|
||||
let [itemMetadata, setItemMetadata] = useState<Map<string, Metadata>>(itemMetadataCache || new Map());
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
if (
|
||||
@@ -17,22 +22,21 @@ export function useItemMetadata() {
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let response = await fetch(`${staticRoot}static/${exportManifest.assets["item-metadata"]}`);
|
||||
let response = await fetch(`${window.staticRoot}static/${exportManifest.assets["item-metadata"]}`);
|
||||
if (response.ok) {
|
||||
let data = await response.json();
|
||||
itemMetadataCache = new Map(data);
|
||||
let style = document.createElement("style");
|
||||
style.type = "text/css";
|
||||
document.head.appendChild(style);
|
||||
for (let [name, meta] of itemMetadataCache) {
|
||||
style.sheet.insertRule(
|
||||
style.sheet!.insertRule(
|
||||
`.item-${CSS.escape(name)} {
|
||||
background-image: url("${staticRoot}static/${exportManifest.assets["item-spritesheet"]}");
|
||||
background-repeat: no-repeat;
|
||||
background-position: -${meta.x}px -${meta.y}px;
|
||||
height: ${meta.size}px;
|
||||
width: ${meta.size}px;
|
||||
}`
|
||||
background-image: url("${window.staticRoot}static/${exportManifest.assets["item-spritesheet"]}");
|
||||
background-repeat: no-repeat;
|
||||
background-position: -${meta.x}px -${meta.y}px;
|
||||
height: ${meta.size}px;
|
||||
width: ${meta.size}px;
|
||||
}`
|
||||
);
|
||||
}
|
||||
setItemMetadata(itemMetadataCache);
|
||||
@@ -46,4 +50,3 @@ export function useItemMetadata() {
|
||||
|
||||
return itemMetadata;
|
||||
}
|
||||
|
||||
@@ -3,16 +3,16 @@ import React, { useEffect, useState } from "react";
|
||||
import { useExportManifest } from "./export_manifest";
|
||||
|
||||
|
||||
let localeCache = null;
|
||||
export function useLocale() {
|
||||
let localeCache: Map<string, string>|null = null;
|
||||
export function useLocale(): Map<string, string> {
|
||||
let exportManifest = useExportManifest();
|
||||
let [locale, setLocale] = useState(localeCache || new Map());
|
||||
let [locale, setLocale] = useState<Map<string, string>>(localeCache || new Map());
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
if (!exportManifest || !exportManifest.assets || !exportManifest.assets["locale"]) {
|
||||
if (!exportManifest?.assets?.["locale"]) {
|
||||
return;
|
||||
}
|
||||
let response = await fetch(`${staticRoot}static/${exportManifest.assets["locale"]}`);
|
||||
let response = await fetch(`${window.staticRoot}static/${exportManifest.assets["locale"]}`);
|
||||
if (response.ok) {
|
||||
let data = await response.json();
|
||||
localeCache = new Map(data);
|
||||
@@ -6,9 +6,16 @@ import ControlContext from "../components/ControlContext";
|
||||
|
||||
const { logger } = lib;
|
||||
|
||||
export function useModPack(id) {
|
||||
|
||||
export type StateLoadStatus = {
|
||||
loading?: boolean;
|
||||
present?: boolean;
|
||||
missing?: boolean;
|
||||
};
|
||||
|
||||
export function useModPack(id: number) {
|
||||
let control = useContext(ControlContext);
|
||||
let [modPack, setModPack] = useState({ loading: true });
|
||||
let [modPack, setModPack] = useState<lib.ModPack|StateLoadStatus>({ loading: true });
|
||||
|
||||
function updateModPack() {
|
||||
control.send(new lib.ModPackGetRequest(id)).then(updatedModPack => {
|
||||
@@ -37,7 +44,7 @@ export function useModPack(id) {
|
||||
|
||||
export function useModPackList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [modPackList, setModPackList] = useState([]);
|
||||
let [modPackList, setModPackList] = useState<lib.ModPack[]>([]);
|
||||
|
||||
function updateModPackList() {
|
||||
control.send(new lib.ModPackListRequest()).then(modPacks => {
|
||||
@@ -50,7 +57,7 @@ export function useModPackList() {
|
||||
useEffect(() => {
|
||||
updateModPackList();
|
||||
|
||||
function updateHandler(newModPack) {
|
||||
function updateHandler(newModPack: lib.ModPack) {
|
||||
setModPackList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
let index = newList.findIndex(u => u.id === newModPack.id);
|
||||
@@ -8,7 +8,7 @@ const { logger } = lib;
|
||||
|
||||
export function useModList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [modList, setModList] = useState([]);
|
||||
let [modList, setModList] = useState<lib.ModInfo[]>([]);
|
||||
|
||||
function updateModList() {
|
||||
control.send(new lib.ModListRequest()).then(mods => {
|
||||
@@ -21,7 +21,7 @@ export function useModList() {
|
||||
useEffect(() => {
|
||||
updateModList();
|
||||
|
||||
function updateHandler(newMod) {
|
||||
function updateHandler(newMod: lib.ModInfo) {
|
||||
setModList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
let index = newList.findIndex(mod => mod.name === newMod.name && mod.version === newMod.version);
|
||||
@@ -5,12 +5,12 @@ import * as lib from "@clusterio/lib";
|
||||
|
||||
const { logger } = lib;
|
||||
|
||||
export function useSaves(instanceId) {
|
||||
export function useSaves(instanceId?: number): lib.SaveDetails[] {
|
||||
let control = useContext(ControlContext);
|
||||
let [saves, setSaves] = useState([]);
|
||||
let [saves, setSaves] = useState<lib.SaveDetails[]>([]);
|
||||
|
||||
function updateSaves() {
|
||||
control.sendTo({ instanceId }, new lib.InstanceListSavesRequest()).then(updatedSaves => {
|
||||
control.sendTo({ instanceId:instanceId! }, new lib.InstanceListSavesRequest()).then(updatedSaves => {
|
||||
setSaves(updatedSaves);
|
||||
}).catch(err => {
|
||||
logger.error(`Failed to list instance saves: ${err}`);
|
||||
@@ -25,13 +25,13 @@ export function useSaves(instanceId) {
|
||||
}
|
||||
updateSaves();
|
||||
|
||||
function updateHandler(data) {
|
||||
function updateHandler(data: lib.InstanceSaveListUpdateEvent) {
|
||||
setSaves(data.saves);
|
||||
}
|
||||
|
||||
control.onSaveListUpdate(instanceId, updateHandler);
|
||||
control.onSaveListUpdate(instanceId!, updateHandler);
|
||||
return () => {
|
||||
control.offSaveListUpdate(instanceId, updateHandler);
|
||||
control.offSaveListUpdate(instanceId!, updateHandler);
|
||||
};
|
||||
}, [instanceId]);
|
||||
|
||||
@@ -6,7 +6,7 @@ import * as lib from "@clusterio/lib";
|
||||
|
||||
const { logger } = lib;
|
||||
|
||||
function calculateLastSeen(user, instanceId) {
|
||||
function calculateLastSeen(user: RawUserState, instanceId?: number) {
|
||||
let stats;
|
||||
if (instanceId === undefined) {
|
||||
stats = user.playerStats;
|
||||
@@ -25,8 +25,8 @@ function calculateLastSeen(user, instanceId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function formatLastSeen(user, instanceId = undefined) {
|
||||
if (user.instances.some(id => instanceId === undefined || id === instanceId)) {
|
||||
export function formatLastSeen(user: RawUserState, instanceId?: number) {
|
||||
if (user.instances && user.instances.some(id => instanceId === undefined || id === instanceId)) {
|
||||
return <Tag color="green">Online</Tag>;
|
||||
}
|
||||
let lastSeen = calculateLastSeen(user, instanceId);
|
||||
@@ -36,15 +36,15 @@ export function formatLastSeen(user, instanceId = undefined) {
|
||||
return new Date(lastSeen).toLocaleString();
|
||||
}
|
||||
|
||||
export function sortLastSeen(userA, userB, instanceIdA = undefined, instanceIdB = undefined) {
|
||||
function epoch(user, instanceId) {
|
||||
return user.instances.some(id => instanceId === undefined || id === instanceId);
|
||||
export function sortLastSeen(userA:RawUserState, userB:RawUserState, instanceIdA?: number, instanceIdB?: number) {
|
||||
function epoch(user:RawUserState, instanceId?: number) {
|
||||
return user.instances && user.instances.some(id => instanceId === undefined || id === instanceId);
|
||||
}
|
||||
|
||||
let epochA = epoch(userA, instanceIdA);
|
||||
let epochB = epoch(userB, instanceIdB);
|
||||
if (epochA !== epochB) {
|
||||
return epochA - epochB;
|
||||
return Number(epochA) - Number(epochB);
|
||||
}
|
||||
|
||||
let lastSeenA = calculateLastSeen(userA, instanceIdA) || 0;
|
||||
@@ -52,9 +52,15 @@ export function sortLastSeen(userA, userB, instanceIdA = undefined, instanceIdB
|
||||
return lastSeenA - lastSeenB;
|
||||
}
|
||||
|
||||
export function useUser(name) {
|
||||
export type RawUserState = Partial<lib.RawUser> & {
|
||||
loading?: Boolean;
|
||||
present?: Boolean;
|
||||
missing?: Boolean;
|
||||
};
|
||||
|
||||
export function useUser(name: string): [RawUserState, () => void] {
|
||||
let control = useContext(ControlContext);
|
||||
let [user, setUser] = useState({ loading: true });
|
||||
let [user, setUser] = useState<RawUserState>({ loading: true });
|
||||
|
||||
function updateUser() {
|
||||
control.send(new lib.UserGetRequest(name)).then(updatedUser => {
|
||||
@@ -63,7 +69,6 @@ export function useUser(name) {
|
||||
logger.error(`Failed to get user: ${err}`);
|
||||
setUser({ missing: true });
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
@@ -73,7 +78,7 @@ export function useUser(name) {
|
||||
}
|
||||
updateUser();
|
||||
|
||||
function updateHandler(newUser) {
|
||||
function updateHandler(newUser: lib.RawUser) {
|
||||
setUser({ ...newUser, present: true });
|
||||
}
|
||||
|
||||
@@ -88,7 +93,7 @@ export function useUser(name) {
|
||||
|
||||
export function useUserList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [userList, setUserList] = useState([]);
|
||||
let [userList, setUserList] = useState<lib.RawUser[]>([]);
|
||||
|
||||
function updateUserList() {
|
||||
control.send(new lib.UserListRequest()).then(users => {
|
||||
@@ -101,7 +106,7 @@ export function useUserList() {
|
||||
useEffect(() => {
|
||||
updateUserList();
|
||||
|
||||
function updateHandler(newUser) {
|
||||
function updateHandler(newUser: lib.RawUser) {
|
||||
setUserList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
let index = newList.findIndex(u => u.name === newUser.name);
|
||||
Reference in new issue
Block a user