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';
64 import { loadApiClientAuthorizationsPanel } from '~/store/api-client-authorizations/api-client-authorizations-actions';
66 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
68 export const isWorkbenchLoading = (state: RootState) => {
69 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
70 return progress ? progress.working : false;
73 const handleFirstTimeLoad = (action: any) =>
74 async (dispatch: Dispatch<any>, getState: () => RootState) => {
76 await dispatch(action);
78 if (isWorkbenchLoading(getState())) {
79 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
84 export const loadWorkbench = () =>
85 async (dispatch: Dispatch, getState: () => RootState) => {
86 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
87 const { auth, router } = getState();
88 const { user } = auth;
90 const userResource = await dispatch<any>(loadResource(user.uuid));
92 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
93 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
94 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
95 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
96 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
97 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
98 dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
99 dispatch<any>(initSidePanelTree());
100 if (router.location) {
101 const match = matchRootRoute(router.location.pathname);
103 dispatch(navigateToProject(userResource.uuid));
107 dispatch(userIsNotAuthenticated);
110 dispatch(userIsNotAuthenticated);
114 export const loadFavorites = () =>
116 (dispatch: Dispatch) => {
117 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
118 dispatch<any>(loadFavoritePanel());
119 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
122 export const loadTrash = () =>
124 (dispatch: Dispatch) => {
125 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
126 dispatch<any>(loadTrashPanel());
127 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
130 export const loadProject = (uuid: string) =>
132 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
133 const userUuid = services.authService.getUuid();
134 dispatch(setIsProjectPanelTrashed(false));
136 if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
137 // Load another users home projects
138 dispatch(finishLoadingProject(uuid));
139 } else if (userUuid !== uuid) {
140 const match = await loadGroupContentsResource({ uuid, userUuid, services });
142 OWNED: async project => {
143 await dispatch(activateSidePanelTreeItem(uuid));
144 dispatch<any>(setSidePanelBreadcrumbs(uuid));
145 dispatch(finishLoadingProject(project));
148 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
149 dispatch(activateSidePanelTreeItem(uuid));
150 dispatch(finishLoadingProject(project));
152 TRASHED: project => {
153 dispatch<any>(setTrashBreadcrumbs(uuid));
154 dispatch(setIsProjectPanelTrashed(true));
155 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
156 dispatch(finishLoadingProject(project));
160 await dispatch(activateSidePanelTreeItem(userUuid));
161 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
162 dispatch(finishLoadingProject(userUuid));
167 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
168 async (dispatch: Dispatch) => {
169 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
171 dispatch(snackbarActions.OPEN_SNACKBAR({
172 message: "Project has been successfully created.",
175 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
176 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
180 export const moveProject = (data: MoveToFormDialogData) =>
181 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
183 const oldProject = getResource(data.uuid)(getState().resources);
184 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
185 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
187 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
189 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
191 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
194 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
198 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
199 async (dispatch: Dispatch) => {
200 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
201 if (updatedProject) {
202 dispatch(snackbarActions.OPEN_SNACKBAR({
203 message: "Project has been successfully updated.",
206 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
207 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
211 export const loadCollection = (uuid: string) =>
213 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
214 const userUuid = services.authService.getUuid();
216 const match = await loadGroupContentsResource({ uuid, userUuid, services });
218 OWNED: async collection => {
219 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
220 dispatch(updateResources([collection]));
221 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
222 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
223 dispatch(loadCollectionFiles(collection.uuid));
225 SHARED: collection => {
226 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
227 dispatch(updateResources([collection]));
228 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
229 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
230 dispatch(loadCollectionFiles(collection.uuid));
232 TRASHED: collection => {
233 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
234 dispatch(updateResources([collection]));
235 dispatch(setTrashBreadcrumbs(''));
236 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
237 dispatch(loadCollectionFiles(collection.uuid));
244 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
245 async (dispatch: Dispatch) => {
246 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
248 dispatch(snackbarActions.OPEN_SNACKBAR({
249 message: "Collection has been successfully created.",
252 dispatch<any>(updateResources([collection]));
253 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
257 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
258 async (dispatch: Dispatch) => {
259 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
261 dispatch(snackbarActions.OPEN_SNACKBAR({
262 message: "Collection has been successfully updated.",
265 dispatch<any>(updateResources([collection]));
266 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
270 export const copyCollection = (data: CopyFormDialogData) =>
271 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
273 const copyToProject = getResource(data.ownerUuid)(getState().resources);
274 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
275 if (copyToProject && collection) {
276 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
277 dispatch(snackbarActions.OPEN_SNACKBAR({
278 message: 'Collection has been copied.',
280 kind: SnackbarKind.SUCCESS,
281 link: collection.ownerUuid
285 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
289 export const moveCollection = (data: MoveToFormDialogData) =>
290 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
292 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
293 dispatch<any>(updateResources([collection]));
294 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
295 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
297 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
301 export const loadProcess = (uuid: string) =>
303 async (dispatch: Dispatch, getState: () => RootState) => {
304 dispatch<any>(loadProcessPanel(uuid));
305 const process = await dispatch<any>(processesActions.loadProcess(uuid));
306 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
307 dispatch<any>(setProcessBreadcrumbs(uuid));
308 dispatch(loadDetailsPanel(uuid));
311 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
312 async (dispatch: Dispatch) => {
314 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
316 dispatch(snackbarActions.OPEN_SNACKBAR({
317 message: "Process has been successfully updated.",
320 dispatch<any>(updateResources([process]));
321 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
324 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
328 export const moveProcess = (data: MoveToFormDialogData) =>
329 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
331 const process = await dispatch<any>(processMoveActions.moveProcess(data));
332 dispatch<any>(updateResources([process]));
333 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
334 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
336 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
340 export const copyProcess = (data: CopyFormDialogData) =>
341 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
343 const process = await dispatch<any>(processCopyActions.copyProcess(data));
344 dispatch<any>(updateResources([process]));
345 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
346 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
348 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
352 export const loadProcessLog = (uuid: string) =>
354 async (dispatch: Dispatch) => {
355 const process = await dispatch<any>(processesActions.loadProcess(uuid));
356 dispatch<any>(setProcessBreadcrumbs(uuid));
357 dispatch<any>(initProcessLogsPanel(uuid));
358 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
361 export const resourceIsNotLoaded = (uuid: string) =>
362 snackbarActions.OPEN_SNACKBAR({
363 message: `Resource identified by ${uuid} is not loaded.`
366 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
367 message: 'User is not authenticated'
370 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
371 message: 'Could not load user'
374 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
375 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
376 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
377 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
378 dispatch<any>(loadProject(currentProjectPanelUuid));
382 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
383 dispatch<any>(loadSharedWithMePanel());
384 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
385 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
388 export const loadRunProcess = handleFirstTimeLoad(
389 async (dispatch: Dispatch) => {
390 await dispatch<any>(loadRunProcessPanel());
394 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
395 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
396 await dispatch(loadWorkflowPanel());
397 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
400 export const loadSearchResults = handleFirstTimeLoad(
401 async (dispatch: Dispatch<any>) => {
402 await dispatch(loadSearchResultsPanel());
405 export const loadVirtualMachines = handleFirstTimeLoad(
406 async (dispatch: Dispatch<any>) => {
407 await dispatch(loadVirtualMachinesPanel());
408 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
411 export const loadRepositories = handleFirstTimeLoad(
412 async (dispatch: Dispatch<any>) => {
413 await dispatch(loadRepositoriesPanel());
414 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
417 export const loadSshKeys = handleFirstTimeLoad(
418 async (dispatch: Dispatch<any>) => {
419 await dispatch(loadSshKeysPanel());
422 export const loadMyAccount = handleFirstTimeLoad(
423 (dispatch: Dispatch<any>) => {
424 dispatch(loadMyAccountPanel());
427 export const loadKeepServices = handleFirstTimeLoad(
428 async (dispatch: Dispatch<any>) => {
429 await dispatch(loadKeepServicesPanel());
432 export const loadUsers = handleFirstTimeLoad(
433 async (dispatch: Dispatch<any>) => {
434 await dispatch(loadUsersPanel());
435 dispatch(setBreadcrumbs([{ label: 'Users' }]));
438 export const loadComputeNodes = handleFirstTimeLoad(
439 async (dispatch: Dispatch<any>) => {
440 await dispatch(loadComputeNodesPanel());
443 export const loadApiClientAuthorizations = handleFirstTimeLoad(
444 async (dispatch: Dispatch<any>) => {
445 await dispatch(loadApiClientAuthorizationsPanel());
448 const finishLoadingProject = (project: GroupContentsResource | string) =>
449 async (dispatch: Dispatch<any>) => {
450 const uuid = typeof project === 'string' ? project : project.uuid;
451 dispatch(openProjectPanel(uuid));
452 dispatch(loadDetailsPanel(uuid));
453 if (typeof project !== 'string') {
454 dispatch(updateResources([project]));
458 const loadGroupContentsResource = async (params: {
461 services: ServiceRepository
463 const filters = new FilterBuilder()
464 .addEqual('uuid', params.uuid)
466 const { items } = await params.services.groupsService.contents(params.userUuid, {
471 const resource = items.shift();
472 let handler: GroupContentsHandler;
474 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
475 ? groupContentsHandlers.TRASHED(resource)
476 : groupContentsHandlers.OWNED(resource);
478 const kind = extractUuidKind(params.uuid);
479 let resource: GroupContentsResource;
480 if (kind === ResourceKind.COLLECTION) {
481 resource = await params.services.collectionService.get(params.uuid);
482 } else if (kind === ResourceKind.PROJECT) {
483 resource = await params.services.projectService.get(params.uuid);
485 resource = await params.services.containerRequestService.get(params.uuid);
487 handler = groupContentsHandlers.SHARED(resource);
489 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
490 groupContentsHandlers.match(handler, cases);
494 const groupContentsHandlersRecord = {
495 TRASHED: ofType<GroupContentsResource>(),
496 SHARED: ofType<GroupContentsResource>(),
497 OWNED: ofType<GroupContentsResource>(),
500 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
502 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;