Merge branch '14422_item_sorting_in_copy_to_project_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 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) =>
35     event.nativeEvent.detail === 0;
36 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
37     (dispatch: Dispatch) => {
38         event.preventDefault();
39         const { left, top } = event.currentTarget.getBoundingClientRect();
40         dispatch(
41             contextMenuActions.OPEN_CONTEXT_MENU({
42                 position: {
43                     x: event.clientX || left,
44                     y: event.clientY || top,
45                 },
46                 resource
47             })
48         );
49     };
50
51 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
52     (dispatch: Dispatch, getState: () => RootState) => {
53         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
54         dispatch<any>(openContextMenu(event, {
55             name: '',
56             uuid: '',
57             ownerUuid: '',
58             kind: ResourceKind.COLLECTION,
59             menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
60         }));
61     };
62
63 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
64     (dispatch: Dispatch, getState: () => RootState) => {
65         const res = getResource<UserResource>(projectUuid)(getState().resources);
66         if (res) {
67             dispatch<any>(openContextMenu(event, {
68                 name: '',
69                 uuid: res.uuid,
70                 ownerUuid: res.uuid,
71                 kind: res.kind,
72                 menuKind: ContextMenuKind.ROOT_PROJECT,
73                 isTrashed: false
74             }));
75         }
76     };
77
78 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
79     (dispatch: Dispatch, getState: () => RootState) => {
80         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
81         if (res) {
82             dispatch<any>(openContextMenu(event, {
83                 name: res.name,
84                 uuid: res.uuid,
85                 kind: res.kind,
86                 menuKind: ContextMenuKind.PROJECT,
87                 ownerUuid: res.ownerUuid,
88                 isTrashed: res.isTrashed
89             }));
90         }
91     };
92
93 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
94     (dispatch: Dispatch, getState: () => RootState) => {
95         if (!isSidePanelTreeCategory(id)) {
96             const kind = extractUuidKind(id);
97             if (kind === ResourceKind.USER) {
98                 dispatch<any>(openRootProjectContextMenu(event, id));
99             } else if (kind === ResourceKind.PROJECT) {
100                 dispatch<any>(openProjectContextMenu(event, id));
101             }
102         }
103     };
104
105 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
106     (dispatch: Dispatch, getState: () => RootState) => {
107         const resource = {
108             uuid: process.containerRequest.uuid,
109             ownerUuid: '',
110             kind: ResourceKind.PROCESS,
111             name: '',
112             description: '',
113             menuKind: ContextMenuKind.PROCESS
114         };
115         dispatch<any>(openContextMenu(event, resource));
116     };
117
118 export const resourceKindToContextMenuKind = (uuid: string) => {
119     const kind = extractUuidKind(uuid);
120     switch (kind) {
121         case ResourceKind.PROJECT:
122             return ContextMenuKind.PROJECT;
123         case ResourceKind.COLLECTION:
124             return ContextMenuKind.COLLECTION_RESOURCE;
125         case ResourceKind.PROCESS:
126             return ContextMenuKind.PROCESS_RESOURCE;
127         case ResourceKind.USER:
128             return ContextMenuKind.ROOT_PROJECT;
129         default:
130             return;
131     }
132 };