2019-07-10 04:25:57 +00:00
|
|
|
import getServer, { Server } from '@/api/server/getServer';
|
|
|
|
import { action, Action, createContextStore, thunk, Thunk } from 'easy-peasy';
|
|
|
|
import socket, { SocketStore } from './socket';
|
2019-08-04 21:58:31 +00:00
|
|
|
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-10 17:46:00 +00:00
|
|
|
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
|
2020-04-10 17:56:25 +00:00
|
|
|
import databases, { ServerDatabaseStore } from '@/state/server/databases';
|
2020-08-26 04:25:31 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
2019-07-10 04:25:57 +00:00
|
|
|
|
2020-09-12 06:18:01 +00:00
|
|
|
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running' | null;
|
2019-07-10 04:25:57 +00:00
|
|
|
|
|
|
|
interface ServerDataStore {
|
|
|
|
data?: Server;
|
2020-03-29 00:25:04 +00:00
|
|
|
permissions: string[];
|
|
|
|
|
2020-07-05 01:30:50 +00:00
|
|
|
getServer: Thunk<ServerDataStore, string, Record<string, unknown>, ServerStore, Promise<void>>;
|
2019-07-10 04:25:57 +00:00
|
|
|
setServer: Action<ServerDataStore, Server>;
|
2020-08-26 04:25:31 +00:00
|
|
|
setServerFromState: Action<ServerDataStore, (s: Server) => Server>;
|
2020-03-29 00:25:04 +00:00
|
|
|
setPermissions: Action<ServerDataStore, string[]>;
|
2019-07-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const server: ServerDataStore = {
|
2020-03-29 00:25:04 +00:00
|
|
|
permissions: [],
|
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
getServer: thunk(async (actions, payload) => {
|
2020-04-10 17:56:25 +00:00
|
|
|
const [ server, permissions ] = await getServer(payload);
|
2020-03-29 00:25:04 +00:00
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
actions.setServer(server);
|
2020-03-29 00:25:04 +00:00
|
|
|
actions.setPermissions(permissions);
|
2019-07-10 04:25:57 +00:00
|
|
|
}),
|
2020-03-29 00:25:04 +00:00
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
setServer: action((state, payload) => {
|
2020-08-26 04:25:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-07-10 04:25:57 +00:00
|
|
|
}),
|
2020-03-29 00:25:04 +00:00
|
|
|
|
|
|
|
setPermissions: action((state, payload) => {
|
2020-08-26 04:25:31 +00:00
|
|
|
if (!isEqual(payload, state.permissions)) {
|
|
|
|
state.permissions = payload;
|
|
|
|
}
|
2020-03-29 00:25:04 +00:00
|
|
|
}),
|
2019-07-10 04:25:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface ServerStatusStore {
|
|
|
|
value: ServerStatus;
|
|
|
|
setServerStatus: Action<ServerStatusStore, ServerStatus>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const status: ServerStatusStore = {
|
2020-09-12 06:18:01 +00:00
|
|
|
value: null,
|
2019-07-10 04:25:57 +00:00
|
|
|
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;
|
2019-08-04 21:58:31 +00:00
|
|
|
files: ServerFileStore;
|
2020-04-10 17:46:00 +00:00
|
|
|
schedules: ServerScheduleStore;
|
2019-07-10 04:25:57 +00:00
|
|
|
socket: SocketStore;
|
|
|
|
status: ServerStatusStore;
|
|
|
|
clearServerState: Action<ServerStore>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ServerContext = createContextStore<ServerStore>({
|
|
|
|
server,
|
|
|
|
socket,
|
|
|
|
status,
|
2019-07-27 22:17:50 +00:00
|
|
|
databases,
|
2019-08-04 21:58:31 +00:00
|
|
|
files,
|
2019-11-03 20:20:11 +00:00
|
|
|
subusers,
|
2020-04-10 17:46:00 +00:00
|
|
|
schedules,
|
2019-07-10 04:25:57 +00:00
|
|
|
clearServerState: action(state => {
|
|
|
|
state.server.data = undefined;
|
2020-03-29 00:25:04 +00:00
|
|
|
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 = '/';
|
2020-07-11 23:47:13 +00:00
|
|
|
state.files.selectedFiles = [];
|
2020-04-10 17:46:00 +00:00
|
|
|
state.schedules.data = [];
|
2019-07-10 04:25:57 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}),
|
|
|
|
});
|