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 } 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';
65 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
67 export const isWorkbenchLoading = (state: RootState) => {
68 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
69 return progress ? progress.working : false;
72 const handleFirstTimeLoad = (action: any) =>
73 async (dispatch: Dispatch<any>, getState: () => RootState) => {
75 await dispatch(action);
77 if (isWorkbenchLoading(getState())) {
78 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
83 export const loadWorkbench = () =>
84 async (dispatch: Dispatch, getState: () => RootState) => {
85 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
86 const { auth, router } = getState();
87 const { user } = auth;
89 const userResource = await dispatch<any>(loadResource(user.uuid));
91 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
92 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
93 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
94 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
95 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
96 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
97 dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
98 dispatch<any>(initSidePanelTree());
99 if (router.location) {
100 const match = matchRootRoute(router.location.pathname);
102 dispatch(navigateToProject(userResource.uuid));
106 dispatch(userIsNotAuthenticated);
109 dispatch(userIsNotAuthenticated);
113 export const loadFavorites = () =>
115 (dispatch: Dispatch) => {
116 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
117 dispatch<any>(loadFavoritePanel());
118 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
121 export const loadTrash = () =>
123 (dispatch: Dispatch) => {
124 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
125 dispatch<any>(loadTrashPanel());
126 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
129 export const loadProject = (uuid: string) =>
131 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
132 const userUuid = services.authService.getUuid();
133 dispatch(setIsProjectPanelTrashed(false));
135 if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
136 // Load another users home projects
137 dispatch(finishLoadingProject(uuid));
138 } else if (userUuid !== uuid) {
139 const match = await loadGroupContentsResource({ uuid, userUuid, services });
141 OWNED: async project => {
142 await dispatch(activateSidePanelTreeItem(uuid));
143 dispatch<any>(setSidePanelBreadcrumbs(uuid));
144 dispatch(finishLoadingProject(project));
147 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
148 dispatch(activateSidePanelTreeItem(uuid));
149 dispatch(finishLoadingProject(project));
151 TRASHED: project => {
152 dispatch<any>(setTrashBreadcrumbs(uuid));
153 dispatch(setIsProjectPanelTrashed(true));
154 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
155 dispatch(finishLoadingProject(project));
159 await dispatch(activateSidePanelTreeItem(userUuid));
160 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
161 dispatch(finishLoadingProject(userUuid));
166 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
167 async (dispatch: Dispatch) => {
168 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
170 dispatch(snackbarActions.OPEN_SNACKBAR({
171 message: "Project has been successfully created.",
174 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
175 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
179 export const moveProject = (data: MoveToFormDialogData) =>
180 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
182 const oldProject = getResource(data.uuid)(getState().resources);
183 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
184 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
186 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
188 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
190 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
193 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
197 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
198 async (dispatch: Dispatch) => {
199 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
200 if (updatedProject) {
201 dispatch(snackbarActions.OPEN_SNACKBAR({
202 message: "Project has been successfully updated.",
205 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
206 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
210 export const loadCollection = (uuid: string) =>
212 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
213 const userUuid = services.authService.getUuid();
215 const match = await loadGroupContentsResource({ uuid, userUuid, services });
217 OWNED: async collection => {
218 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
219 dispatch(updateResources([collection]));
220 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
221 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
222 dispatch(loadCollectionFiles(collection.uuid));
224 SHARED: collection => {
225 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
226 dispatch(updateResources([collection]));
227 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
228 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
229 dispatch(loadCollectionFiles(collection.uuid));
231 TRASHED: collection => {
232 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
233 dispatch(updateResources([collection]));
234 dispatch(setTrashBreadcrumbs(''));
235 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
236 dispatch(loadCollectionFiles(collection.uuid));
243 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
244 async (dispatch: Dispatch) => {
245 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
247 dispatch(snackbarActions.OPEN_SNACKBAR({
248 message: "Collection has been successfully created.",
251 dispatch<any>(updateResources([collection]));
252 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
256 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
257 async (dispatch: Dispatch) => {
258 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
260 dispatch(snackbarActions.OPEN_SNACKBAR({
261 message: "Collection has been successfully updated.",
264 dispatch<any>(updateResources([collection]));
265 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
269 export const copyCollection = (data: CopyFormDialogData) =>
270 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
272 const copyToProject = getResource(data.ownerUuid)(getState().resources);
273 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
274 if (copyToProject && collection) {
275 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
276 dispatch(snackbarActions.OPEN_SNACKBAR({
277 message: 'Collection has been copied.',
279 kind: SnackbarKind.SUCCESS,
280 link: collection.ownerUuid
284 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
288 export const moveCollection = (data: MoveToFormDialogData) =>
289 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
291 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
292 dispatch<any>(updateResources([collection]));
293 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
294 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
296 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
300 export const loadProcess = (uuid: string) =>
302 async (dispatch: Dispatch, getState: () => RootState) => {
303 dispatch<any>(loadProcessPanel(uuid));
304 const process = await dispatch<any>(processesActions.loadProcess(uuid));
305 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
306 dispatch<any>(setProcessBreadcrumbs(uuid));
307 dispatch(loadDetailsPanel(uuid));
310 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
311 async (dispatch: Dispatch) => {
313 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
315 dispatch(snackbarActions.OPEN_SNACKBAR({
316 message: "Process has been successfully updated.",
319 dispatch<any>(updateResources([process]));
320 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
323 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
327 export const moveProcess = (data: MoveToFormDialogData) =>
328 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
330 const process = await dispatch<any>(processMoveActions.moveProcess(data));
331 dispatch<any>(updateResources([process]));
332 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
333 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
335 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
339 export const copyProcess = (data: CopyFormDialogData) =>
340 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
342 const process = await dispatch<any>(processCopyActions.copyProcess(data));
343 dispatch<any>(updateResources([process]));
344 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
345 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
347 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
351 export const loadProcessLog = (uuid: string) =>
353 async (dispatch: Dispatch) => {
354 const process = await dispatch<any>(processesActions.loadProcess(uuid));
355 dispatch<any>(setProcessBreadcrumbs(uuid));
356 dispatch<any>(initProcessLogsPanel(uuid));
357 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
360 export const resourceIsNotLoaded = (uuid: string) =>
361 snackbarActions.OPEN_SNACKBAR({
362 message: `Resource identified by ${uuid} is not loaded.`
365 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
366 message: 'User is not authenticated'
369 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
370 message: 'Could not load user'
373 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
374 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
375 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
376 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
377 dispatch<any>(loadProject(currentProjectPanelUuid));
381 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
382 dispatch<any>(loadSharedWithMePanel());
383 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
384 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
387 export const loadRunProcess = handleFirstTimeLoad(
388 async (dispatch: Dispatch) => {
389 await dispatch<any>(loadRunProcessPanel());
393 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
394 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
395 await dispatch(loadWorkflowPanel());
396 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
399 export const loadSearchResults = handleFirstTimeLoad(
400 async (dispatch: Dispatch<any>) => {
401 await dispatch(loadSearchResultsPanel());
404 export const loadVirtualMachines = handleFirstTimeLoad(
405 async (dispatch: Dispatch<any>) => {
406 await dispatch(loadVirtualMachinesPanel());
407 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
410 export const loadRepositories = handleFirstTimeLoad(
411 async (dispatch: Dispatch<any>) => {
412 await dispatch(loadRepositoriesPanel());
413 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
416 export const loadSshKeys = handleFirstTimeLoad(
417 async (dispatch: Dispatch<any>) => {
418 await dispatch(loadSshKeysPanel());
421 export const loadMyAccount = handleFirstTimeLoad(
422 (dispatch: Dispatch<any>) => {
423 dispatch(loadMyAccountPanel());
426 export const loadKeepServices = handleFirstTimeLoad(
427 async (dispatch: Dispatch<any>) => {
428 await dispatch(loadKeepServicesPanel());
431 export const loadUsers = handleFirstTimeLoad(
432 async (dispatch: Dispatch<any>) => {
433 await dispatch(loadUsersPanel());
434 dispatch(setBreadcrumbs([{ label: 'Users' }]));
437 export const loadComputeNodes = handleFirstTimeLoad(
438 async (dispatch: Dispatch<any>) => {
439 await dispatch(loadComputeNodesPanel());
442 const finishLoadingProject = (project: GroupContentsResource | string) =>
443 async (dispatch: Dispatch<any>) => {
444 const uuid = typeof project === 'string' ? project : project.uuid;
445 dispatch(openProjectPanel(uuid));
446 dispatch(loadDetailsPanel(uuid));
447 if (typeof project !== 'string') {
448 dispatch(updateResources([project]));
452 const loadGroupContentsResource = async (params: {
455 services: ServiceRepository
457 const filters = new FilterBuilder()
458 .addEqual('uuid', params.uuid)
460 const { items } = await params.services.groupsService.contents(params.userUuid, {
465 const resource = items.shift();
466 let handler: GroupContentsHandler;
468 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
469 ? groupContentsHandlers.TRASHED(resource)
470 : groupContentsHandlers.OWNED(resource);
472 const kind = extractUuidKind(params.uuid);
473 let resource: GroupContentsResource;
474 if (kind === ResourceKind.COLLECTION) {
475 resource = await params.services.collectionService.get(params.uuid);
476 } else if (kind === ResourceKind.PROJECT) {
477 resource = await params.services.projectService.get(params.uuid);
479 resource = await params.services.containerRequestService.get(params.uuid);
481 handler = groupContentsHandlers.SHARED(resource);
483 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
484 groupContentsHandlers.match(handler, cases);
488 const groupContentsHandlersRecord = {
489 TRASHED: ofType<GroupContentsResource>(),
490 SHARED: ofType<GroupContentsResource>(),
491 OWNED: ofType<GroupContentsResource>(),
494 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
496 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;