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

75 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-07-11 06:09:37 +00:00
import { action, Action } from 'easy-peasy';
import { cleanDirectoryPath } from '@/helpers';
export interface FileUploadData {
loaded: number;
readonly abort: AbortController;
readonly total: number;
}
export interface ServerFileStore {
directory: string;
selectedFiles: string[];
uploads: Record<string, FileUploadData>;
setDirectory: Action<ServerFileStore, string>;
setSelectedFiles: Action<ServerFileStore, string[]>;
appendSelectedFile: Action<ServerFileStore, string>;
removeSelectedFile: Action<ServerFileStore, string>;
pushFileUpload: Action<ServerFileStore, { name: string; data: FileUploadData }>;
setUploadProgress: Action<ServerFileStore, { name: string; loaded: number }>;
2022-07-24 22:50:47 +00:00
clearFileUploads: Action<ServerFileStore>;
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) => {
Object.values(state.uploads).forEach((upload) => upload.abort.abort());
state.uploads = {};
}),
pushFileUpload: action((state, payload) => {
state.uploads[payload.name] = payload.data;
2022-07-24 22:50:47 +00:00
}),
setUploadProgress: action((state, { name, loaded }) => {
if (state.uploads[name]) {
state.uploads[name].loaded = loaded;
2022-07-24 22:50:47 +00:00
}
}),
removeFileUpload: action((state, payload) => {
if (state.uploads[payload]) {
// Abort the request if it is still in flight. If it already completed this is
// a no-op.
state.uploads[payload].abort.abort();
delete state.uploads[payload];
}
}),
};
export default files;