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 { loadRepositoriesPanel } from '~/store/repositories/repositories-actions';
59 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
61 export const isWorkbenchLoading = (state: RootState) => {
62 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
63 return progress ? progress.working : false;
66 const handleFirstTimeLoad = (action: any) =>
67 async (dispatch: Dispatch<any>, getState: () => RootState) => {
69 await dispatch(action);
71 if (isWorkbenchLoading(getState())) {
72 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
78 export const loadWorkbench = () =>
79 async (dispatch: Dispatch, getState: () => RootState) => {
80 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
81 const { auth, router } = getState();
82 const { user } = auth;
84 const userResource = await dispatch<any>(loadResource(user.uuid));
86 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
87 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
88 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
89 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
90 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
91 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
92 dispatch<any>(initSidePanelTree());
93 if (router.location) {
94 const match = matchRootRoute(router.location.pathname);
96 dispatch(navigateToProject(userResource.uuid));
100 dispatch(userIsNotAuthenticated);
103 dispatch(userIsNotAuthenticated);
107 export const loadFavorites = () =>
109 (dispatch: Dispatch) => {
110 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
111 dispatch<any>(loadFavoritePanel());
112 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
115 export const loadTrash = () =>
117 (dispatch: Dispatch) => {
118 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
119 dispatch<any>(loadTrashPanel());
120 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
123 export const loadProject = (uuid: string) =>
125 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
126 const userUuid = services.authService.getUuid();
127 dispatch(setIsProjectPanelTrashed(false));
129 if (userUuid !== uuid) {
130 const match = await loadGroupContentsResource({ uuid, userUuid, services });
132 OWNED: async project => {
133 await dispatch(activateSidePanelTreeItem(uuid));
134 dispatch<any>(setSidePanelBreadcrumbs(uuid));
135 dispatch(finishLoadingProject(project));
138 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
139 dispatch(activateSidePanelTreeItem(uuid));
140 dispatch(finishLoadingProject(project));
142 TRASHED: project => {
143 dispatch<any>(setTrashBreadcrumbs(uuid));
144 dispatch(setIsProjectPanelTrashed(true));
145 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
146 dispatch(finishLoadingProject(project));
150 await dispatch(activateSidePanelTreeItem(userUuid));
151 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
152 dispatch(finishLoadingProject(userUuid));
157 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
158 async (dispatch: Dispatch) => {
159 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
161 dispatch(snackbarActions.OPEN_SNACKBAR({
162 message: "Project has been successfully created.",
165 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
166 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
170 export const moveProject = (data: MoveToFormDialogData) =>
171 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
173 const oldProject = getResource(data.uuid)(getState().resources);
174 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
175 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
177 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
179 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
181 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
184 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
188 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
189 async (dispatch: Dispatch) => {
190 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
191 if (updatedProject) {
192 dispatch(snackbarActions.OPEN_SNACKBAR({
193 message: "Project has been successfully updated.",
196 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
197 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
201 export const loadCollection = (uuid: string) =>
203 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
204 const userUuid = services.authService.getUuid();
206 const match = await loadGroupContentsResource({ uuid, userUuid, services });
208 OWNED: async collection => {
209 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
210 dispatch(updateResources([collection]));
211 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
212 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
213 dispatch(loadCollectionFiles(collection.uuid));
215 SHARED: collection => {
216 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
217 dispatch(updateResources([collection]));
218 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
219 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
220 dispatch(loadCollectionFiles(collection.uuid));
222 TRASHED: collection => {
223 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
224 dispatch(updateResources([collection]));
225 dispatch(setTrashBreadcrumbs(''));
226 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
227 dispatch(loadCollectionFiles(collection.uuid));
234 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
235 async (dispatch: Dispatch) => {
236 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
238 dispatch(snackbarActions.OPEN_SNACKBAR({
239 message: "Collection has been successfully created.",
242 dispatch<any>(updateResources([collection]));
243 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
247 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
248 async (dispatch: Dispatch) => {
249 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
251 dispatch(snackbarActions.OPEN_SNACKBAR({
252 message: "Collection has been successfully updated.",
255 dispatch<any>(updateResources([collection]));
256 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
260 export const copyCollection = (data: CopyFormDialogData) =>
261 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
263 const copyToProject = getResource(data.ownerUuid)(getState().resources);
264 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
265 if (copyToProject && collection) {
266 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
267 dispatch(snackbarActions.OPEN_SNACKBAR({
268 message: 'Collection has been copied.',
270 kind: SnackbarKind.SUCCESS,
271 link: collection.ownerUuid
275 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
279 export const moveCollection = (data: MoveToFormDialogData) =>
280 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
282 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
283 dispatch<any>(updateResources([collection]));
284 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
285 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
287 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
291 export const loadProcess = (uuid: string) =>
293 async (dispatch: Dispatch, getState: () => RootState) => {
294 dispatch<any>(loadProcessPanel(uuid));
295 const process = await dispatch<any>(processesActions.loadProcess(uuid));
296 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
297 dispatch<any>(setProcessBreadcrumbs(uuid));
298 dispatch(loadDetailsPanel(uuid));
301 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
302 async (dispatch: Dispatch) => {
304 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
306 dispatch(snackbarActions.OPEN_SNACKBAR({
307 message: "Process has been successfully updated.",
310 dispatch<any>(updateResources([process]));
311 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
314 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
318 export const moveProcess = (data: MoveToFormDialogData) =>
319 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
321 const process = await dispatch<any>(processMoveActions.moveProcess(data));
322 dispatch<any>(updateResources([process]));
323 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
324 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
326 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
330 export const copyProcess = (data: CopyFormDialogData) =>
331 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
333 const process = await dispatch<any>(processCopyActions.copyProcess(data));
334 dispatch<any>(updateResources([process]));
335 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
336 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
338 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
342 export const loadProcessLog = (uuid: string) =>
344 async (dispatch: Dispatch) => {
345 const process = await dispatch<any>(processesActions.loadProcess(uuid));
346 dispatch<any>(setProcessBreadcrumbs(uuid));
347 dispatch<any>(initProcessLogsPanel(uuid));
348 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
351 export const resourceIsNotLoaded = (uuid: string) =>
352 snackbarActions.OPEN_SNACKBAR({
353 message: `Resource identified by ${uuid} is not loaded.`
356 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
357 message: 'User is not authenticated'
360 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
361 message: 'Could not load user'
364 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
365 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
366 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
367 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
368 dispatch<any>(loadProject(currentProjectPanelUuid));
372 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
373 dispatch<any>(loadSharedWithMePanel());
374 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
375 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
378 export const loadRunProcess = handleFirstTimeLoad(
379 async (dispatch: Dispatch) => {
380 await dispatch<any>(loadRunProcessPanel());
384 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
385 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
386 await dispatch(loadWorkflowPanel());
387 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
390 export const loadSearchResults = handleFirstTimeLoad(
391 async (dispatch: Dispatch<any>) => {
392 await dispatch(loadSearchResultsPanel());
395 export const loadRepositories = handleFirstTimeLoad(
396 async (dispatch: Dispatch<any>) => {
397 await dispatch(loadRepositoriesPanel());
398 dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
401 export const loadSshKeys = handleFirstTimeLoad(
402 async (dispatch: Dispatch<any>) => {
403 await dispatch(loadSshKeysPanel());
406 const finishLoadingProject = (project: GroupContentsResource | string) =>
407 async (dispatch: Dispatch<any>) => {
408 const uuid = typeof project === 'string' ? project : project.uuid;
409 dispatch(openProjectPanel(uuid));
410 dispatch(loadDetailsPanel(uuid));
411 if (typeof project !== 'string') {
412 dispatch(updateResources([project]));
416 const loadGroupContentsResource = async (params: {
419 services: ServiceRepository
421 const filters = new FilterBuilder()
422 .addEqual('uuid', params.uuid)
424 const { items } = await params.services.groupsService.contents(params.userUuid, {
429 const resource = items.shift();
430 let handler: GroupContentsHandler;
432 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
433 ? groupContentsHandlers.TRASHED(resource)
434 : groupContentsHandlers.OWNED(resource);
436 const kind = extractUuidKind(params.uuid);
437 let resource: GroupContentsResource;
438 if (kind === ResourceKind.COLLECTION) {
439 resource = await params.services.collectionService.get(params.uuid);
440 } else if (kind === ResourceKind.PROJECT) {
441 resource = await params.services.projectService.get(params.uuid);
443 resource = await params.services.containerRequestService.get(params.uuid);
445 handler = groupContentsHandlers.SHARED(resource);
447 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
448 groupContentsHandlers.match(handler, cases);
452 const groupContentsHandlersRecord = {
453 TRASHED: ofType<GroupContentsResource>(),
454 SHARED: ofType<GroupContentsResource>(),
455 OWNED: ofType<GroupContentsResource>(),
458 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
460 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;