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