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 { linkPanelColumns } from '~/views/link-panel/link-panel-root';
64 import { userPanelColumns } from '~/views/user-panel/user-panel';
65 import { loadComputeNodesPanel } from '~/store/compute-nodes/compute-nodes-actions';
66 import { loadApiClientAuthorizationsPanel } from '~/store/api-client-authorizations/api-client-authorizations-actions';
68 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
70 export const isWorkbenchLoading = (state: RootState) => {
71 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
72 return progress ? progress.working : false;
75 const handleFirstTimeLoad = (action: any) =>
76 async (dispatch: Dispatch<any>, getState: () => RootState) => {
78 await dispatch(action);
80 if (isWorkbenchLoading(getState())) {
81 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
86 export const loadWorkbench = () =>
87 async (dispatch: Dispatch, getState: () => RootState) => {
88 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
89 const { auth, router } = getState();
90 const { user } = auth;
92 const userResource = await dispatch<any>(loadResource(user.uuid));
94 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
95 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
96 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
97 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
98 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
99 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
100 dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
101 dispatch(linkPanelActions.SET_COLUMNS({ columns: linkPanelColumns }));
102 dispatch<any>(initSidePanelTree());
103 if (router.location) {
104 const match = matchRootRoute(router.location.pathname);
106 dispatch(navigateToProject(userResource.uuid));
110 dispatch(userIsNotAuthenticated);
113 dispatch(userIsNotAuthenticated);
117 export const loadFavorites = () =>
119 (dispatch: Dispatch) => {
120 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
121 dispatch<any>(loadFavoritePanel());
122 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
125 export const loadTrash = () =>
127 (dispatch: Dispatch) => {
128 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
129 dispatch<any>(loadTrashPanel());
130 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
133 export const loadProject = (uuid: string) =>
135 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
136 const userUuid = services.authService.getUuid();
137 dispatch(setIsProjectPanelTrashed(false));
139 if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
140 // Load another users home projects
141 dispatch(finishLoadingProject(uuid));
142 } else if (userUuid !== uuid) {
143 const match = await loadGroupContentsResource({ uuid, userUuid, services });
145 OWNED: async project => {
146 await dispatch(activateSidePanelTreeItem(uuid));
147 dispatch<any>(setSidePanelBreadcrumbs(uuid));
148 dispatch(finishLoadingProject(project));
151 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
152 dispatch(activateSidePanelTreeItem(uuid));
153 dispatch(finishLoadingProject(project));
155 TRASHED: project => {
156 dispatch<any>(setTrashBreadcrumbs(uuid));
157 dispatch(setIsProjectPanelTrashed(true));
158 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
159 dispatch(finishLoadingProject(project));
163 await dispatch(activateSidePanelTreeItem(userUuid));
164 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
165 dispatch(finishLoadingProject(userUuid));
170 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
171 async (dispatch: Dispatch) => {
172 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
174 dispatch(snackbarActions.OPEN_SNACKBAR({
175 message: "Project has been successfully created.",
178 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
179 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
183 export const moveProject = (data: MoveToFormDialogData) =>
184 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
186 const oldProject = getResource(data.uuid)(getState().resources);
187 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
188 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
190 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
192 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
194 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
197 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
201 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
202 async (dispatch: Dispatch) => {
203 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
204 if (updatedProject) {
205 dispatch(snackbarActions.OPEN_SNACKBAR({
206 message: "Project has been successfully updated.",
209 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
210 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
214 export const loadCollection = (uuid: string) =>
216 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
217 const userUuid = services.authService.getUuid();
219 const match = await loadGroupContentsResource({ uuid, userUuid, services });
221 OWNED: async collection => {
222 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
223 dispatch(updateResources([collection]));
224 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
225 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
226 dispatch(loadCollectionFiles(collection.uuid));
228 SHARED: collection => {
229 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
230 dispatch(updateResources([collection]));
231 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
232 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
233 dispatch(loadCollectionFiles(collection.uuid));
235 TRASHED: collection => {
236 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
237 dispatch(updateResources([collection]));
238 dispatch(setTrashBreadcrumbs(''));
239 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
240 dispatch(loadCollectionFiles(collection.uuid));
247 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
248 async (dispatch: Dispatch) => {
249 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
251 dispatch(snackbarActions.OPEN_SNACKBAR({
252 message: "Collection has been successfully created.",
255 dispatch<any>(updateResources([collection]));
256 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
260 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
261 async (dispatch: Dispatch) => {
262 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
264 dispatch(snackbarActions.OPEN_SNACKBAR({
265 message: "Collection has been successfully updated.",
268 dispatch<any>(updateResources([collection]));
269 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
273 export const copyCollection = (data: CopyFormDialogData) =>
274 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
276 const copyToProject = getResource(data.ownerUuid)(getState().resources);
277 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
278 if (copyToProject && collection) {
279 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
280 dispatch(snackbarActions.OPEN_SNACKBAR({
281 message: 'Collection has been copied.',
283 kind: SnackbarKind.SUCCESS,
284 link: collection.ownerUuid
288 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
292 export const moveCollection = (data: MoveToFormDialogData) =>
293 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
295 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
296 dispatch<any>(updateResources([collection]));
297 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
298 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
300 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
304 export const loadProcess = (uuid: string) =>
306 async (dispatch: Dispatch, getState: () => RootState) => {
307 dispatch<any>(loadProcessPanel(uuid));
308 const process = await dispatch<any>(processesActions.loadProcess(uuid));
309 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
310 dispatch<any>(setProcessBreadcrumbs(uuid));
311 dispatch(loadDetailsPanel(uuid));
314 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
315 async (dispatch: Dispatch) => {
317 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
319 dispatch(snackbarActions.OPEN_SNACKBAR({
320 message: "Process has been successfully updated.",
323 dispatch<any>(updateResources([process]));
324 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
327 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
331 export const moveProcess = (data: MoveToFormDialogData) =>
332 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
334 const process = await dispatch<any>(processMoveActions.moveProcess(data));
335 dispatch<any>(updateResources([process]));
336 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
337 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
339 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
343 export const copyProcess = (data: CopyFormDialogData) =>
344 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
346 const process = await dispatch<any>(processCopyActions.copyProcess(data));
347 dispatch<any>(updateResources([process]));
348 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
349 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
351 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
355 export const loadProcessLog = (uuid: string) =>
357 async (dispatch: Dispatch) => {
358 const process = await dispatch<any>(processesActions.loadProcess(uuid));
359 dispatch<any>(setProcessBreadcrumbs(uuid));
360 dispatch<any>(initProcessLogsPanel(uuid));
361 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
364 export const resourceIsNotLoaded = (uuid: string) =>
365 snackbarActions.OPEN_SNACKBAR({
366 message: `Resource identified by ${uuid} is not loaded.`
369 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
370 message: 'User is not authenticated'
373 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
374 message: 'Could not load user'
377 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
378 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
379 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
380 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
381 dispatch<any>(loadProject(currentProjectPanelUuid));
385 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
386 dispatch<any>(loadSharedWithMePanel());
387 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
388 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
391 export const loadRunProcess = handleFirstTimeLoad(
392 async (dispatch: Dispatch) => {
393 await dispatch<any>(loadRunProcessPanel());
397 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
398 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
399 await dispatch(loadWorkflowPanel());
400 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
403 export const loadSearchResults = handleFirstTimeLoad(
404 async (dispatch: Dispatch<any>) => {
405 await dispatch(loadSearchResultsPanel());
408 export const loadLinks = handleFirstTimeLoad(
409 async (dispatch: Dispatch<any>) => {
410 await dispatch(loadLinkPanel());
413 export const loadVirtualMachines = handleFirstTimeLoad(
414 async (dispatch: Dispatch<any>) => {
415 await dispatch(loadVirtualMachinesPanel());
416 dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
419 export const loadRepositories = handleFirstTimeLoad(
420 async (dispatch: Dispatch<any>) => {
421 await dispatch(loadRepositoriesPanel());
422 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
425 export const loadSshKeys = handleFirstTimeLoad(
426 async (dispatch: Dispatch<any>) => {
427 await dispatch(loadSshKeysPanel());
430 export const loadMyAccount = handleFirstTimeLoad(
431 (dispatch: Dispatch<any>) => {
432 dispatch(loadMyAccountPanel());
435 export const loadKeepServices = handleFirstTimeLoad(
436 async (dispatch: Dispatch<any>) => {
437 await dispatch(loadKeepServicesPanel());
440 export const loadUsers = handleFirstTimeLoad(
441 async (dispatch: Dispatch<any>) => {
442 await dispatch(loadUsersPanel());
443 dispatch(setBreadcrumbs([{ label: 'Users' }]));
446 export const loadComputeNodes = handleFirstTimeLoad(
447 async (dispatch: Dispatch<any>) => {
448 await dispatch(loadComputeNodesPanel());
451 export const loadApiClientAuthorizations = handleFirstTimeLoad(
452 async (dispatch: Dispatch<any>) => {
453 await dispatch(loadApiClientAuthorizationsPanel());
456 const finishLoadingProject = (project: GroupContentsResource | string) =>
457 async (dispatch: Dispatch<any>) => {
458 const uuid = typeof project === 'string' ? project : project.uuid;
459 dispatch(openProjectPanel(uuid));
460 dispatch(loadDetailsPanel(uuid));
461 if (typeof project !== 'string') {
462 dispatch(updateResources([project]));
466 const loadGroupContentsResource = async (params: {
469 services: ServiceRepository
471 const filters = new FilterBuilder()
472 .addEqual('uuid', params.uuid)
474 const { items } = await params.services.groupsService.contents(params.userUuid, {
479 const resource = items.shift();
480 let handler: GroupContentsHandler;
482 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
483 ? groupContentsHandlers.TRASHED(resource)
484 : groupContentsHandlers.OWNED(resource);
486 const kind = extractUuidKind(params.uuid);
487 let resource: GroupContentsResource;
488 if (kind === ResourceKind.COLLECTION) {
489 resource = await params.services.collectionService.get(params.uuid);
490 } else if (kind === ResourceKind.PROJECT) {
491 resource = await params.services.projectService.get(params.uuid);
493 resource = await params.services.containerRequestService.get(params.uuid);
495 handler = groupContentsHandlers.SHARED(resource);
497 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
498 groupContentsHandlers.match(handler, cases);
502 const groupContentsHandlersRecord = {
503 TRASHED: ofType<GroupContentsResource>(),
504 SHARED: ofType<GroupContentsResource>(),
505 OWNED: ofType<GroupContentsResource>(),
508 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
510 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;