[FEATURE] Basic System with file manager (#4)
Co-authored-by: dunemask <dunemask@gmail.com> Co-authored-by: Dunemask <dunemask@gmail.com> Reviewed-on: https://gitea.dunemask.dev/elysium/minecluster/pulls/4
This commit is contained in:
parent
8fb5b34c77
commit
4f19cf19d9
62 changed files with 5910 additions and 1190 deletions
|
@ -2,6 +2,15 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|||
const fetchApi = (subPath) => async () =>
|
||||
fetch(`/api${subPath}`).then((res) => res.json());
|
||||
|
||||
const fetchApiCore = async (subPath, json, method = "POST", jsonify = false) =>
|
||||
fetch(`/api${subPath}`, {
|
||||
method,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(json),
|
||||
}).then((res) => (jsonify ? res.json() : res));
|
||||
|
||||
const fetchApiPost = (subPath, json) => async () =>
|
||||
fetch(`/api${subPath}`, {
|
||||
method: "POST",
|
||||
|
@ -12,16 +21,16 @@ const fetchApiPost = (subPath, json) => async () =>
|
|||
}).then((res) => res.json());
|
||||
|
||||
export const useServerStatus = (server) =>
|
||||
useQuery(
|
||||
[`server-status-${server}`],
|
||||
fetchApiPost("/server/status", { name: server })
|
||||
);
|
||||
useQuery({
|
||||
queryKey: [`server-status-${server}`],
|
||||
queryFn: fetchApiPost("/server/status", { name: server }),
|
||||
});
|
||||
export const useServerMetrics = (server) =>
|
||||
useQuery(
|
||||
[`server-metrics-${server}`],
|
||||
fetchApiPost("/server/metrics", { name: server }),
|
||||
{ refetchInterval: 10000 }
|
||||
);
|
||||
useQuery({
|
||||
queryKey: [`server-metrics-${server}`],
|
||||
queryFn: fetchApiPost("/server/metrics", { name: server }),
|
||||
refetchInterval: 10000,
|
||||
});
|
||||
export const useStartServer = (server) =>
|
||||
postJsonApi("/server/start", { name: server }, "server-instances");
|
||||
export const useStopServer = (server) =>
|
||||
|
@ -30,20 +39,61 @@ export const useDeleteServer = (server) =>
|
|||
postJsonApi("/server/delete", { name: server }, "server-instances", "DELETE");
|
||||
export const useCreateServer = (spec) =>
|
||||
postJsonApi("/server/create", spec, "server-list");
|
||||
|
||||
export const getServerFiles = async (server, path) =>
|
||||
fetchApiCore("/files/list", { name: server, path }, "POST", true);
|
||||
export const createServerFolder = async (server, path) =>
|
||||
fetchApiCore("/files/folder", {
|
||||
name: server,
|
||||
path,
|
||||
}); /*postJsonApi("/files/folder", {name: server, path});*/
|
||||
export const deleteServerItem = async (server, path, isDir) =>
|
||||
fetchApiCore("/files/item", { name: server, path, isDir }, "DELETE");
|
||||
|
||||
export const getServerItem = async (server, name, path) =>
|
||||
fetchApiCore("/files/item", { name: server, path })
|
||||
.then((resp) =>
|
||||
resp.status === 200
|
||||
? resp.blob()
|
||||
: Promise.reject("something went wrong"),
|
||||
)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.style.display = "none";
|
||||
a.href = url;
|
||||
a.download = name;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
});
|
||||
|
||||
export const useInvalidator = () => {
|
||||
const qc = useQueryClient();
|
||||
return (q) => qc.invalidateQueries([q]);
|
||||
};
|
||||
|
||||
export const useServerList = () =>
|
||||
useQuery(["server-list"], fetchApi("/server/list"));
|
||||
useQuery({ queryKey: ["server-list"], queryFn: fetchApi("/server/list") });
|
||||
export const useServerInstances = () =>
|
||||
useQuery(["server-instances"], fetchApi("/server/instances"), {
|
||||
useQuery({
|
||||
queryKey: ["server-instances"],
|
||||
queryFn: fetchApi("/server/instances"),
|
||||
refetchInterval: 5000,
|
||||
});
|
||||
export const useSystemAvailable = () =>
|
||||
useQuery(["system-available"], fetchApi("/system/available"));
|
||||
useQuery({
|
||||
queryKey: ["system-available"],
|
||||
queryFn: fetchApi("/system/available"),
|
||||
});
|
||||
export const useVersionList = () =>
|
||||
useQuery(["minecraft-versions"], () =>
|
||||
fetch("https://piston-meta.mojang.com/mc/game/version_manifest.json").then(
|
||||
(r) => r.json()
|
||||
)
|
||||
);
|
||||
useQuery({
|
||||
queryKey: ["minecraft-versions"],
|
||||
queryFn: () =>
|
||||
fetch(
|
||||
"https://piston-meta.mojang.com/mc/game/version_manifest.json",
|
||||
).then((r) => r.json()),
|
||||
});
|
||||
|
||||
const postJsonApi = (subPath, body, invalidate, method = "POST") => {
|
||||
const qc = useQueryClient();
|
||||
|
@ -55,6 +105,7 @@ const postJsonApi = (subPath, body, invalidate, method = "POST") => {
|
|||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
qc.invalidateQueries([invalidate]);
|
||||
if (invalidate) qc.invalidateQueries([invalidate]);
|
||||
return res.json();
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue