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