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 { loadLinkPanel, linkPanelActions } from '~/store/link-panel/link-panel-actions';
63 import { loadComputeNodesPanel, computeNodesActions } from '~/store/compute-nodes/compute-nodes-actions';
64 import { linkPanelColumns } from '~/views/link-panel/link-panel-root';
65 import { userPanelColumns } from '~/views/user-panel/user-panel';
66 import { computeNodePanelColumns } from '~/views/compute-node-panel/compute-node-panel-root';
67 import { loadApiClientAuthorizationsPanel } from '~/store/api-client-authorizations/api-client-authorizations-actions';
69 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
71 export const isWorkbenchLoading = (state: RootState) => {
72 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
73 return progress ? progress.working : false;
76 const handleFirstTimeLoad = (action: any) =>
77 async (dispatch: Dispatch<any>, getState: () => RootState) => {
79 await dispatch(action);
81 if (isWorkbenchLoading(getState())) {
82 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
87 export const loadWorkbench = () =>
88 async (dispatch: Dispatch, getState: () => RootState) => {
89 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
90 const { auth, router } = getState();
91 const { user } = auth;
93 const userResource = await dispatch<any>(loadResource(user.uuid));
95 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
96 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
97 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
98 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
99 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
100 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
101 dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
102 dispatch(linkPanelActions.SET_COLUMNS({ columns: linkPanelColumns }));
103 dispatch(computeNodesActions.SET_COLUMNS({ columns: computeNodePanelColumns }));
104 dispatch<any>(initSidePanelTree());
105 if (router.location) {
106 const match = matchRootRoute(router.location.pathname);
108 dispatch(navigateToProject(userResource.uuid));
112 dispatch(userIsNotAuthenticated);
115 dispatch(userIsNotAuthenticated);
119 export const loadFavorites = () =>
121 (dispatch: Dispatch) => {
122 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
123 dispatch<any>(loadFavoritePanel());
124 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
127 export const loadTrash = () =>
129 (dispatch: Dispatch) => {
130 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
131 dispatch<any>(loadTrashPanel());
132 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
135 export const loadProject = (uuid: string) =>
137 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
138 const userUuid = services.authService.getUuid();
139 dispatch(setIsProjectPanelTrashed(false));
141 if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
142 // Load another users home projects
143 dispatch(finishLoadingProject(uuid));
144 } else if (userUuid !== uuid) {
145 const match = await loadGroupContentsResource({ uuid, userUuid, services });
147 OWNED: async project => {
148 await dispatch(activateSidePanelTreeItem(uuid));
149 dispatch<any>(setSidePanelBreadcrumbs(uuid));
150 dispatch(finishLoadingProject(project));
153 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
154 dispatch(activateSidePanelTreeItem(uuid));
155 dispatch(finishLoadingProject(project));
157 TRASHED: project => {
158 dispatch<any>(setTrashBreadcrumbs(uuid));
159 dispatch(setIsProjectPanelTrashed(true));
160 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
161 dispatch(finishLoadingProject(project));
165 await dispatch(activateSidePanelTreeItem(userUuid));
166 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
167 dispatch(finishLoadingProject(userUuid));
172 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
173 async (dispatch: Dispatch) => {
174 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
176 dispatch(snackbarActions.OPEN_SNACKBAR({
177 message: "Project has been successfully created.",
180 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
181 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
185 export const moveProject = (data: MoveToFormDialogData) =>
186 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
188 const oldProject = getResource(data.uuid)(getState().resources);
189 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
190 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
192 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
194 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
196 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
199 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
203 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
204 async (dispatch: Dispatch) => {
205 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
206 if (updatedProject) {
207 dispatch(snackbarActions.OPEN_SNACKBAR({
208 message: "Project has been successfully updated.",
211 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
212 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
216 export const loadCollection = (uuid: string) =>
218 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
219 const userUuid = services.authService.getUuid();
221 const match = await loadGroupContentsResource({ uuid, userUuid, services });
223 OWNED: async collection => {
224 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
225 dispatch(updateResources([collection]));
226 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
227 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
228 dispatch(loadCollectionFiles(collection.uuid));
230 SHARED: collection => {
231 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
232 dispatch(updateResources([collection]));
233 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
234 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
235 dispatch(loadCollectionFiles(collection.uuid));
237 TRASHED: collection => {
238 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
239 dispatch(updateResources([collection]));
240 dispatch(setTrashBreadcrumbs(''));
241 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
242 dispatch(loadCollectionFiles(collection.uuid));
249 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
250 async (dispatch: Dispatch) => {
251 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
253 dispatch(snackbarActions.OPEN_SNACKBAR({
254 message: "Collection has been successfully created.",
257 dispatch<any>(updateResources([collection]));
258 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
262 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
263 async (dispatch: Dispatch) => {
264 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
266 dispatch(snackbarActions.OPEN_SNACKBAR({
267 message: "Collection has been successfully updated.",
270 dispatch<any>(updateResources([collection]));
271 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
275 export const copyCollection = (data: CopyFormDialogData) =>
276 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
278 const copyToProject = getResource(data.ownerUuid)(getState().resources);
279 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
280 if (copyToProject && collection) {
281 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
282 dispatch(snackbarActions.OPEN_SNACKBAR({
283 message: 'Collection has been copied.',
285 kind: SnackbarKind.SUCCESS,
286 link: collection.ownerUuid
290 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
294 export const moveCollection = (data: MoveToFormDialogData) =>
295 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
297 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
298 dispatch<any>(updateResources([collection]));
299 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
300 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
302 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
306 export const loadProcess = (uuid: string) =>
308 async (dispatch: Dispatch, getState: () => RootState) => {
309 dispatch<any>(loadProcessPanel(uuid));
310 const process = await dispatch<any>(processesActions.loadProcess(uuid));
311 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
312 dispatch<any>(setProcessBreadcrumbs(uuid));
313 dispatch(loadDetailsPanel(uuid));
316 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
317 async (dispatch: Dispatch) => {
319 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
321 dispatch(snackbarActions.OPEN_SNACKBAR({
322 message: "Process has been successfully updated.",
325 dispatch<any>(updateResources([process]));
326 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
329 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
333 export const moveProcess = (data: MoveToFormDialogData) =>
334 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
336 const process = await dispatch<any>(processMoveActions.moveProcess(data));
337 dispatch<any>(updateResources([process]));
338 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
339 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
341 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
345 export const copyProcess = (data: CopyFormDialogData) =>
346 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
348 const process = await dispatch<any>(processCopyActions.copyProcess(data));
349 dispatch<any>(updateResources([process]));
350 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
351 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
353 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
357 export const loadProcessLog = (uuid: string) =>
359 async (dispatch: Dispatch) => {
360 const process = await dispatch<any>(processesActions.loadProcess(uuid));
361 dispatch<any>(setProcessBreadcrumbs(uuid));
362 dispatch<any>(initProcessLogsPanel(uuid));
363 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
366 export const resourceIsNotLoaded = (uuid: string) =>
367 snackbarActions.OPEN_SNACKBAR({
368 message: `Resource identified by ${uuid} is not loaded.`
371 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
372 message: 'User is not authenticated'
375 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
376 message: 'Could not load user'
379 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
380 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
381 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
382 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
383 dispatch<any>(loadProject(currentProjectPanelUuid));
387 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
388 dispatch<any>(loadSharedWithMePanel());
389 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
390 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
393 export const loadRunProcess = handleFirstTimeLoad(
394 async (dispatch: Dispatch) => {
395 await dispatch<any>(loadRunProcessPanel());
399 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
400 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
401 await dispatch(loadWorkflowPanel());
402 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
405 export const loadSearchResults = handleFirstTimeLoad(
406 async (dispatch: Dispatch<any>) => {
407 await dispatch(loadSearchResultsPanel());
410 export const loadLinks = handleFirstTimeLoad(
411 async (dispatch: Dispatch<any>) => {
412 await dispatch(loadLinkPanel());
415 export const loadVirtualMachines = handleFirstTimeLoad(
416 async (dispatch: Dispatch<any>) => {
417 await dispatch(loadVirtualMachinesPanel());
418 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
421 export const loadRepositories = handleFirstTimeLoad(
422 async (dispatch: Dispatch<any>) => {
423 await dispatch(loadRepositoriesPanel());
424 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
427 export const loadSshKeys = handleFirstTimeLoad(
428 async (dispatch: Dispatch<any>) => {
429 await dispatch(loadSshKeysPanel());
432 export const loadMyAccount = handleFirstTimeLoad(
433 (dispatch: Dispatch<any>) => {
434 dispatch(loadMyAccountPanel());
437 export const loadKeepServices = handleFirstTimeLoad(
438 async (dispatch: Dispatch<any>) => {
439 await dispatch(loadKeepServicesPanel());
442 export const loadUsers = handleFirstTimeLoad(
443 async (dispatch: Dispatch<any>) => {
444 await dispatch(loadUsersPanel());
445 dispatch(setBreadcrumbs([{ label: 'Users' }]));
448 export const loadComputeNodes = handleFirstTimeLoad(
449 async (dispatch: Dispatch<any>) => {
450 await dispatch(loadComputeNodesPanel());
453 export const loadApiClientAuthorizations = handleFirstTimeLoad(
454 async (dispatch: Dispatch<any>) => {
455 await dispatch(loadApiClientAuthorizationsPanel());
458 const finishLoadingProject = (project: GroupContentsResource | string) =>
459 async (dispatch: Dispatch<any>) => {
460 const uuid = typeof project === 'string' ? project : project.uuid;
461 dispatch(openProjectPanel(uuid));
462 dispatch(loadDetailsPanel(uuid));
463 if (typeof project !== 'string') {
464 dispatch(updateResources([project]));
468 const loadGroupContentsResource = async (params: {
471 services: ServiceRepository
473 const filters = new FilterBuilder()
474 .addEqual('uuid', params.uuid)
476 const { items } = await params.services.groupsService.contents(params.userUuid, {
481 const resource = items.shift();
482 let handler: GroupContentsHandler;
484 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
485 ? groupContentsHandlers.TRASHED(resource)
486 : groupContentsHandlers.OWNED(resource);
488 const kind = extractUuidKind(params.uuid);
489 let resource: GroupContentsResource;
490 if (kind === ResourceKind.COLLECTION) {
491 resource = await params.services.collectionService.get(params.uuid);
492 } else if (kind === ResourceKind.PROJECT) {
493 resource = await params.services.projectService.get(params.uuid);
495 resource = await params.services.containerRequestService.get(params.uuid);
497 handler = groupContentsHandlers.SHARED(resource);
499 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
500 groupContentsHandlers.match(handler, cases);
504 const groupContentsHandlersRecord = {
505 TRASHED: ofType<GroupContentsResource>(),
506 SHARED: ofType<GroupContentsResource>(),
507 OWNED: ofType<GroupContentsResource>(),
510 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
512 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;