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