Merge branch '14130-process-command-modal'
[arvados-workbench2.git] / src / store / context-menu / context-menu-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from '~/common/unionize';
6 import { ContextMenuPosition } from "./context-menu-reducer";
7 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
8 import { Dispatch } from 'redux';
9 import { RootState } from '~/store/store';
10 import { getResource } from '../resources/resources';
11 import { ProjectResource } from '~/models/project';
12 import { UserResource } from '~/models/user';
13 import { isSidePanelTreeCategory } from '~/store/side-panel-tree/side-panel-tree-actions';
14 import { extractUuidKind, ResourceKind } from '~/models/resource';
15 import { matchProcessRoute } from '~/routes/routes';
16 import { Process } from '~/store/processes/process';
17
18 export const contextMenuActions = unionize({
19     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
20     CLOSE_CONTEXT_MENU: ofType<{}>()
21 });
22
23 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
24
25 export type ContextMenuResource = {
26     name: string;
27     uuid: string;
28     ownerUuid: string;
29     description?: string;
30     kind: ResourceKind,
31     menuKind: ContextMenuKind;
32     isTrashed?: boolean;
33 };
34
35 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
36     (dispatch: Dispatch) => {
37         event.preventDefault();
38         dispatch(
39             contextMenuActions.OPEN_CONTEXT_MENU({
40                 position: { x: event.clientX, y: event.clientY },
41                 resource
42             })
43         );
44     };
45
46 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
47     (dispatch: Dispatch, getState: () => RootState) => {
48         const res = getResource<UserResource>(projectUuid)(getState().resources);
49         if (res) {
50             dispatch<any>(openContextMenu(event, {
51                 name: '',
52                 uuid: res.uuid,
53                 ownerUuid: res.uuid,
54                 kind: res.kind,
55                 menuKind: ContextMenuKind.ROOT_PROJECT,
56                 isTrashed: false
57             }));
58         }
59     };
60
61 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
62     (dispatch: Dispatch, getState: () => RootState) => {
63         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
64         if (res) {
65             dispatch<any>(openContextMenu(event, {
66                 name: res.name,
67                 uuid: res.uuid,
68                 kind: res.kind,
69                 menuKind: ContextMenuKind.PROJECT,
70                 ownerUuid: res.ownerUuid,
71                 isTrashed: res.isTrashed
72             }));
73         }
74     };
75
76 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
77     (dispatch: Dispatch, getState: () => RootState) => {
78         if (!isSidePanelTreeCategory(id)) {
79             const kind = extractUuidKind(id);
80             if (kind === ResourceKind.USER) {
81                 dispatch<any>(openRootProjectContextMenu(event, id));
82             } else if (kind === ResourceKind.PROJECT) {
83                 dispatch<any>(openProjectContextMenu(event, id));
84             }
85         }
86     };
87
88 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
89     (dispatch: Dispatch, getState: () => RootState) => {
90         const resource = {
91             uuid: process.containerRequest.uuid,
92             ownerUuid: '',
93             kind: ResourceKind.PROCESS,
94             name: '',
95             description: '',
96             menuKind: ContextMenuKind.PROCESS
97         };
98         dispatch<any>(openContextMenu(event, resource));
99     };
100
101 export const resourceKindToContextMenuKind = (uuid: string) => {
102     const kind = extractUuidKind(uuid);
103     switch (kind) {
104         case ResourceKind.PROJECT:
105             return ContextMenuKind.PROJECT;
106         case ResourceKind.COLLECTION:
107             return ContextMenuKind.COLLECTION_RESOURCE;
108         case ResourceKind.PROCESS:
109             return ContextMenuKind.PROCESS_RESOURCE;
110         case ResourceKind.USER:
111             return ContextMenuKind.ROOT_PROJECT;
112         default:
113             return;
114     }
115 };