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 { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel-view';
44 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
45 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
46 import { ResourceKind, extractUuidKind } from '~/models/resource';
47 import { FilterBuilder } from '~/services/api/filter-builder';
48 import { GroupContentsResource } from '~/services/groups-service/groups-service';
49 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
50 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
51 import { loadCollectionFiles } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
52 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
53 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
54 import { CollectionResource } from "~/models/collection";
55 import { searchResultsPanelActions, loadSearchResultsPanel } from '~/store/search-results-panel/search-results-panel-actions';
56 import { searchResultsPanelColumns } from '~/views/search-results-panel/search-results-panel-view';
57 import { loadVirtualMachinesPanel } from '~/store/virtual-machines/virtual-machines-actions';
58 import { loadRepositoriesPanel } from '~/store/repositories/repositories-actions';
59 import { loadKeepServicesPanel } from '~/store/keep-services/keep-services-actions';
60 import { loadUsersPanel, userBindedActions } from '~/store/users/users-actions';
61 import { userPanelColumns } from '~/views/user-panel/user-panel';
63 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
65 export const isWorkbenchLoading = (state: RootState) => {
66 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
67 return progress ? progress.working : false;
70 const handleFirstTimeLoad = (action: any) =>
71 async (dispatch: Dispatch<any>, getState: () => RootState) => {
73 await dispatch(action);
75 if (isWorkbenchLoading(getState())) {
76 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
81 export const loadWorkbench = () =>
82 async (dispatch: Dispatch, getState: () => RootState) => {
83 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
84 const { auth, router } = getState();
85 const { user } = auth;
87 const userResource = await dispatch<any>(loadResource(user.uuid));
89 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
90 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
91 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
92 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
93 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
94 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
95 dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
96 dispatch<any>(initSidePanelTree());
97 if (router.location) {
98 const match = matchRootRoute(router.location.pathname);
100 dispatch(navigateToProject(userResource.uuid));
104 dispatch(userIsNotAuthenticated);
107 dispatch(userIsNotAuthenticated);
111 export const loadFavorites = () =>
113 (dispatch: Dispatch) => {
114 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
115 dispatch<any>(loadFavoritePanel());
116 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
119 export const loadTrash = () =>
121 (dispatch: Dispatch) => {
122 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
123 dispatch<any>(loadTrashPanel());
124 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
127 export const loadProject = (uuid: string) =>
129 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
130 const userUuid = services.authService.getUuid();
131 dispatch(setIsProjectPanelTrashed(false));
133 if (extractUuidKind(uuid) === ResourceKind.USER && userUuid!==uuid) {
134 // Load another users home projects
135 dispatch(finishLoadingProject(uuid));
136 } else if (userUuid !== uuid) {
137 const match = await loadGroupContentsResource({ uuid, userUuid, services });
139 OWNED: async project => {
140 await dispatch(activateSidePanelTreeItem(uuid));
141 dispatch<any>(setSidePanelBreadcrumbs(uuid));
142 dispatch(finishLoadingProject(project));
145 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
146 dispatch(activateSidePanelTreeItem(uuid));
147 dispatch(finishLoadingProject(project));
149 TRASHED: project => {
150 dispatch<any>(setTrashBreadcrumbs(uuid));
151 dispatch(setIsProjectPanelTrashed(true));
152 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
153 dispatch(finishLoadingProject(project));
157 await dispatch(activateSidePanelTreeItem(userUuid));
158 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
159 dispatch(finishLoadingProject(userUuid));
164 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
165 async (dispatch: Dispatch) => {
166 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
168 dispatch(snackbarActions.OPEN_SNACKBAR({
169 message: "Project has been successfully created.",
172 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
173 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
177 export const moveProject = (data: MoveToFormDialogData) =>
178 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
180 const oldProject = getResource(data.uuid)(getState().resources);
181 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
182 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
184 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
186 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
188 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
191 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
195 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
196 async (dispatch: Dispatch) => {
197 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
198 if (updatedProject) {
199 dispatch(snackbarActions.OPEN_SNACKBAR({
200 message: "Project has been successfully updated.",
203 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
204 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
208 export const loadCollection = (uuid: string) =>
210 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
211 const userUuid = services.authService.getUuid();
213 const match = await loadGroupContentsResource({ uuid, userUuid, services });
215 OWNED: async collection => {
216 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
217 dispatch(updateResources([collection]));
218 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
219 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
220 dispatch(loadCollectionFiles(collection.uuid));
222 SHARED: collection => {
223 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
224 dispatch(updateResources([collection]));
225 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
226 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
227 dispatch(loadCollectionFiles(collection.uuid));
229 TRASHED: collection => {
230 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
231 dispatch(updateResources([collection]));
232 dispatch(setTrashBreadcrumbs(''));
233 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
234 dispatch(loadCollectionFiles(collection.uuid));
241 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
242 async (dispatch: Dispatch) => {
243 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
245 dispatch(snackbarActions.OPEN_SNACKBAR({
246 message: "Collection has been successfully created.",
249 dispatch<any>(updateResources([collection]));
250 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
254 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
255 async (dispatch: Dispatch) => {
256 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
258 dispatch(snackbarActions.OPEN_SNACKBAR({
259 message: "Collection has been successfully updated.",
262 dispatch<any>(updateResources([collection]));
263 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
267 export const copyCollection = (data: CopyFormDialogData) =>
268 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
270 const copyToProject = getResource(data.ownerUuid)(getState().resources);
271 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
272 if (copyToProject && collection) {
273 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
274 dispatch(snackbarActions.OPEN_SNACKBAR({
275 message: 'Collection has been copied.',
277 kind: SnackbarKind.SUCCESS,
278 link: collection.ownerUuid
282 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
286 export const moveCollection = (data: MoveToFormDialogData) =>
287 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
289 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
290 dispatch<any>(updateResources([collection]));
291 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
292 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
294 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
298 export const loadProcess = (uuid: string) =>
300 async (dispatch: Dispatch, getState: () => RootState) => {
301 dispatch<any>(loadProcessPanel(uuid));
302 const process = await dispatch<any>(processesActions.loadProcess(uuid));
303 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
304 dispatch<any>(setProcessBreadcrumbs(uuid));
305 dispatch(loadDetailsPanel(uuid));
308 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
309 async (dispatch: Dispatch) => {
311 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
313 dispatch(snackbarActions.OPEN_SNACKBAR({
314 message: "Process has been successfully updated.",
317 dispatch<any>(updateResources([process]));
318 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
321 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
325 export const moveProcess = (data: MoveToFormDialogData) =>
326 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
328 const process = await dispatch<any>(processMoveActions.moveProcess(data));
329 dispatch<any>(updateResources([process]));
330 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
331 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
333 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
337 export const copyProcess = (data: CopyFormDialogData) =>
338 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
340 const process = await dispatch<any>(processCopyActions.copyProcess(data));
341 dispatch<any>(updateResources([process]));
342 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
343 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
345 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
349 export const loadProcessLog = (uuid: string) =>
351 async (dispatch: Dispatch) => {
352 const process = await dispatch<any>(processesActions.loadProcess(uuid));
353 dispatch<any>(setProcessBreadcrumbs(uuid));
354 dispatch<any>(initProcessLogsPanel(uuid));
355 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
358 export const resourceIsNotLoaded = (uuid: string) =>
359 snackbarActions.OPEN_SNACKBAR({
360 message: `Resource identified by ${uuid} is not loaded.`
363 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
364 message: 'User is not authenticated'
367 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
368 message: 'Could not load user'
371 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
372 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
373 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
374 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
375 dispatch<any>(loadProject(currentProjectPanelUuid));
379 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
380 dispatch<any>(loadSharedWithMePanel());
381 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
382 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
385 export const loadRunProcess = handleFirstTimeLoad(
386 async (dispatch: Dispatch) => {
387 await dispatch<any>(loadRunProcessPanel());
391 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
392 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
393 await dispatch(loadWorkflowPanel());
394 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
397 export const loadSearchResults = handleFirstTimeLoad(
398 async (dispatch: Dispatch<any>) => {
399 await dispatch(loadSearchResultsPanel());
402 export const loadVirtualMachines = handleFirstTimeLoad(
403 async (dispatch: Dispatch<any>) => {
404 await dispatch(loadVirtualMachinesPanel());
405 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
408 export const loadRepositories = handleFirstTimeLoad(
409 async (dispatch: Dispatch<any>) => {
410 await dispatch(loadRepositoriesPanel());
411 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
414 export const loadSshKeys = handleFirstTimeLoad(
415 async (dispatch: Dispatch<any>) => {
416 await dispatch(loadSshKeysPanel());
419 export const loadKeepServices = handleFirstTimeLoad(
420 async (dispatch: Dispatch<any>) => {
421 await dispatch(loadKeepServicesPanel());
424 export const loadUsers = handleFirstTimeLoad(
425 async (dispatch: Dispatch<any>) => {
426 await dispatch(loadUsersPanel());
427 dispatch(setBreadcrumbs([{ label: 'Users' }]));
430 const finishLoadingProject = (project: GroupContentsResource | string) =>
431 async (dispatch: Dispatch<any>) => {
432 const uuid = typeof project === 'string' ? project : project.uuid;
433 dispatch(openProjectPanel(uuid));
434 dispatch(loadDetailsPanel(uuid));
435 if (typeof project !== 'string') {
436 dispatch(updateResources([project]));
440 const loadGroupContentsResource = async (params: {
443 services: ServiceRepository
445 const filters = new FilterBuilder()
446 .addEqual('uuid', params.uuid)
448 const { items } = await params.services.groupsService.contents(params.userUuid, {
453 const resource = items.shift();
454 let handler: GroupContentsHandler;
456 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
457 ? groupContentsHandlers.TRASHED(resource)
458 : groupContentsHandlers.OWNED(resource);
460 const kind = extractUuidKind(params.uuid);
461 let resource: GroupContentsResource;
462 if (kind === ResourceKind.COLLECTION) {
463 resource = await params.services.collectionService.get(params.uuid);
464 } else if (kind === ResourceKind.PROJECT) {
465 resource = await params.services.projectService.get(params.uuid);
467 resource = await params.services.containerRequestService.get(params.uuid);
469 handler = groupContentsHandlers.SHARED(resource);
471 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
472 groupContentsHandlers.match(handler, cases);
476 const groupContentsHandlersRecord = {
477 TRASHED: ofType<GroupContentsResource>(),
478 SHARED: ofType<GroupContentsResource>(),
479 OWNED: ofType<GroupContentsResource>(),
482 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
484 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;