Files

94 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2022-11-25 13:25:03 -07:00
import type { Action } from 'easy-peasy';
import { action } from 'easy-peasy';
import { cleanDirectoryPath } from '@/helpers';
2019-08-04 14:58:31 -07:00
2022-11-25 13:25:03 -07:00
interface FileUploadData {
2022-07-24 23:18:32 +02:00
loaded: number;
2022-11-21 12:58:55 -08:00
readonly abort: AbortController;
2022-07-24 23:18:32 +02:00
readonly total: number;
}
2022-11-25 13:25:03 -07:00
interface ServerFileStore {
2019-08-04 14:58:31 -07:00
directory: string;
selectedFiles: string[];
2022-11-21 12:58:55 -08:00
uploads: Record<string, FileUploadData>;
2019-08-04 14:58:31 -07:00
setDirectory: Action<ServerFileStore, string>;
setSelectedFiles: Action<ServerFileStore, string[]>;
appendSelectedFile: Action<ServerFileStore, string>;
removeSelectedFile: Action<ServerFileStore, string>;
2022-07-24 23:18:32 +02:00
2022-11-21 12:58:55 -08:00
pushFileUpload: Action<ServerFileStore, { name: string; data: FileUploadData }>;
setUploadProgress: Action<ServerFileStore, { name: string; loaded: number }>;
2022-07-24 18:50:47 -04:00
clearFileUploads: Action<ServerFileStore>;
2022-07-24 23:18:32 +02:00
removeFileUpload: Action<ServerFileStore, string>;
cancelFileUpload: Action<ServerFileStore, string>;
2019-08-04 14:58:31 -07:00
}
const files: ServerFileStore = {
directory: '/',
selectedFiles: [],
2022-11-21 12:58:55 -08:00
uploads: {},
2019-08-04 14:58:31 -07:00
setDirectory: action((state, payload) => {
state.directory = cleanDirectoryPath(payload);
2019-08-04 14:58:31 -07:00
}),
setSelectedFiles: action((state, payload) => {
state.selectedFiles = payload;
}),
appendSelectedFile: action((state, payload) => {
2022-11-25 13:25:03 -07:00
state.selectedFiles = state.selectedFiles.filter(f => f !== payload).concat(payload);
}),
removeSelectedFile: action((state, payload) => {
2022-11-25 13:25:03 -07:00
state.selectedFiles = state.selectedFiles.filter(f => f !== payload);
}),
2022-07-24 23:18:32 +02:00
2022-11-25 13:25:03 -07:00
clearFileUploads: action(state => {
Object.values(state.uploads).forEach(upload => upload.abort.abort());
2022-11-21 12:58:55 -08:00
state.uploads = {};
}),
pushFileUpload: action((state, payload) => {
state.uploads[payload.name] = payload.data;
2022-07-24 18:50:47 -04:00
}),
2022-11-21 12:58:55 -08:00
setUploadProgress: action((state, { name, loaded }) => {
2022-11-25 13:25:03 -07:00
const upload = state.uploads[name];
if (upload === undefined) {
return;
2022-07-24 18:50:47 -04:00
}
2022-11-25 13:25:03 -07:00
upload.loaded = loaded;
2022-07-24 23:18:32 +02:00
}),
removeFileUpload: action((state, payload) => {
2022-11-25 13:25:03 -07:00
const upload = state.uploads[payload];
if (upload === undefined) {
return;
2022-11-21 12:58:55 -08:00
}
2022-11-25 13:25:03 -07:00
2022-12-12 14:06:52 -07:00
delete state.uploads[payload];
}),
cancelFileUpload: action((state, payload) => {
2022-12-12 14:06:52 -07:00
const upload = state.uploads[payload];
if (upload === undefined) {
return;
2022-11-21 12:58:55 -08:00
}
2022-12-12 14:06:52 -07:00
2022-11-25 13:25:03 -07:00
// Abort the request if it is still in flight. If it already completed this is
// a no-op.
upload.abort.abort();
delete state.uploads[payload];
2022-07-24 23:18:32 +02:00
}),
2019-08-04 14:58:31 -07:00
};
2022-11-25 13:25:03 -07:00
export type { FileUploadData, ServerFileStore };
2019-08-04 14:58:31 -07:00
export default files;