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

55 lines
1.7 KiB
TypeScript
Raw Normal View History

import loadDirectory, { FileObject } from '@/api/server/files/loadDirectory';
import { action, Action, thunk, Thunk } from 'easy-peasy';
import { ServerStore } from '@/state/server/index';
export interface ServerFileStore {
directory: string;
contents: FileObject[];
getDirectoryContents: Thunk<ServerFileStore, string, {}, ServerStore, Promise<void>>;
setContents: Action<ServerFileStore, FileObject[]>;
pushFile: Action<ServerFileStore, FileObject>;
removeFile: Action<ServerFileStore, string>;
setDirectory: Action<ServerFileStore, string>;
}
const files: ServerFileStore = {
directory: '/',
contents: [],
getDirectoryContents: thunk(async (actions, payload, { getStoreState }) => {
const server = getStoreState().server.data;
if (!server) {
return;
}
const contents = await loadDirectory(server.uuid, payload);
2019-11-03 18:46:43 +00:00
actions.setDirectory(payload.length === 0 ? '/' : payload);
actions.setContents(contents);
}),
setContents: action((state, payload) => {
state.contents = payload;
}),
pushFile: action((state, payload) => {
const matchIndex = state.contents.findIndex(file => file.uuid === payload.uuid);
if (matchIndex < 0) {
state.contents = state.contents.concat(payload);
return;
}
state.contents[matchIndex] = payload;
}),
removeFile: action((state, payload) => {
state.contents = state.contents.filter(file => file.uuid !== payload);
}),
setDirectory: action((state, payload) => {
2019-11-03 18:46:43 +00:00
state.directory = payload.length === 0 ? '/' : payload;
}),
};
export default files;