21412: disabled breadcrumb focus listener Arvados-DCO-1.1-Signed-off-by: Lisa Knox...
[arvados.git] / services / workbench2 / src / store / workbench / workbench-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from "store/store";
7 import { getUserUuid } from "common/getuser";
8 import { loadDetailsPanel } from "store/details-panel/details-panel-action";
9 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
10 import { favoritePanelActions, loadFavoritePanel } from "store/favorite-panel/favorite-panel-action";
11 import { getProjectPanelCurrentUuid, setIsProjectPanelTrashed } from "store/project-panel/project-panel-action";
12 import { projectPanelActions } from "store/project-panel/project-panel-action-bind";
13 import {
14     activateSidePanelTreeItem,
15     initSidePanelTree,
16     loadSidePanelTreeProjects,
17     SidePanelTreeCategory,
18     SIDE_PANEL_TREE,
19 } from "store/side-panel-tree/side-panel-tree-actions";
20 import { updateResources } from "store/resources/resources-actions";
21 import { projectPanelColumns } from "views/project-panel/project-panel";
22 import { favoritePanelColumns } from "views/favorite-panel/favorite-panel";
23 import { matchRootRoute } from "routes/routes";
24 import {
25     setGroupDetailsBreadcrumbs,
26     setGroupsBreadcrumbs,
27     setProcessBreadcrumbs,
28     setSharedWithMeBreadcrumbs,
29     setSidePanelBreadcrumbs,
30     setTrashBreadcrumbs,
31     setUsersBreadcrumbs,
32     setMyAccountBreadcrumbs,
33     setUserProfileBreadcrumbs,
34     setInstanceTypesBreadcrumbs,
35     setVirtualMachinesBreadcrumbs,
36     setVirtualMachinesAdminBreadcrumbs,
37     setRepositoriesBreadcrumbs,
38 } from "store/breadcrumbs/breadcrumbs-actions";
39 import { navigateTo, navigateToRootProject } from "store/navigation/navigation-action";
40 import { MoveToFormDialogData } from "store/move-to-dialog/move-to-dialog";
41 import { ServiceRepository } from "services/services";
42 import { getResource } from "store/resources/resources";
43 import * as projectCreateActions from "store/projects/project-create-actions";
44 import * as projectMoveActions from "store/projects/project-move-actions";
45 import * as projectUpdateActions from "store/projects/project-update-actions";
46 import * as collectionCreateActions from "store/collections/collection-create-actions";
47 import * as collectionCopyActions from "store/collections/collection-copy-actions";
48 import * as collectionMoveActions from "store/collections/collection-move-actions";
49 import * as processesActions from "store/processes/processes-actions";
50 import * as processMoveActions from "store/processes/process-move-actions";
51 import * as processUpdateActions from "store/processes/process-update-actions";
52 import * as processCopyActions from "store/processes/process-copy-actions";
53 import { trashPanelColumns } from "views/trash-panel/trash-panel";
54 import { loadTrashPanel, trashPanelActions } from "store/trash-panel/trash-panel-action";
55 import { loadProcessPanel } from "store/process-panel/process-panel-actions";
56 import { loadSharedWithMePanel, sharedWithMePanelActions } from "store/shared-with-me-panel/shared-with-me-panel-actions";
57 import { sharedWithMePanelColumns } from "views/shared-with-me-panel/shared-with-me-panel";
58 import { CopyFormDialogData } from "store/copy-dialog/copy-dialog";
59 import { workflowPanelActions } from "store/workflow-panel/workflow-panel-actions";
60 import { loadSshKeysPanel } from "store/auth/auth-action-ssh";
61 import { loadLinkAccountPanel, linkAccountPanelActions } from "store/link-account-panel/link-account-panel-actions";
62 import { loadSiteManagerPanel } from "store/auth/auth-action-session";
63 import { workflowPanelColumns } from "views/workflow-panel/workflow-panel-view";
64 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
65 import { getProgressIndicator } from "store/progress-indicator/progress-indicator-reducer";
66 import { extractUuidKind, Resource, ResourceKind } from "models/resource";
67 import { FilterBuilder } from "services/api/filter-builder";
68 import { GroupContentsResource } from "services/groups-service/groups-service";
69 import { MatchCases, ofType, unionize, UnionOf } from "common/unionize";
70 import { loadRunProcessPanel } from "store/run-process-panel/run-process-panel-actions";
71 import { collectionPanelActions, loadCollectionPanel } from "store/collection-panel/collection-panel-action";
72 import { CollectionResource } from "models/collection";
73 import { WorkflowResource } from "models/workflow";
74 import { loadSearchResultsPanel, searchResultsPanelActions } from "store/search-results-panel/search-results-panel-actions";
75 import { searchResultsPanelColumns } from "views/search-results-panel/search-results-panel-view";
76 import { loadVirtualMachinesPanel } from "store/virtual-machines/virtual-machines-actions";
77 import { loadRepositoriesPanel } from "store/repositories/repositories-actions";
78 import { loadKeepServicesPanel } from "store/keep-services/keep-services-actions";
79 import { loadUsersPanel, userBindedActions } from "store/users/users-actions";
80 import * as userProfilePanelActions from "store/user-profile/user-profile-actions";
81 import { linkPanelActions, loadLinkPanel } from "store/link-panel/link-panel-actions";
82 import { linkPanelColumns } from "views/link-panel/link-panel-root";
83 import { userPanelColumns } from "views/user-panel/user-panel";
84 import { loadApiClientAuthorizationsPanel, apiClientAuthorizationsActions } from "store/api-client-authorizations/api-client-authorizations-actions";
85 import { apiClientAuthorizationPanelColumns } from "views/api-client-authorization-panel/api-client-authorization-panel-root";
86 import * as groupPanelActions from "store/groups-panel/groups-panel-actions";
87 import { groupsPanelColumns } from "views/groups-panel/groups-panel";
88 import * as groupDetailsPanelActions from "store/group-details-panel/group-details-panel-actions";
89 import { groupDetailsMembersPanelColumns, groupDetailsPermissionsPanelColumns } from "views/group-details-panel/group-details-panel";
90 import { DataTableFetchMode } from "components/data-table/data-table";
91 import { loadPublicFavoritePanel, publicFavoritePanelActions } from "store/public-favorites-panel/public-favorites-action";
92 import { publicFavoritePanelColumns } from "views/public-favorites-panel/public-favorites-panel";
93 import {
94     loadCollectionsContentAddressPanel,
95     collectionsContentAddressActions,
96 } from "store/collections-content-address-panel/collections-content-address-panel-actions";
97 import { collectionContentAddressPanelColumns } from "views/collection-content-address-panel/collection-content-address-panel";
98 import { subprocessPanelActions } from "store/subprocess-panel/subprocess-panel-actions";
99 import { subprocessPanelColumns } from "views/subprocess-panel/subprocess-panel-root";
100 import { loadAllProcessesPanel, allProcessesPanelActions } from "../all-processes-panel/all-processes-panel-action";
101 import { allProcessesPanelColumns } from "views/all-processes-panel/all-processes-panel";
102 import { userProfileGroupsColumns } from "views/user-profile-panel/user-profile-panel-root";
103 import { selectedToArray, selectedToKindSet } from "components/multiselect-toolbar/MultiselectToolbar";
104 import { deselectOne } from "store/multiselect/multiselect-actions";
105 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
106 import { workflowProcessesPanelColumns } from "views/workflow-panel/workflow-processes-panel-root";
107 import { workflowProcessesPanelActions } from "store/workflow-panel/workflow-panel-actions";
108 import { Breadcrumb } from "components/breadcrumbs/breadcrumbs";
109
110 export const WORKBENCH_LOADING_SCREEN = "workbenchLoadingScreen";
111
112 export const isWorkbenchLoading = (state: RootState) => {
113     const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
114     return progress ? progress.working : false;
115 };
116
117 export const handleFirstTimeLoad = (action: any) => async (dispatch: Dispatch<any>, getState: () => RootState) => {
118     try {
119         await dispatch(action);
120     } catch (e) {
121         snackbarActions.OPEN_SNACKBAR({
122             message: "Error " + e,
123             hideDuration: 8000,
124             kind: SnackbarKind.WARNING,
125         })
126     } finally {
127         if (isWorkbenchLoading(getState())) {
128             dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
129         }
130     }
131 };
132
133 export const loadWorkbench = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
134     dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
135     const { auth, router } = getState();
136     const { user } = auth;
137     if (user) {
138         dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
139         dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
140         dispatch(
141             allProcessesPanelActions.SET_COLUMNS({
142                 columns: allProcessesPanelColumns,
143             })
144         );
145         dispatch(
146             publicFavoritePanelActions.SET_COLUMNS({
147                 columns: publicFavoritePanelColumns,
148             })
149         );
150         dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
151         dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: sharedWithMePanelColumns }));
152         dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
153         dispatch(
154             searchResultsPanelActions.SET_FETCH_MODE({
155                 fetchMode: DataTableFetchMode.INFINITE,
156             })
157         );
158         dispatch(
159             searchResultsPanelActions.SET_COLUMNS({
160                 columns: searchResultsPanelColumns,
161             })
162         );
163         dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
164         dispatch(
165             groupPanelActions.GroupsPanelActions.SET_COLUMNS({
166                 columns: groupsPanelColumns,
167             })
168         );
169         dispatch(
170             groupDetailsPanelActions.GroupMembersPanelActions.SET_COLUMNS({
171                 columns: groupDetailsMembersPanelColumns,
172             })
173         );
174         dispatch(
175             groupDetailsPanelActions.GroupPermissionsPanelActions.SET_COLUMNS({
176                 columns: groupDetailsPermissionsPanelColumns,
177             })
178         );
179         dispatch(
180             userProfilePanelActions.UserProfileGroupsActions.SET_COLUMNS({
181                 columns: userProfileGroupsColumns,
182             })
183         );
184         dispatch(linkPanelActions.SET_COLUMNS({ columns: linkPanelColumns }));
185         dispatch(
186             apiClientAuthorizationsActions.SET_COLUMNS({
187                 columns: apiClientAuthorizationPanelColumns,
188             })
189         );
190         dispatch(
191             collectionsContentAddressActions.SET_COLUMNS({
192                 columns: collectionContentAddressPanelColumns,
193             })
194         );
195         dispatch(subprocessPanelActions.SET_COLUMNS({ columns: subprocessPanelColumns }));
196         dispatch(workflowProcessesPanelActions.SET_COLUMNS({ columns: workflowProcessesPanelColumns }));
197
198         if (services.linkAccountService.getAccountToLink()) {
199             dispatch(linkAccountPanelActions.HAS_SESSION_DATA());
200         }
201
202         dispatch<any>(initSidePanelTree());
203         if (router.location) {
204             const match = matchRootRoute(router.location.pathname);
205             if (match) {
206                 dispatch<any>(navigateToRootProject);
207             }
208         }
209     } else {
210         dispatch(userIsNotAuthenticated);
211     }
212 };
213
214 export const loadFavorites = () =>
215     handleFirstTimeLoad((dispatch: Dispatch) => {
216         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
217         dispatch<any>(loadFavoritePanel());
218         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
219     });
220
221 export const loadCollectionContentAddress = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
222     await dispatch(loadCollectionsContentAddressPanel());
223 });
224
225 export const loadTrash = () =>
226     handleFirstTimeLoad((dispatch: Dispatch) => {
227         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
228         dispatch<any>(loadTrashPanel());
229         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
230     });
231
232 export const loadAllProcesses = () =>
233     handleFirstTimeLoad((dispatch: Dispatch) => {
234         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.ALL_PROCESSES));
235         dispatch<any>(loadAllProcessesPanel());
236         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.ALL_PROCESSES));
237     });
238
239 export const loadProject = (uuid: string) =>
240     handleFirstTimeLoad(async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
241         const userUuid = getUserUuid(getState());
242         dispatch(setIsProjectPanelTrashed(false));
243         if (!userUuid) {
244             return;
245         }
246         try {
247             dispatch(progressIndicatorActions.START_WORKING(uuid));
248             if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
249                 // Load another users home projects
250                 dispatch(finishLoadingProject(uuid));
251                 const prevBreadcrumbs = getState().properties.breadcrumbs as Breadcrumb[] || [];
252                 if (prevBreadcrumbs.length === 0) {
253                     dispatch<any>(setSidePanelBreadcrumbs(uuid));
254                 }
255             } else if (userUuid !== uuid) {
256                 await dispatch(finishLoadingProject(uuid));
257                 const match = await loadGroupContentsResource({
258                     uuid,
259                     userUuid,
260                     services,
261                 });
262                 match({
263                     OWNED: async () => {
264                         await dispatch(activateSidePanelTreeItem(uuid));
265                         dispatch<any>(setSidePanelBreadcrumbs(uuid));
266                     },
267                     SHARED: async () => {
268                         await dispatch(activateSidePanelTreeItem(uuid));
269                         dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
270                     },
271                     TRASHED: async () => {
272                         await dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
273                         dispatch<any>(setTrashBreadcrumbs(uuid));
274                         dispatch(setIsProjectPanelTrashed(true));
275                     },
276                 });
277             } else {
278                 await dispatch(finishLoadingProject(userUuid));
279                 await dispatch(activateSidePanelTreeItem(userUuid));
280                 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
281             }
282         } finally {
283             dispatch(progressIndicatorActions.STOP_WORKING(uuid));
284         }
285     });
286
287 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) => async (dispatch: Dispatch) => {
288     const newProject = await dispatch<any>(projectCreateActions.createProject(data));
289     if (newProject) {
290         dispatch(
291             snackbarActions.OPEN_SNACKBAR({
292                 message: "Project has been successfully created.",
293                 hideDuration: 2000,
294                 kind: SnackbarKind.SUCCESS,
295             })
296         );
297         await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
298         dispatch<any>(navigateTo(newProject.uuid));
299     }
300 };
301
302 export const moveProject =
303     (data: MoveToFormDialogData, isSecondaryMove = false) =>
304         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
305             const checkedList = getState().multiselect.checkedList;
306             const uuidsToMove: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
307
308             //if no items in checkedlist default to normal context menu behavior
309             if (!isSecondaryMove && !uuidsToMove.length) uuidsToMove.push(data.uuid);
310
311             const sourceUuid = getResource(data.uuid)(getState().resources)?.ownerUuid;
312             const destinationUuid = data.ownerUuid;
313
314             const projectsToMove: MoveableResource[] = uuidsToMove
315                 .map(uuid => getResource(uuid)(getState().resources) as MoveableResource)
316                 .filter(resource => resource.kind === ResourceKind.PROJECT);
317
318             for (const project of projectsToMove) {
319                 await moveSingleProject(project);
320             }
321
322             //omly propagate if this call is the original
323             if (!isSecondaryMove) {
324                 const kindsToMove: Set<string> = selectedToKindSet(checkedList);
325                 kindsToMove.delete(ResourceKind.PROJECT);
326
327                 kindsToMove.forEach(kind => {
328                     secondaryMove[kind](data, true)(dispatch, getState, services);
329                 });
330             }
331
332             async function moveSingleProject(project: MoveableResource) {
333                 try {
334                     const oldProject: MoveToFormDialogData = { name: project.name, uuid: project.uuid, ownerUuid: data.ownerUuid };
335                     const oldOwnerUuid = oldProject ? oldProject.ownerUuid : "";
336                     const movedProject = await dispatch<any>(projectMoveActions.moveProject(oldProject));
337                     if (movedProject) {
338                         dispatch(
339                             snackbarActions.OPEN_SNACKBAR({
340                                 message: "Project has been moved",
341                                 hideDuration: 2000,
342                                 kind: SnackbarKind.SUCCESS,
343                             })
344                         );
345                         await dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
346                     }
347                 } catch (e) {
348                     dispatch(
349                         snackbarActions.OPEN_SNACKBAR({
350                             message: !!(project as any).frozenByUuid ? 'Could not move frozen project.' : e.message,
351                             hideDuration: 2000,
352                             kind: SnackbarKind.ERROR,
353                         })
354                     );
355                 }
356             }
357             if (sourceUuid) await dispatch<any>(loadSidePanelTreeProjects(sourceUuid));
358             await dispatch<any>(loadSidePanelTreeProjects(destinationUuid));
359         };
360
361 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) => async (dispatch: Dispatch) => {
362     const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
363     if (updatedProject) {
364         dispatch(
365             snackbarActions.OPEN_SNACKBAR({
366                 message: "Project has been successfully updated.",
367                 hideDuration: 2000,
368                 kind: SnackbarKind.SUCCESS,
369             })
370         );
371         await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
372         dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
373     }
374 };
375
376 export const updateGroup = (data: projectUpdateActions.ProjectUpdateFormDialogData) => async (dispatch: Dispatch) => {
377     const updatedGroup = await dispatch<any>(groupPanelActions.updateGroup(data));
378     if (updatedGroup) {
379         dispatch(
380             snackbarActions.OPEN_SNACKBAR({
381                 message: "Group has been successfully updated.",
382                 hideDuration: 2000,
383                 kind: SnackbarKind.SUCCESS,
384             })
385         );
386         await dispatch<any>(loadSidePanelTreeProjects(updatedGroup.ownerUuid));
387         dispatch<any>(reloadProjectMatchingUuid([updatedGroup.ownerUuid, updatedGroup.uuid]));
388     }
389 };
390
391 export const loadCollection = (uuid: string) =>
392     handleFirstTimeLoad(async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
393         const userUuid = getUserUuid(getState());
394         try {
395             dispatch(progressIndicatorActions.START_WORKING(uuid));
396             if (userUuid) {
397                 const match = await loadGroupContentsResource({
398                     uuid,
399                     userUuid,
400                     services,
401                 });
402                 let collection: CollectionResource | undefined;
403                 let breadcrumbfunc:
404                     | ((uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => Promise<void>)
405                     | undefined;
406                 let sidepanel: string | undefined;
407                 match({
408                     OWNED: thecollection => {
409                         collection = thecollection as CollectionResource;
410                         sidepanel = collection.ownerUuid;
411                         breadcrumbfunc = setSidePanelBreadcrumbs;
412                     },
413                     SHARED: thecollection => {
414                         collection = thecollection as CollectionResource;
415                         sidepanel = collection.ownerUuid;
416                         breadcrumbfunc = setSharedWithMeBreadcrumbs;
417                     },
418                     TRASHED: thecollection => {
419                         collection = thecollection as CollectionResource;
420                         sidepanel = SidePanelTreeCategory.TRASH;
421                         breadcrumbfunc = () => setTrashBreadcrumbs("");
422                     },
423                 });
424                 if (collection && breadcrumbfunc && sidepanel) {
425                     dispatch(updateResources([collection]));
426                     await dispatch<any>(finishLoadingProject(collection.ownerUuid));
427                     dispatch(collectionPanelActions.SET_COLLECTION(collection));
428                     await dispatch(activateSidePanelTreeItem(sidepanel));
429                     dispatch(breadcrumbfunc(collection.ownerUuid));
430                     dispatch(loadCollectionPanel(collection.uuid));
431                 }
432             }
433         } finally {
434             dispatch(progressIndicatorActions.STOP_WORKING(uuid));
435         }
436     });
437
438 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) => async (dispatch: Dispatch) => {
439     const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
440     if (collection) {
441         dispatch(
442             snackbarActions.OPEN_SNACKBAR({
443                 message: "Collection has been successfully created.",
444                 hideDuration: 2000,
445                 kind: SnackbarKind.SUCCESS,
446             })
447         );
448         dispatch<any>(updateResources([collection]));
449         dispatch<any>(navigateTo(collection.uuid));
450     }
451 };
452
453 export const copyCollection = (data: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
454     const checkedList = getState().multiselect.checkedList;
455     const uuidsToCopy: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
456
457     //if no items in checkedlist && no items passed in, default to normal context menu behavior
458     if (!uuidsToCopy.length) uuidsToCopy.push(data.uuid);
459
460     const collectionsToCopy: CollectionCopyResource[] = uuidsToCopy
461         .map(uuid => getResource(uuid)(getState().resources) as CollectionCopyResource)
462         .filter(resource => resource.kind === ResourceKind.COLLECTION);
463
464     for (const collection of collectionsToCopy) {
465         await copySingleCollection({ ...collection, ownerUuid: data.ownerUuid } as CollectionCopyResource);
466     }
467
468     async function copySingleCollection(copyToProject: CollectionCopyResource) {
469         const newName = data.fromContextMenu || collectionsToCopy.length === 1 ? data.name : `Copy of: ${copyToProject.name}`;
470         try {
471             const collection = await dispatch<any>(
472                 collectionCopyActions.copyCollection({
473                     ...copyToProject,
474                     name: newName,
475                     fromContextMenu: collectionsToCopy.length === 1 ? true : data.fromContextMenu,
476                 })
477             );
478             if (copyToProject && collection) {
479                 await dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
480                 dispatch(
481                     snackbarActions.OPEN_SNACKBAR({
482                         message: "Collection has been copied.",
483                         hideDuration: 3000,
484                         kind: SnackbarKind.SUCCESS,
485                         link: collection.ownerUuid,
486                     })
487                 );
488                 dispatch<any>(deselectOne(copyToProject.uuid));
489             }
490         } catch (e) {
491             dispatch(
492                 snackbarActions.OPEN_SNACKBAR({
493                     message: e.message,
494                     hideDuration: 2000,
495                     kind: SnackbarKind.ERROR,
496                 })
497             );
498         }
499     }
500     dispatch(projectPanelActions.REQUEST_ITEMS());
501 };
502
503 export const moveCollection =
504     (data: MoveToFormDialogData, isSecondaryMove = false) =>
505         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
506             const checkedList = getState().multiselect.checkedList;
507             const uuidsToMove: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
508
509             //if no items in checkedlist && no items passed in, default to normal context menu behavior
510             if (!isSecondaryMove && !uuidsToMove.length) uuidsToMove.push(data.uuid);
511
512             const collectionsToMove: MoveableResource[] = uuidsToMove
513                 .map(uuid => getResource(uuid)(getState().resources) as MoveableResource)
514                 .filter(resource => resource.kind === ResourceKind.COLLECTION);
515
516             for (const collection of collectionsToMove) {
517                 await moveSingleCollection(collection);
518             }
519
520             //omly propagate if this call is the original
521             if (!isSecondaryMove) {
522                 const kindsToMove: Set<string> = selectedToKindSet(checkedList);
523                 kindsToMove.delete(ResourceKind.COLLECTION);
524
525                 kindsToMove.forEach(kind => {
526                     secondaryMove[kind](data, true)(dispatch, getState, services);
527                 });
528             }
529
530             async function moveSingleCollection(collection: MoveableResource) {
531                 try {
532                     const oldCollection: MoveToFormDialogData = { name: collection.name, uuid: collection.uuid, ownerUuid: data.ownerUuid };
533                     const movedCollection = await dispatch<any>(collectionMoveActions.moveCollection(oldCollection));
534                     dispatch<any>(updateResources([movedCollection]));
535                     dispatch<any>(reloadProjectMatchingUuid([movedCollection.ownerUuid]));
536                     dispatch(
537                         snackbarActions.OPEN_SNACKBAR({
538                             message: "Collection has been moved.",
539                             hideDuration: 2000,
540                             kind: SnackbarKind.SUCCESS,
541                         })
542                     );
543                 } catch (e) {
544                     dispatch(
545                         snackbarActions.OPEN_SNACKBAR({
546                             message: e.message,
547                             hideDuration: 2000,
548                             kind: SnackbarKind.ERROR,
549                         })
550                     );
551                 }
552             }
553         };
554
555 export const loadProcess = (uuid: string) =>
556     handleFirstTimeLoad(async (dispatch: Dispatch, getState: () => RootState) => {
557         try {
558             dispatch(progressIndicatorActions.START_WORKING(uuid));
559             dispatch<any>(loadProcessPanel(uuid));
560             const process = await dispatch<any>(processesActions.loadProcess(uuid));
561             if (process) {
562                 await dispatch<any>(finishLoadingProject(process.containerRequest.ownerUuid));
563                 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
564                 dispatch<any>(setProcessBreadcrumbs(uuid));
565                 dispatch<any>(loadDetailsPanel(uuid));
566             }
567         } finally {
568             dispatch(progressIndicatorActions.STOP_WORKING(uuid));
569         }
570     });
571
572 export const loadRegisteredWorkflow = (uuid: string) =>
573     handleFirstTimeLoad(async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
574         const userUuid = getUserUuid(getState());
575         if (userUuid) {
576             const match = await loadGroupContentsResource({
577                 uuid,
578                 userUuid,
579                 services,
580             });
581             let workflow: WorkflowResource | undefined;
582             let breadcrumbfunc:
583                 | ((uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => Promise<void>)
584                 | undefined;
585             match({
586                 OWNED: async theworkflow => {
587                     workflow = theworkflow as WorkflowResource;
588                     breadcrumbfunc = setSidePanelBreadcrumbs;
589                 },
590                 SHARED: async theworkflow => {
591                     workflow = theworkflow as WorkflowResource;
592                     breadcrumbfunc = setSharedWithMeBreadcrumbs;
593                 },
594                 TRASHED: () => { },
595             });
596             if (workflow && breadcrumbfunc) {
597                 dispatch(updateResources([workflow]));
598                 await dispatch<any>(finishLoadingProject(workflow.ownerUuid));
599                 await dispatch<any>(activateSidePanelTreeItem(workflow.ownerUuid));
600                 dispatch<any>(breadcrumbfunc(workflow.ownerUuid));
601                 dispatch(workflowProcessesPanelActions.REQUEST_ITEMS());
602             }
603         }
604     });
605
606 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) => async (dispatch: Dispatch) => {
607     try {
608         const process = await dispatch<any>(processUpdateActions.updateProcess(data));
609         if (process) {
610             dispatch(
611                 snackbarActions.OPEN_SNACKBAR({
612                     message: "Process has been successfully updated.",
613                     hideDuration: 2000,
614                     kind: SnackbarKind.SUCCESS,
615                 })
616             );
617             dispatch<any>(updateResources([process]));
618             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
619         }
620     } catch (e) {
621         dispatch(
622             snackbarActions.OPEN_SNACKBAR({
623                 message: e.message,
624                 hideDuration: 2000,
625                 kind: SnackbarKind.ERROR,
626             })
627         );
628     }
629 };
630
631 export const moveProcess =
632     (data: MoveToFormDialogData, isSecondaryMove = false) =>
633         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
634             const checkedList = getState().multiselect.checkedList;
635             const uuidsToMove: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
636
637             //if no items in checkedlist && no items passed in, default to normal context menu behavior
638             if (!isSecondaryMove && !uuidsToMove.length) uuidsToMove.push(data.uuid);
639
640             const processesToMove: MoveableResource[] = uuidsToMove
641                 .map(uuid => getResource(uuid)(getState().resources) as MoveableResource)
642                 .filter(resource => resource.kind === ResourceKind.PROCESS);
643
644             for (const process of processesToMove) {
645                 await moveSingleProcess(process);
646             }
647
648             //omly propagate if this call is the original
649             if (!isSecondaryMove) {
650                 const kindsToMove: Set<string> = selectedToKindSet(checkedList);
651                 kindsToMove.delete(ResourceKind.PROCESS);
652
653                 kindsToMove.forEach(kind => {
654                     secondaryMove[kind](data, true)(dispatch, getState, services);
655                 });
656             }
657
658             async function moveSingleProcess(process: MoveableResource) {
659                 try {
660                     const oldProcess: MoveToFormDialogData = { name: process.name, uuid: process.uuid, ownerUuid: data.ownerUuid };
661                     const movedProcess = await dispatch<any>(processMoveActions.moveProcess(oldProcess));
662                     dispatch<any>(updateResources([movedProcess]));
663                     dispatch<any>(reloadProjectMatchingUuid([movedProcess.ownerUuid]));
664                     dispatch(
665                         snackbarActions.OPEN_SNACKBAR({
666                             message: "Process has been moved.",
667                             hideDuration: 2000,
668                             kind: SnackbarKind.SUCCESS,
669                         })
670                     );
671                 } catch (e) {
672                     dispatch(
673                         snackbarActions.OPEN_SNACKBAR({
674                             message: e.message,
675                             hideDuration: 2000,
676                             kind: SnackbarKind.ERROR,
677                         })
678                     );
679                 }
680             }
681         };
682
683 export const copyProcess = (data: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
684     try {
685         const process = await dispatch<any>(processCopyActions.copyProcess(data));
686         dispatch<any>(updateResources([process]));
687         dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
688         dispatch(
689             snackbarActions.OPEN_SNACKBAR({
690                 message: "Process has been copied.",
691                 hideDuration: 2000,
692                 kind: SnackbarKind.SUCCESS,
693             })
694         );
695         dispatch<any>(navigateTo(process.uuid));
696     } catch (e) {
697         dispatch(
698             snackbarActions.OPEN_SNACKBAR({
699                 message: e.message,
700                 hideDuration: 2000,
701                 kind: SnackbarKind.ERROR,
702             })
703         );
704     }
705 };
706
707 export const resourceIsNotLoaded = (uuid: string) =>
708     snackbarActions.OPEN_SNACKBAR({
709         message: `Resource identified by ${uuid} is not loaded.`,
710         kind: SnackbarKind.ERROR,
711     });
712
713 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
714     message: "User is not authenticated",
715     kind: SnackbarKind.ERROR,
716 });
717
718 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
719     message: "Could not load user",
720     kind: SnackbarKind.ERROR,
721 });
722
723 export const reloadProjectMatchingUuid =
724     (matchingUuids: string[]) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
725         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
726         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
727             dispatch<any>(loadProject(currentProjectPanelUuid));
728         }
729     };
730
731 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
732     dispatch<any>(loadSharedWithMePanel());
733     await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
734     await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
735 });
736
737 export const loadRunProcess = handleFirstTimeLoad(async (dispatch: Dispatch) => {
738     await dispatch<any>(loadRunProcessPanel());
739 });
740
741 export const loadPublicFavorites = () =>
742     handleFirstTimeLoad((dispatch: Dispatch) => {
743         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.PUBLIC_FAVORITES));
744         dispatch<any>(loadPublicFavoritePanel());
745         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.PUBLIC_FAVORITES));
746     });
747
748 export const loadSearchResults = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
749     await dispatch(loadSearchResultsPanel());
750 });
751
752 export const loadLinks = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
753     await dispatch(loadLinkPanel());
754 });
755
756 export const loadVirtualMachines = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
757     await dispatch(loadVirtualMachinesPanel());
758     dispatch(setVirtualMachinesBreadcrumbs());
759     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHELL_ACCESS));
760 });
761
762 export const loadVirtualMachinesAdmin = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
763     await dispatch(loadVirtualMachinesPanel());
764     dispatch(setVirtualMachinesAdminBreadcrumbs());
765     dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SIDE_PANEL_TREE }))
766 });
767
768 export const loadRepositories = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
769     await dispatch(loadRepositoriesPanel());
770     dispatch(setRepositoriesBreadcrumbs());
771 });
772
773 export const loadSshKeys = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
774     await dispatch(loadSshKeysPanel());
775 });
776
777 export const loadInstanceTypes = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
778     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.INSTANCE_TYPES));
779     dispatch(setInstanceTypesBreadcrumbs());
780 });
781
782 export const loadSiteManager = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
783     await dispatch(loadSiteManagerPanel());
784 });
785
786 export const loadUserProfile = (userUuid?: string) =>
787     handleFirstTimeLoad((dispatch: Dispatch<any>) => {
788         if (userUuid) {
789             dispatch(setUserProfileBreadcrumbs(userUuid));
790             dispatch(userProfilePanelActions.loadUserProfilePanel(userUuid));
791         } else {
792             dispatch(setMyAccountBreadcrumbs());
793             dispatch(userProfilePanelActions.loadUserProfilePanel());
794         }
795     });
796
797 export const loadLinkAccount = handleFirstTimeLoad((dispatch: Dispatch<any>) => {
798     dispatch(loadLinkAccountPanel());
799 });
800
801 export const loadKeepServices = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
802     await dispatch(loadKeepServicesPanel());
803 });
804
805 export const loadUsers = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
806     await dispatch(loadUsersPanel());
807     dispatch(setUsersBreadcrumbs());
808 });
809
810 export const loadApiClientAuthorizations = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
811     await dispatch(loadApiClientAuthorizationsPanel());
812 });
813
814 export const loadGroupsPanel = handleFirstTimeLoad((dispatch: Dispatch<any>) => {
815     dispatch(setGroupsBreadcrumbs());
816     dispatch(groupPanelActions.loadGroupsPanel());
817 });
818
819 export const loadGroupDetailsPanel = (groupUuid: string) =>
820     handleFirstTimeLoad((dispatch: Dispatch<any>) => {
821         dispatch(setGroupDetailsBreadcrumbs(groupUuid));
822         dispatch(groupDetailsPanelActions.loadGroupDetailsPanel(groupUuid));
823     });
824
825 const finishLoadingProject = (project: GroupContentsResource | string) => async (dispatch: Dispatch<any>) => {
826     const uuid = typeof project === "string" ? project : project.uuid;
827     dispatch(loadDetailsPanel(uuid));
828     if (typeof project !== "string") {
829         dispatch(updateResources([project]));
830     }
831 };
832
833 const loadGroupContentsResource = async (params: { uuid: string; userUuid: string; services: ServiceRepository }) => {
834     const filters = new FilterBuilder().addEqual("uuid", params.uuid).getFilters();
835     const { items } = await params.services.groupsService.contents(params.userUuid, {
836         filters,
837         recursive: true,
838         includeTrash: true,
839     });
840     const resource = items.shift();
841     let handler: GroupContentsHandler;
842     if (resource) {
843         handler =
844             (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
845                 ? groupContentsHandlers.TRASHED(resource)
846                 : groupContentsHandlers.OWNED(resource);
847     } else {
848         const kind = extractUuidKind(params.uuid);
849         let resource: GroupContentsResource;
850         if (kind === ResourceKind.COLLECTION) {
851             resource = await params.services.collectionService.get(params.uuid);
852         } else if (kind === ResourceKind.PROJECT) {
853             resource = await params.services.projectService.get(params.uuid);
854         } else if (kind === ResourceKind.WORKFLOW) {
855             resource = await params.services.workflowService.get(params.uuid);
856         } else if (kind === ResourceKind.CONTAINER_REQUEST) {
857             resource = await params.services.containerRequestService.get(params.uuid);
858         } else {
859             throw new Error("loadGroupContentsResource unsupported kind " + kind);
860         }
861         handler = groupContentsHandlers.SHARED(resource);
862     }
863     return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) => groupContentsHandlers.match(handler, cases);
864 };
865
866 const groupContentsHandlersRecord = {
867     TRASHED: ofType<GroupContentsResource>(),
868     SHARED: ofType<GroupContentsResource>(),
869     OWNED: ofType<GroupContentsResource>(),
870 };
871
872 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
873
874 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;
875
876 type CollectionCopyResource = Resource & { name: string; fromContextMenu: boolean };
877
878 type MoveableResource = Resource & { name: string };
879
880 type MoveFunc = (
881     data: MoveToFormDialogData,
882     isSecondaryMove?: boolean
883 ) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => Promise<void>;
884
885 const secondaryMove: Record<string, MoveFunc> = {
886     [ResourceKind.PROJECT]: moveProject,
887     [ResourceKind.PROCESS]: moveProcess,
888     [ResourceKind.COLLECTION]: moveCollection,
889 };