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';
60 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
62 export const isWorkbenchLoading = (state: RootState) => {
63 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
64 return progress ? progress.working : false;
67 const handleFirstTimeLoad = (action: any) =>
68 async (dispatch: Dispatch<any>, getState: () => RootState) => {
70 await dispatch(action);
72 if (isWorkbenchLoading(getState())) {
73 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
79 export const loadWorkbench = () =>
80 async (dispatch: Dispatch, getState: () => RootState) => {
81 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
82 const { auth, router } = getState();
83 const { user } = auth;
85 const userResource = await dispatch<any>(loadResource(user.uuid));
87 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
88 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
89 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
90 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
91 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
92 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
93 dispatch<any>(initSidePanelTree());
94 if (router.location) {
95 const match = matchRootRoute(router.location.pathname);
97 dispatch(navigateToProject(userResource.uuid));
101 dispatch(userIsNotAuthenticated);
104 dispatch(userIsNotAuthenticated);
108 export const loadFavorites = () =>
110 (dispatch: Dispatch) => {
111 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
112 dispatch<any>(loadFavoritePanel());
113 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
116 export const loadTrash = () =>
118 (dispatch: Dispatch) => {
119 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
120 dispatch<any>(loadTrashPanel());
121 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
124 export const loadProject = (uuid: string) =>
126 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
127 const userUuid = services.authService.getUuid();
128 dispatch(setIsProjectPanelTrashed(false));
130 if (userUuid !== uuid) {
131 const match = await loadGroupContentsResource({ uuid, userUuid, services });
133 OWNED: async project => {
134 await dispatch(activateSidePanelTreeItem(uuid));
135 dispatch<any>(setSidePanelBreadcrumbs(uuid));
136 dispatch(finishLoadingProject(project));
139 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
140 dispatch(activateSidePanelTreeItem(uuid));
141 dispatch(finishLoadingProject(project));
143 TRASHED: project => {
144 dispatch<any>(setTrashBreadcrumbs(uuid));
145 dispatch(setIsProjectPanelTrashed(true));
146 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
147 dispatch(finishLoadingProject(project));
151 await dispatch(activateSidePanelTreeItem(userUuid));
152 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
153 dispatch(finishLoadingProject(userUuid));
158 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
159 async (dispatch: Dispatch) => {
160 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
162 dispatch(snackbarActions.OPEN_SNACKBAR({
163 message: "Project has been successfully created.",
166 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
167 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
171 export const moveProject = (data: MoveToFormDialogData) =>
172 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
174 const oldProject = getResource(data.uuid)(getState().resources);
175 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
176 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
178 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
180 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
182 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
185 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
189 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
190 async (dispatch: Dispatch) => {
191 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
192 if (updatedProject) {
193 dispatch(snackbarActions.OPEN_SNACKBAR({
194 message: "Project has been successfully updated.",
197 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
198 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
202 export const loadCollection = (uuid: string) =>
204 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
205 const userUuid = services.authService.getUuid();
207 const match = await loadGroupContentsResource({ uuid, userUuid, services });
209 OWNED: async collection => {
210 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
211 dispatch(updateResources([collection]));
212 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
213 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
214 dispatch(loadCollectionFiles(collection.uuid));
216 SHARED: collection => {
217 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
218 dispatch(updateResources([collection]));
219 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
220 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
221 dispatch(loadCollectionFiles(collection.uuid));
223 TRASHED: collection => {
224 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
225 dispatch(updateResources([collection]));
226 dispatch(setTrashBreadcrumbs(''));
227 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
228 dispatch(loadCollectionFiles(collection.uuid));
235 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
236 async (dispatch: Dispatch) => {
237 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
239 dispatch(snackbarActions.OPEN_SNACKBAR({
240 message: "Collection has been successfully created.",
243 dispatch<any>(updateResources([collection]));
244 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
248 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
249 async (dispatch: Dispatch) => {
250 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
252 dispatch(snackbarActions.OPEN_SNACKBAR({
253 message: "Collection has been successfully updated.",
256 dispatch<any>(updateResources([collection]));
257 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
261 export const copyCollection = (data: CopyFormDialogData) =>
262 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
264 const copyToProject = getResource(data.ownerUuid)(getState().resources);
265 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
266 if (copyToProject && collection) {
267 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
268 dispatch(snackbarActions.OPEN_SNACKBAR({
269 message: 'Collection has been copied.',
271 kind: SnackbarKind.SUCCESS,
272 link: collection.ownerUuid
276 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
280 export const moveCollection = (data: MoveToFormDialogData) =>
281 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
283 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
284 dispatch<any>(updateResources([collection]));
285 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
286 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
288 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
292 export const loadProcess = (uuid: string) =>
294 async (dispatch: Dispatch, getState: () => RootState) => {
295 dispatch<any>(loadProcessPanel(uuid));
296 const process = await dispatch<any>(processesActions.loadProcess(uuid));
297 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
298 dispatch<any>(setProcessBreadcrumbs(uuid));
299 dispatch(loadDetailsPanel(uuid));
302 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
303 async (dispatch: Dispatch) => {
305 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
307 dispatch(snackbarActions.OPEN_SNACKBAR({
308 message: "Process has been successfully updated.",
311 dispatch<any>(updateResources([process]));
312 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
315 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
319 export const moveProcess = (data: MoveToFormDialogData) =>
320 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
322 const process = await dispatch<any>(processMoveActions.moveProcess(data));
323 dispatch<any>(updateResources([process]));
324 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
325 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
327 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
331 export const copyProcess = (data: CopyFormDialogData) =>
332 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
334 const process = await dispatch<any>(processCopyActions.copyProcess(data));
335 dispatch<any>(updateResources([process]));
336 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
337 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
339 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
343 export const loadProcessLog = (uuid: string) =>
345 async (dispatch: Dispatch) => {
346 const process = await dispatch<any>(processesActions.loadProcess(uuid));
347 dispatch<any>(setProcessBreadcrumbs(uuid));
348 dispatch<any>(initProcessLogsPanel(uuid));
349 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
352 export const resourceIsNotLoaded = (uuid: string) =>
353 snackbarActions.OPEN_SNACKBAR({
354 message: `Resource identified by ${uuid} is not loaded.`
357 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
358 message: 'User is not authenticated'
361 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
362 message: 'Could not load user'
365 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
366 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
367 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
368 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
369 dispatch<any>(loadProject(currentProjectPanelUuid));
373 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
374 dispatch<any>(loadSharedWithMePanel());
375 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
376 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
379 export const loadRunProcess = handleFirstTimeLoad(
380 async (dispatch: Dispatch) => {
381 await dispatch<any>(loadRunProcessPanel());
385 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
386 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
387 await dispatch(loadWorkflowPanel());
388 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
391 export const loadSearchResults = handleFirstTimeLoad(
392 async (dispatch: Dispatch<any>) => {
393 await dispatch(loadSearchResultsPanel());
396 export const loadVirtualMachines = handleFirstTimeLoad(
397 async (dispatch: Dispatch<any>) => {
398 await dispatch(loadVirtualMachinesPanel());
399 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
402 export const loadRepositories = handleFirstTimeLoad(
403 async (dispatch: Dispatch<any>) => {
404 await dispatch(loadRepositoriesPanel());
405 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
408 export const loadSshKeys = handleFirstTimeLoad(
409 async (dispatch: Dispatch<any>) => {
410 await dispatch(loadSshKeysPanel());
413 const finishLoadingProject = (project: GroupContentsResource | string) =>
414 async (dispatch: Dispatch<any>) => {
415 const uuid = typeof project === 'string' ? project : project.uuid;
416 dispatch(openProjectPanel(uuid));
417 dispatch(loadDetailsPanel(uuid));
418 if (typeof project !== 'string') {
419 dispatch(updateResources([project]));
423 const loadGroupContentsResource = async (params: {
426 services: ServiceRepository
428 const filters = new FilterBuilder()
429 .addEqual('uuid', params.uuid)
431 const { items } = await params.services.groupsService.contents(params.userUuid, {
436 const resource = items.shift();
437 let handler: GroupContentsHandler;
439 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
440 ? groupContentsHandlers.TRASHED(resource)
441 : groupContentsHandlers.OWNED(resource);
443 const kind = extractUuidKind(params.uuid);
444 let resource: GroupContentsResource;
445 if (kind === ResourceKind.COLLECTION) {
446 resource = await params.services.collectionService.get(params.uuid);
447 } else if (kind === ResourceKind.PROJECT) {
448 resource = await params.services.projectService.get(params.uuid);
450 resource = await params.services.containerRequestService.get(params.uuid);
452 handler = groupContentsHandlers.SHARED(resource);
454 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
455 groupContentsHandlers.match(handler, cases);
459 const groupContentsHandlersRecord = {
460 TRASHED: ofType<GroupContentsResource>(),
461 SHARED: ofType<GroupContentsResource>(),
462 OWNED: ofType<GroupContentsResource>(),
465 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
467 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;