15610: Uses a virtualized list to show the collection's file tree. (WIP)
[arvados-workbench2.git] / src / store / collection-panel / collection-panel-files / collection-panel-files-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "~/common/unionize";
6 import { Dispatch } from "redux";
7 import { CollectionFilesTree, CollectionFileType, createCollectionFilesTree } from "~/models/collection-file";
8 import { ServiceRepository } from "~/services/services";
9 import { RootState } from "../../store";
10 import { snackbarActions, SnackbarKind } from "../../snackbar/snackbar-actions";
11 import { dialogActions } from '../../dialog/dialog-actions';
12 import { getNodeValue, mapTreeValues } from "~/models/tree";
13 import { filterCollectionFilesBySelection } from './collection-panel-files-state';
14 import { startSubmit, stopSubmit, initialize, FormErrors } from 'redux-form';
15 import { getDialog } from "~/store/dialog/dialog-reducer";
16 import { getFileFullPath, sortFilesTree } from "~/services/collection-service/collection-service-files-response";
17 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
18
19 export const collectionPanelFilesAction = unionize({
20     SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
21     TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
22     TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
23     SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
24     UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
25 });
26
27 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
28
29 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
30 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
31
32 export const loadCollectionFiles = (uuid: string) =>
33     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34         let step = Date.now();
35         dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
36         const files = await services.collectionService.files(uuid);
37         console.log('Get files: ', (Date.now()-step)/1000);
38
39         // Given the array of directories and files, create the appropriate tree nodes,
40         // sort them, and add the complete url to each.
41         step = Date.now();
42         const tree = createCollectionFilesTree(files);
43         console.log('Create tree: ', (Date.now()-step)/1000);
44         step = Date.now();
45         const sorted = sortFilesTree(tree);
46         console.log('Sort tree: ', (Date.now()-step)/1000);
47         step = Date.now();
48         const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
49         console.log('Add URL: ', (Date.now()-step)/1000);
50         step = Date.now();
51         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
52         console.log('Dispatch: ', (Date.now()-step)/1000);
53         dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
54     };
55
56 export const removeCollectionFiles = (filePaths: string[]) =>
57     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58         const currentCollection = getState().collectionPanel.item;
59         if (currentCollection) {
60             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
61             try {
62                 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
63                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
64                 dispatch(snackbarActions.OPEN_SNACKBAR({
65                     message: 'Removed.',
66                     hideDuration: 2000,
67                     kind: SnackbarKind.SUCCESS
68                 }));
69             } catch (e) {
70                 dispatch(snackbarActions.OPEN_SNACKBAR({
71                     message: 'Could not remove file.',
72                     hideDuration: 2000,
73                     kind: SnackbarKind.ERROR
74                 }));
75             }
76         }
77     };
78
79 export const removeCollectionsSelectedFiles = () =>
80     (dispatch: Dispatch, getState: () => RootState) => {
81         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
82             .map(getFileFullPath);
83         dispatch<any>(removeCollectionFiles(paths));
84     };
85
86 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
87
88 export const openFileRemoveDialog = (filePath: string) =>
89     (dispatch: Dispatch, getState: () => RootState) => {
90         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
91         if (file) {
92             const isDirectory = file.type === CollectionFileType.DIRECTORY;
93             const title = isDirectory
94                 ? 'Removing directory'
95                 : 'Removing file';
96             const text = isDirectory
97                 ? 'Are you sure you want to remove this directory?'
98                 : 'Are you sure you want to remove this file?';
99             const info = isDirectory
100                 ? 'Removing files will change content address.'
101                 : 'Removing a file will change content address.';
102
103             dispatch(dialogActions.OPEN_DIALOG({
104                 id: FILE_REMOVE_DIALOG,
105                 data: {
106                     title,
107                     text,
108                     info,
109                     confirmButtonLabel: 'Remove',
110                     filePath
111                 }
112             }));
113         }
114     };
115
116 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
117
118 export const openMultipleFilesRemoveDialog = () =>
119     dialogActions.OPEN_DIALOG({
120         id: MULTIPLE_FILES_REMOVE_DIALOG,
121         data: {
122             title: 'Removing files',
123             text: 'Are you sure you want to remove selected files?',
124             info: 'Removing files will change content address.',
125             confirmButtonLabel: 'Remove'
126         }
127     });
128
129 export const RENAME_FILE_DIALOG = 'renameFileDialog';
130 export interface RenameFileDialogData {
131     name: string;
132     id: string;
133 }
134
135 export const openRenameFileDialog = (data: RenameFileDialogData) =>
136     (dispatch: Dispatch) => {
137         dispatch(initialize(RENAME_FILE_DIALOG, data));
138         dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
139     };
140
141 export const renameFile = (newName: string) =>
142     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
143         const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
144         const currentCollection = getState().collectionPanel.item;
145         if (dialog && currentCollection) {
146             const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
147             if (file) {
148                 dispatch(startSubmit(RENAME_FILE_DIALOG));
149                 const oldPath = getFileFullPath(file);
150                 const newPath = getFileFullPath({ ...file, name: newName });
151                 try {
152                     await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
153                     dispatch<any>(loadCollectionFiles(currentCollection.uuid));
154                     dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
155                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
156                 } catch (e) {
157                     const errors: FormErrors<RenameFileDialogData, string> = {
158                         name: 'Could not rename the file'
159                     };
160                     dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));
161                 }
162             }
163         }
164     };