1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { RootState } from "~/store/store";
7 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
8 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
9 import { loadFavoritePanel } from '~/store/favorite-panel/favorite-panel-action';
10 import { openProjectPanel, projectPanelActions, setIsProjectPanelTrashed } from '~/store/project-panel/project-panel-action';
11 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory, loadSidePanelTreeProjects } from '~/store/side-panel-tree/side-panel-tree-actions';
12 import { loadResource, updateResources } from '~/store/resources/resources-actions';
13 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
14 import { projectPanelColumns } from '~/views/project-panel/project-panel';
15 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
16 import { matchRootRoute } from '~/routes/routes';
17 import { setSidePanelBreadcrumbs, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs, setTrashBreadcrumbs, setBreadcrumbs, setGroupDetailsBreadcrumbs, setGroupsBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
18 import { navigateToProject } from '~/store/navigation/navigation-action';
19 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
20 import { ServiceRepository } from '~/services/services';
21 import { getResource } from '~/store/resources/resources';
22 import { getProjectPanelCurrentUuid } from '~/store/project-panel/project-panel-action';
23 import * as projectCreateActions from '~/store/projects/project-create-actions';
24 import * as projectMoveActions from '~/store/projects/project-move-actions';
25 import * as projectUpdateActions from '~/store/projects/project-update-actions';
26 import * as collectionCreateActions from '~/store/collections/collection-create-actions';
27 import * as collectionCopyActions from '~/store/collections/collection-copy-actions';
28 import * as collectionUpdateActions from '~/store/collections/collection-update-actions';
29 import * as collectionMoveActions from '~/store/collections/collection-move-actions';
30 import * as processesActions from '~/store/processes/processes-actions';
31 import * as processMoveActions from '~/store/processes/process-move-actions';
32 import * as processUpdateActions from '~/store/processes/process-update-actions';
33 import * as processCopyActions from '~/store/processes/process-copy-actions';
34 import { trashPanelColumns } from "~/views/trash-panel/trash-panel";
35 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
36 import { initProcessLogsPanel } from '~/store/process-logs-panel/process-logs-panel-actions';
37 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
38 import { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
39 import { loadSharedWithMePanel } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
40 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
41 import { loadWorkflowPanel, workflowPanelActions } from '~/store/workflow-panel/workflow-panel-actions';
42 import { loadSshKeysPanel } from '~/store/auth/auth-action';
43 import { loadMyAccountPanel } from '~/store/my-account/my-account-panel-actions';
44 import { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel-view';
45 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
46 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
47 import { ResourceKind, extractUuidKind } from '~/models/resource';
48 import { FilterBuilder } from '~/services/api/filter-builder';
49 import { GroupContentsResource } from '~/services/groups-service/groups-service';
50 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
51 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
52 import { loadCollectionFiles } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
53 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
54 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
55 import { CollectionResource } from "~/models/collection";
56 import { searchResultsPanelActions, loadSearchResultsPanel } from '~/store/search-results-panel/search-results-panel-actions';
57 import { searchResultsPanelColumns } from '~/views/search-results-panel/search-results-panel-view';
58 import { loadVirtualMachinesPanel } from '~/store/virtual-machines/virtual-machines-actions';
59 import { loadRepositoriesPanel } from '~/store/repositories/repositories-actions';
60 import { loadKeepServicesPanel } from '~/store/keep-services/keep-services-actions';
61 import { loadUsersPanel, userBindedActions } from '~/store/users/users-actions';
62 import { userPanelColumns } from '~/views/user-panel/user-panel';
63 import { loadComputeNodesPanel } from '~/store/compute-nodes/compute-nodes-actions';
64 import { loadApiClientAuthorizationsPanel } from '~/store/api-client-authorizations/api-client-authorizations-actions';
65 import * as groupPanelActions from '~/store/groups-panel/groups-panel-actions';
66 import { groupsPanelColumns } from '~/views/groups-panel/groups-panel';
67 import * as groupDetailsPanelActions from '~/store/group-details-panel/group-details-panel-actions';
68 import { groupDetailsPanelColumns } from '~/views/group-details-panel/group-details-panel';
70 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
72 export const isWorkbenchLoading = (state: RootState) => {
73 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
74 return progress ? progress.working : false;
77 const handleFirstTimeLoad = (action: any) =>
78 async (dispatch: Dispatch<any>, getState: () => RootState) => {
80 await dispatch(action);
82 if (isWorkbenchLoading(getState())) {
83 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
88 export const loadWorkbench = () =>
89 async (dispatch: Dispatch, getState: () => RootState) => {
90 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
91 const { auth, router } = getState();
92 const { user } = auth;
94 const userResource = await dispatch<any>(loadResource(user.uuid));
96 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
97 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
98 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
99 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
100 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
101 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
102 dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
103 dispatch(groupPanelActions.GroupsPanelActions.SET_COLUMNS({ columns: groupsPanelColumns }));
104 dispatch(groupDetailsPanelActions.GroupDetailsPanelActions.SET_COLUMNS({columns: groupDetailsPanelColumns}));
105 dispatch<any>(initSidePanelTree());
106 if (router.location) {
107 const match = matchRootRoute(router.location.pathname);
109 dispatch(navigateToProject(userResource.uuid));
113 dispatch(userIsNotAuthenticated);
116 dispatch(userIsNotAuthenticated);
120 export const loadFavorites = () =>
122 (dispatch: Dispatch) => {
123 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
124 dispatch<any>(loadFavoritePanel());
125 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
128 export const loadTrash = () =>
130 (dispatch: Dispatch) => {
131 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
132 dispatch<any>(loadTrashPanel());
133 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
136 export const loadProject = (uuid: string) =>
138 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
139 const userUuid = services.authService.getUuid();
140 dispatch(setIsProjectPanelTrashed(false));
142 if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
143 // Load another users home projects
144 dispatch(finishLoadingProject(uuid));
145 } else if (userUuid !== uuid) {
146 const match = await loadGroupContentsResource({ uuid, userUuid, services });
148 OWNED: async project => {
149 await dispatch(activateSidePanelTreeItem(uuid));
150 dispatch<any>(setSidePanelBreadcrumbs(uuid));
151 dispatch(finishLoadingProject(project));
154 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
155 dispatch(activateSidePanelTreeItem(uuid));
156 dispatch(finishLoadingProject(project));
158 TRASHED: project => {
159 dispatch<any>(setTrashBreadcrumbs(uuid));
160 dispatch(setIsProjectPanelTrashed(true));
161 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
162 dispatch(finishLoadingProject(project));
166 await dispatch(activateSidePanelTreeItem(userUuid));
167 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
168 dispatch(finishLoadingProject(userUuid));
173 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
174 async (dispatch: Dispatch) => {
175 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
177 dispatch(snackbarActions.OPEN_SNACKBAR({
178 message: "Project has been successfully created.",
181 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
182 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
186 export const moveProject = (data: MoveToFormDialogData) =>
187 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
189 const oldProject = getResource(data.uuid)(getState().resources);
190 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
191 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
193 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
195 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
197 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
200 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
204 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
205 async (dispatch: Dispatch) => {
206 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
207 if (updatedProject) {
208 dispatch(snackbarActions.OPEN_SNACKBAR({
209 message: "Project has been successfully updated.",
212 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
213 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
217 export const loadCollection = (uuid: string) =>
219 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
220 const userUuid = services.authService.getUuid();
222 const match = await loadGroupContentsResource({ uuid, userUuid, services });
224 OWNED: async collection => {
225 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
226 dispatch(updateResources([collection]));
227 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
228 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
229 dispatch(loadCollectionFiles(collection.uuid));
231 SHARED: collection => {
232 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
233 dispatch(updateResources([collection]));
234 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
235 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
236 dispatch(loadCollectionFiles(collection.uuid));
238 TRASHED: collection => {
239 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
240 dispatch(updateResources([collection]));
241 dispatch(setTrashBreadcrumbs(''));
242 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
243 dispatch(loadCollectionFiles(collection.uuid));
250 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
251 async (dispatch: Dispatch) => {
252 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
254 dispatch(snackbarActions.OPEN_SNACKBAR({
255 message: "Collection has been successfully created.",
258 dispatch<any>(updateResources([collection]));
259 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
263 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
264 async (dispatch: Dispatch) => {
265 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
267 dispatch(snackbarActions.OPEN_SNACKBAR({
268 message: "Collection has been successfully updated.",
271 dispatch<any>(updateResources([collection]));
272 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
276 export const copyCollection = (data: CopyFormDialogData) =>
277 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
279 const copyToProject = getResource(data.ownerUuid)(getState().resources);
280 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
281 if (copyToProject && collection) {
282 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
283 dispatch(snackbarActions.OPEN_SNACKBAR({
284 message: 'Collection has been copied.',
286 kind: SnackbarKind.SUCCESS,
287 link: collection.ownerUuid
291 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
295 export const moveCollection = (data: MoveToFormDialogData) =>
296 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
298 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
299 dispatch<any>(updateResources([collection]));
300 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
301 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
303 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
307 export const loadProcess = (uuid: string) =>
309 async (dispatch: Dispatch, getState: () => RootState) => {
310 dispatch<any>(loadProcessPanel(uuid));
311 const process = await dispatch<any>(processesActions.loadProcess(uuid));
312 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
313 dispatch<any>(setProcessBreadcrumbs(uuid));
314 dispatch(loadDetailsPanel(uuid));
317 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
318 async (dispatch: Dispatch) => {
320 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
322 dispatch(snackbarActions.OPEN_SNACKBAR({
323 message: "Process has been successfully updated.",
326 dispatch<any>(updateResources([process]));
327 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
330 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
334 export const moveProcess = (data: MoveToFormDialogData) =>
335 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
337 const process = await dispatch<any>(processMoveActions.moveProcess(data));
338 dispatch<any>(updateResources([process]));
339 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
340 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
342 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
346 export const copyProcess = (data: CopyFormDialogData) =>
347 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
349 const process = await dispatch<any>(processCopyActions.copyProcess(data));
350 dispatch<any>(updateResources([process]));
351 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
352 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
354 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
358 export const loadProcessLog = (uuid: string) =>
360 async (dispatch: Dispatch) => {
361 const process = await dispatch<any>(processesActions.loadProcess(uuid));
362 dispatch<any>(setProcessBreadcrumbs(uuid));
363 dispatch<any>(initProcessLogsPanel(uuid));
364 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
367 export const resourceIsNotLoaded = (uuid: string) =>
368 snackbarActions.OPEN_SNACKBAR({
369 message: `Resource identified by ${uuid} is not loaded.`
372 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
373 message: 'User is not authenticated'
376 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
377 message: 'Could not load user'
380 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
381 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
382 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
383 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
384 dispatch<any>(loadProject(currentProjectPanelUuid));
388 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
389 dispatch<any>(loadSharedWithMePanel());
390 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
391 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
394 export const loadRunProcess = handleFirstTimeLoad(
395 async (dispatch: Dispatch) => {
396 await dispatch<any>(loadRunProcessPanel());
400 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
401 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
402 await dispatch(loadWorkflowPanel());
403 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
406 export const loadSearchResults = handleFirstTimeLoad(
407 async (dispatch: Dispatch<any>) => {
408 await dispatch(loadSearchResultsPanel());
411 export const loadVirtualMachines = handleFirstTimeLoad(
412 async (dispatch: Dispatch<any>) => {
413 await dispatch(loadVirtualMachinesPanel());
414 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
417 export const loadRepositories = handleFirstTimeLoad(
418 async (dispatch: Dispatch<any>) => {
419 await dispatch(loadRepositoriesPanel());
420 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
423 export const loadSshKeys = handleFirstTimeLoad(
424 async (dispatch: Dispatch<any>) => {
425 await dispatch(loadSshKeysPanel());
428 export const loadMyAccount = handleFirstTimeLoad(
429 (dispatch: Dispatch<any>) => {
430 dispatch(loadMyAccountPanel());
433 export const loadKeepServices = handleFirstTimeLoad(
434 async (dispatch: Dispatch<any>) => {
435 await dispatch(loadKeepServicesPanel());
438 export const loadUsers = handleFirstTimeLoad(
439 async (dispatch: Dispatch<any>) => {
440 await dispatch(loadUsersPanel());
441 dispatch(setBreadcrumbs([{ label: 'Users' }]));
444 export const loadComputeNodes = handleFirstTimeLoad(
445 async (dispatch: Dispatch<any>) => {
446 await dispatch(loadComputeNodesPanel());
449 export const loadApiClientAuthorizations = handleFirstTimeLoad(
450 async (dispatch: Dispatch<any>) => {
451 await dispatch(loadApiClientAuthorizationsPanel());
454 export const loadGroupsPanel = handleFirstTimeLoad(
455 (dispatch: Dispatch<any>) => {
456 dispatch(setGroupsBreadcrumbs());
457 dispatch(groupPanelActions.loadGroupsPanel());
461 export const loadGroupDetailsPanel = (groupUuid: string) =>
463 (dispatch: Dispatch<any>) => {
464 dispatch(setGroupDetailsBreadcrumbs(groupUuid));
465 dispatch(groupDetailsPanelActions.loadGroupDetailsPanel(groupUuid));
468 const finishLoadingProject = (project: GroupContentsResource | string) =>
469 async (dispatch: Dispatch<any>) => {
470 const uuid = typeof project === 'string' ? project : project.uuid;
471 dispatch(openProjectPanel(uuid));
472 dispatch(loadDetailsPanel(uuid));
473 if (typeof project !== 'string') {
474 dispatch(updateResources([project]));
478 const loadGroupContentsResource = async (params: {
481 services: ServiceRepository
483 const filters = new FilterBuilder()
484 .addEqual('uuid', params.uuid)
486 const { items } = await params.services.groupsService.contents(params.userUuid, {
491 const resource = items.shift();
492 let handler: GroupContentsHandler;
494 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
495 ? groupContentsHandlers.TRASHED(resource)
496 : groupContentsHandlers.OWNED(resource);
498 const kind = extractUuidKind(params.uuid);
499 let resource: GroupContentsResource;
500 if (kind === ResourceKind.COLLECTION) {
501 resource = await params.services.collectionService.get(params.uuid);
502 } else if (kind === ResourceKind.PROJECT) {
503 resource = await params.services.projectService.get(params.uuid);
505 resource = await params.services.containerRequestService.get(params.uuid);
507 handler = groupContentsHandlers.SHARED(resource);
509 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
510 groupContentsHandlers.match(handler, cases);
514 const groupContentsHandlersRecord = {
515 TRASHED: ofType<GroupContentsResource>(),
516 SHARED: ofType<GroupContentsResource>(),
517 OWNED: ofType<GroupContentsResource>(),
520 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
522 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;