import getServer, { Server } from '@/api/server/getServer'; import { action, Action, createContextStore, thunk, Thunk } from 'easy-peasy'; import socket, { SocketStore } from './socket'; import { ServerDatabase } from '@/api/server/getServerDatabases'; import files, { ServerFileStore } from '@/state/server/files'; import subusers, { ServerSubuserStore } from '@/state/server/subusers'; export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running'; interface ServerDataStore { data?: Server; getServer: Thunk>; setServer: Action; } const server: ServerDataStore = { getServer: thunk(async (actions, payload) => { const server = await getServer(payload); actions.setServer(server); }), setServer: action((state, payload) => { state.data = payload; }), }; interface ServerStatusStore { value: ServerStatus; setServerStatus: Action; } const status: ServerStatusStore = { value: 'offline', setServerStatus: action((state, payload) => { state.value = payload; }), }; interface ServerDatabaseStore { items: ServerDatabase[]; setDatabases: Action; appendDatabase: Action; removeDatabase: Action; } const databases: ServerDatabaseStore = { items: [], setDatabases: action((state, payload) => { state.items = payload; }), appendDatabase: action((state, payload) => { state.items = state.items.filter(item => item.id !== payload.id).concat(payload); }), removeDatabase: action((state, payload) => { state.items = state.items.filter(item => item.id !== payload.id); }), }; export interface ServerStore { server: ServerDataStore; subusers: ServerSubuserStore; databases: ServerDatabaseStore; files: ServerFileStore; socket: SocketStore; status: ServerStatusStore; clearServerState: Action; } export const ServerContext = createContextStore({ server, socket, status, databases, files, subusers, clearServerState: action(state => { state.server.data = undefined; state.databases.items = []; state.subusers.data = []; state.files.directory = '/'; state.files.contents = []; if (state.socket.instance) { state.socket.instance.removeAllListeners(); state.socket.instance.close(); } state.socket.instance = null; state.socket.connected = false; }), }, { name: 'ServerStore' });