122 lines
3.2 KiB
TypeScript
122 lines
3.2 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { mclAuthenticatedApiRequest, useMutator } from "../requests";
|
|
|
|
// ===== API Utils =====
|
|
|
|
const useInstanceControl = (apiRequest: (args: any) => Promise<any>) =>
|
|
useMutator(apiRequest, ["server-instances"]);
|
|
|
|
const useListControl = (apiRequest: (args: any) => Promise<any>) =>
|
|
useMutator(apiRequest, ["server-list"]);
|
|
|
|
// ===== API Requests =====
|
|
|
|
export const getServerList = () =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/path",
|
|
jsonify: true,
|
|
});
|
|
|
|
export const getServerInstances = () =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/instances",
|
|
jsonify: true,
|
|
});
|
|
|
|
export const getServerMetrics = (serverId: string) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/metrics",
|
|
json: { id: serverId },
|
|
jsonify: true,
|
|
});
|
|
|
|
export const getServerStatus = (serverId: string) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/status",
|
|
json: { id: serverId },
|
|
jsonify: true,
|
|
});
|
|
|
|
export const requestServerStart = (serverId: string) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/start",
|
|
json: { id: serverId },
|
|
});
|
|
|
|
export const requestServerStop = (serverId: string) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/stop",
|
|
json: { id: serverId },
|
|
});
|
|
|
|
export const requestServerDelete = (serverId: string) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/delete",
|
|
json: { id: serverId },
|
|
method: "DELETE",
|
|
});
|
|
|
|
export const requestServerCreate = (serverSpec: any) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/create",
|
|
json: serverSpec,
|
|
});
|
|
|
|
export const requestServerModify = (serverSpec: any) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/modify",
|
|
json: serverSpec,
|
|
});
|
|
|
|
export const getServerBlueprint = (serverId: string) =>
|
|
mclAuthenticatedApiRequest({
|
|
subPath: "/server/blueprint",
|
|
json: { id: serverId },
|
|
jsonify: true
|
|
})
|
|
|
|
// ===== API Queries =====
|
|
|
|
export const useServerList = () =>
|
|
useQuery({ queryKey: ["server-list"], queryFn: getServerList });
|
|
|
|
export const useServerInstances = () =>
|
|
useQuery({
|
|
queryKey: ["server-instances"],
|
|
queryFn: getServerInstances,
|
|
refetchInterval: 5000,
|
|
});
|
|
|
|
export const useServerMetrics = (serverId: string) =>
|
|
useQuery({
|
|
queryKey: [`server-metrics-${serverId}`],
|
|
queryFn: () => getServerMetrics(serverId),
|
|
refetchInterval: 10000,
|
|
});
|
|
|
|
export const useServerStatus = (serverId: string) =>
|
|
useQuery({
|
|
queryKey: [`server-status-${serverId}`],
|
|
queryFn: () => getServerStatus(serverId),
|
|
});
|
|
|
|
export const useServerStart = (serverId: string) =>
|
|
useInstanceControl(() => requestServerStart(serverId));
|
|
|
|
export const useServerStop = (serverId: string) =>
|
|
useInstanceControl(() => requestServerStop(serverId));
|
|
|
|
export const useServerDelete = (serverId: string) =>
|
|
useInstanceControl(() => requestServerDelete(serverId));
|
|
|
|
export const useServerCreate = (spec: any) =>
|
|
useListControl(() => requestServerCreate(spec));
|
|
|
|
export const useServerModify = (spec: any) =>
|
|
useListControl(() => requestServerModify(spec));
|
|
|
|
export const useBlueprint = (serverId: string) =>
|
|
useQuery({
|
|
queryKey: [`server-blueprint-${serverId}`],
|
|
queryFn: ()=>getServerBlueprint(serverId),
|
|
})
|