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

64 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2020-07-11 06:09:37 +00:00
import { action, Action } from 'easy-peasy';
import { cleanDirectoryPath } from '@/helpers';
export interface FileUpload {
name: string;
loaded: number;
readonly total: number;
}
export interface ServerFileStore {
directory: string;
selectedFiles: string[];
uploads: FileUpload[];
setDirectory: Action<ServerFileStore, string>;
setSelectedFiles: Action<ServerFileStore, string[]>;
appendSelectedFile: Action<ServerFileStore, string>;
removeSelectedFile: Action<ServerFileStore, string>;
2022-07-24 22:50:47 +00:00
clearFileUploads: Action<ServerFileStore>;
appendFileUpload: Action<ServerFileStore, FileUpload>;
removeFileUpload: Action<ServerFileStore, string>;
}
const files: ServerFileStore = {
directory: '/',
selectedFiles: [],
uploads: [],
setDirectory: action((state, payload) => {
state.directory = cleanDirectoryPath(payload);
}),
setSelectedFiles: action((state, payload) => {
state.selectedFiles = payload;
}),
appendSelectedFile: action((state, payload) => {
state.selectedFiles = state.selectedFiles.filter((f) => f !== payload).concat(payload);
}),
removeSelectedFile: action((state, payload) => {
state.selectedFiles = state.selectedFiles.filter((f) => f !== payload);
}),
2022-07-24 22:50:47 +00:00
clearFileUploads: action((state) => {
state.uploads = [];
}),
appendFileUpload: action((state, payload) => {
2022-07-24 22:50:47 +00:00
if (!state.uploads.some(({ name }) => name === payload.name)) {
state.uploads = [...state.uploads, payload];
} else {
state.uploads = state.uploads.map((file) => (file.name === payload.name ? payload : file));
}
}),
removeFileUpload: action((state, payload) => {
2022-07-24 22:50:47 +00:00
state.uploads = state.uploads.filter(({ name }) => name !== payload);
}),
};
export default files;