misc_pterodactyl-panel/resources/scripts/state/server/index.ts

116 lines
3.3 KiB
TypeScript
Raw Normal View History

import getServer, { Server } from '@/api/server/getServer';
2021-01-31 02:01:32 +00:00
import { action, Action, computed, Computed, createContextStore, thunk, Thunk } from 'easy-peasy';
import socket, { SocketStore } from './socket';
import files, { ServerFileStore } from '@/state/server/files';
2019-11-03 20:20:11 +00:00
import subusers, { ServerSubuserStore } from '@/state/server/subusers';
2019-12-22 08:16:13 +00:00
import { composeWithDevTools } from 'redux-devtools-extension';
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
2020-04-10 17:56:25 +00:00
import databases, { ServerDatabaseStore } from '@/state/server/databases';
import isEqual from 'react-fast-compare';
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running' | null;
interface ServerDataStore {
data?: Server;
2021-01-31 02:01:32 +00:00
inConflictState: Computed<ServerDataStore, boolean>;
permissions: string[];
2020-07-05 01:30:50 +00:00
getServer: Thunk<ServerDataStore, string, Record<string, unknown>, ServerStore, Promise<void>>;
setServer: Action<ServerDataStore, Server>;
setServerFromState: Action<ServerDataStore, (s: Server) => Server>;
setPermissions: Action<ServerDataStore, string[]>;
}
const server: ServerDataStore = {
permissions: [],
2021-01-31 02:01:32 +00:00
inConflictState: computed(state => {
if (!state.data) {
return false;
}
return state.data.status !== null || state.data.isTransferring;
}),
getServer: thunk(async (actions, payload) => {
2020-04-10 17:56:25 +00:00
const [ server, permissions ] = await getServer(payload);
actions.setServer(server);
actions.setPermissions(permissions);
}),
setServer: action((state, payload) => {
if (!isEqual(payload, state.data)) {
state.data = payload;
}
}),
setServerFromState: action((state, payload) => {
const output = payload(state.data!);
if (!isEqual(output, state.data)) {
state.data = output;
}
}),
setPermissions: action((state, payload) => {
if (!isEqual(payload, state.permissions)) {
state.permissions = payload;
}
}),
};
interface ServerStatusStore {
value: ServerStatus;
setServerStatus: Action<ServerStatusStore, ServerStatus>;
}
const status: ServerStatusStore = {
value: null,
setServerStatus: action((state, payload) => {
state.value = payload;
}),
};
export interface ServerStore {
server: ServerDataStore;
2019-11-03 20:20:11 +00:00
subusers: ServerSubuserStore;
2019-07-27 22:17:50 +00:00
databases: ServerDatabaseStore;
files: ServerFileStore;
schedules: ServerScheduleStore;
socket: SocketStore;
status: ServerStatusStore;
clearServerState: Action<ServerStore>;
}
export const ServerContext = createContextStore<ServerStore>({
server,
socket,
status,
2019-07-27 22:17:50 +00:00
databases,
files,
2019-11-03 20:20:11 +00:00
subusers,
schedules,
clearServerState: action(state => {
state.server.data = undefined;
state.server.permissions = [];
2020-04-10 17:56:25 +00:00
state.databases.data = [];
2019-11-03 20:20:11 +00:00
state.subusers.data = [];
state.files.directory = '/';
state.files.selectedFiles = [];
state.schedules.data = [];
if (state.socket.instance) {
state.socket.instance.removeAllListeners();
state.socket.instance.close();
}
state.socket.instance = null;
state.socket.connected = false;
}),
2019-12-22 08:16:13 +00:00
}, {
compose: composeWithDevTools({
name: 'ServerStore',
trace: true,
}),
});