bb404b88963b9fbd358db6cdba1aa82a6e7bbf20
[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
17 export const contextMenuActions = unionize({
18     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
19     CLOSE_CONTEXT_MENU: ofType<{}>()
20 });
21
22 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
23
24 export type ContextMenuResource = {
25     name: string;
26     uuid: string;
27     ownerUuid: string;
28     description?: string;
29     kind: ResourceKind,
30     menuKind: ContextMenuKind;
31     isTrashed?: boolean;
32 };
33
34 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
35     (dispatch: Dispatch) => {
36         event.preventDefault();
37         dispatch(
38             contextMenuActions.OPEN_CONTEXT_MENU({
39                 position: { x: event.clientX, y: event.clientY },
40                 resource
41             })
42         );
43     };
44
45 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
46     (dispatch: Dispatch, getState: () => RootState) => {
47         const res = getResource<UserResource>(projectUuid)(getState().resources);
48         if (res) {
49             dispatch<any>(openContextMenu(event, {
50                 name: '',
51                 uuid: res.uuid,
52                 ownerUuid: res.uuid,
53                 kind: res.kind,
54                 menuKind: ContextMenuKind.ROOT_PROJECT,
55                 isTrashed: false
56             }));
57         }
58     };
59
60 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
61     (dispatch: Dispatch, getState: () => RootState) => {
62         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
63         if (res) {
64             dispatch<any>(openContextMenu(event, {
65                 name: res.name,
66                 uuid: res.uuid,
67                 kind: res.kind,
68                 menuKind: ContextMenuKind.PROJECT,
69                 ownerUuid: res.ownerUuid,
70                 isTrashed: res.isTrashed
71             }));
72         }
73     };
74
75 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
76     (dispatch: Dispatch, getState: () => RootState) => {
77         if (!isSidePanelTreeCategory(id)) {
78             const kind = extractUuidKind(id);
79             if (kind === ResourceKind.USER) {
80                 dispatch<any>(openRootProjectContextMenu(event, id));
81             } else if (kind === ResourceKind.PROJECT) {
82                 dispatch<any>(openProjectContextMenu(event, id));
83             }
84         }
85     };
86
87 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>) =>
88     (dispatch: Dispatch, getState: () => RootState) => {
89         const { location } = getState().router;
90         const pathname = location ? location.pathname : '';
91         const match = matchProcessRoute(pathname); 
92         const uuid = match ? match.params.id : '';
93         const resource = {
94             uuid,
95             ownerUuid: '',
96             kind: ResourceKind.PROCESS,
97             name: '',
98             description: '',
99             menuKind: ContextMenuKind.PROCESS
100         };
101         dispatch<any>(openContextMenu(event, resource));
102     };
103
104 export const resourceKindToContextMenuKind = (uuid: string) => {
105     const kind = extractUuidKind(uuid);
106     switch (kind) {
107         case ResourceKind.PROJECT:
108             return ContextMenuKind.PROJECT;
109         case ResourceKind.COLLECTION:
110             return ContextMenuKind.COLLECTION_RESOURCE;
111         case ResourceKind.USER:
112             return ContextMenuKind.ROOT_PROJECT;
113         default:
114             return;
115     }
116 };