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

98 lines
2.8 KiB
TypeScript
Raw Normal View History

import getServer, { Server } from '@/api/server/getServer';
import { action, Action, 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';
2020-04-07 05:25:54 +00:00
import backups, { ServerBackupStore } from '@/state/server/backups';
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
2020-04-10 17:56:25 +00:00
import databases, { ServerDatabaseStore } from '@/state/server/databases';
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running';
interface ServerDataStore {
data?: Server;
permissions: string[];
getServer: Thunk<ServerDataStore, string, {}, ServerStore, Promise<void>>;
setServer: Action<ServerDataStore, Server>;
setPermissions: Action<ServerDataStore, string[]>;
}
const server: ServerDataStore = {
permissions: [],
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) => {
state.data = payload;
}),
setPermissions: action((state, payload) => {
state.permissions = payload;
}),
};
interface ServerStatusStore {
value: ServerStatus;
setServerStatus: Action<ServerStatusStore, ServerStatus>;
}
const status: ServerStatusStore = {
value: 'offline',
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;
2020-04-07 05:25:54 +00:00
backups: ServerBackupStore;
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,
2020-04-07 05:25:54 +00:00
backups,
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.contents = [];
state.backups.data = [];
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,
}),
});