forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
123 lines (105 loc) · 3.66 KB
/
index.ts
File metadata and controls
123 lines (105 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import getServer, { Server } from '@/api/server/getServer';
import { action, Action, computed, Computed, createContextStore, thunk, Thunk } from 'easy-peasy';
import socket, { SocketStore } from './socket';
import files, { ServerFileStore } from '@/state/server/files';
import subusers, { ServerSubuserStore } from '@/state/server/subusers';
import { composeWithDevTools } from 'redux-devtools-extension';
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
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;
inConflictState: Computed<ServerDataStore, boolean>;
isInstalling: Computed<ServerDataStore, boolean>;
permissions: string[];
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: [],
inConflictState: computed((state) => {
if (!state.data) {
return false;
}
return state.data.status !== null || state.data.isTransferring || state.data.isNodeUnderMaintenance;
}),
isInstalling: computed((state) => {
return state.data?.status === 'installing' || state.data?.status === 'install_failed';
}),
getServer: thunk(async (actions, payload) => {
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;
subusers: ServerSubuserStore;
databases: ServerDatabaseStore;
files: ServerFileStore;
schedules: ServerScheduleStore;
socket: SocketStore;
status: ServerStatusStore;
clearServerState: Action<ServerStore>;
}
export const ServerContext = createContextStore<ServerStore>(
{
server,
socket,
status,
databases,
files,
subusers,
schedules,
clearServerState: action((state) => {
state.server.data = undefined;
state.server.permissions = [];
state.databases.data = [];
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;
}),
},
{
compose: composeWithDevTools({
name: 'ServerStore',
trace: true,
}),
}
);