forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.ts
More file actions
63 lines (50 loc) · 1.82 KB
/
files.ts
File metadata and controls
63 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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>;
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);
}),
clearFileUploads: action((state) => {
state.uploads = [];
}),
appendFileUpload: action((state, payload) => {
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) => {
state.uploads = state.uploads.filter(({ name }) => name !== payload);
}),
};
export default files;