Merge branch 'master' into 14452-my-account
[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 import { SshKeyResource } from '~/models/ssh-key';
18 import { VirtualMachinesResource } from '~/models/virtual-machines';
19 import { KeepServiceResource } from '~/models/keep-services';
20 import { NodeResource } from '~/models/node';
21
22 export const contextMenuActions = unionize({
23     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
24     CLOSE_CONTEXT_MENU: ofType<{}>()
25 });
26
27 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
28
29 export type ContextMenuResource = {
30     name: string;
31     uuid: string;
32     ownerUuid: string;
33     description?: string;
34     kind: ResourceKind,
35     menuKind: ContextMenuKind;
36     isTrashed?: boolean;
37     index?: number
38 };
39
40 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
41
42 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
43     (dispatch: Dispatch) => {
44         event.preventDefault();
45         const { left, top } = event.currentTarget.getBoundingClientRect();
46         dispatch(
47             contextMenuActions.OPEN_CONTEXT_MENU({
48                 position: {
49                     x: event.clientX || left,
50                     y: event.clientY || top,
51                 },
52                 resource
53             })
54         );
55     };
56
57 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
58     (dispatch: Dispatch, getState: () => RootState) => {
59         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
60         dispatch<any>(openContextMenu(event, {
61             name: '',
62             uuid: '',
63             ownerUuid: '',
64             kind: ResourceKind.COLLECTION,
65             menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
66         }));
67     };
68
69 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
70     (dispatch: Dispatch, getState: () => RootState) => {
71         dispatch<any>(openContextMenu(event, {
72             name: '',
73             uuid: repository.uuid,
74             ownerUuid: repository.ownerUuid,
75             kind: ResourceKind.REPOSITORY,
76             menuKind: ContextMenuKind.REPOSITORY
77         }));
78     };
79
80 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
81     (dispatch: Dispatch, getState: () => RootState) => {
82         dispatch<any>(openContextMenu(event, {
83             name: '',
84             uuid: repository.uuid,
85             ownerUuid: repository.ownerUuid,
86             kind: ResourceKind.VIRTUAL_MACHINE,
87             menuKind: ContextMenuKind.VIRTUAL_MACHINE
88         }));
89     };
90
91 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
92     (dispatch: Dispatch) => {
93         dispatch<any>(openContextMenu(event, {
94             name: '',
95             uuid: sshKey.uuid,
96             ownerUuid: sshKey.ownerUuid,
97             kind: ResourceKind.SSH_KEY,
98             menuKind: ContextMenuKind.SSH_KEY
99         }));
100     };
101
102 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
103     (dispatch: Dispatch) => {
104         dispatch<any>(openContextMenu(event, {
105             name: '',
106             uuid: keepService.uuid,
107             ownerUuid: keepService.ownerUuid,
108             kind: ResourceKind.KEEP_SERVICE,
109             menuKind: ContextMenuKind.KEEP_SERVICE
110         }));
111     };
112
113 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, computeNode: NodeResource) =>
114     (dispatch: Dispatch) => {
115         dispatch<any>(openContextMenu(event, {
116             name: '',
117             uuid: computeNode.uuid,
118             ownerUuid: computeNode.ownerUuid,
119             kind: ResourceKind.NODE,
120             menuKind: ContextMenuKind.NODE
121         }));
122     };
123
124 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
125     (dispatch: Dispatch, getState: () => RootState) => {
126         const res = getResource<UserResource>(projectUuid)(getState().resources);
127         if (res) {
128             dispatch<any>(openContextMenu(event, {
129                 name: '',
130                 uuid: res.uuid,
131                 ownerUuid: res.uuid,
132                 kind: res.kind,
133                 menuKind: ContextMenuKind.ROOT_PROJECT,
134                 isTrashed: false
135             }));
136         }
137     };
138
139 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
140     (dispatch: Dispatch, getState: () => RootState) => {
141         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
142         if (res) {
143             dispatch<any>(openContextMenu(event, {
144                 name: res.name,
145                 uuid: res.uuid,
146                 kind: res.kind,
147                 menuKind: ContextMenuKind.PROJECT,
148                 ownerUuid: res.ownerUuid,
149                 isTrashed: res.isTrashed
150             }));
151         }
152     };
153
154 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
155     (dispatch: Dispatch, getState: () => RootState) => {
156         if (!isSidePanelTreeCategory(id)) {
157             const kind = extractUuidKind(id);
158             if (kind === ResourceKind.USER) {
159                 dispatch<any>(openRootProjectContextMenu(event, id));
160             } else if (kind === ResourceKind.PROJECT) {
161                 dispatch<any>(openProjectContextMenu(event, id));
162             }
163         }
164     };
165
166 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
167     (dispatch: Dispatch, getState: () => RootState) => {
168         const resource = {
169             uuid: process.containerRequest.uuid,
170             ownerUuid: process.containerRequest.ownerUuid,
171             kind: ResourceKind.PROCESS,
172             name: process.containerRequest.name,
173             description: process.containerRequest.description,
174             menuKind: ContextMenuKind.PROCESS
175         };
176         dispatch<any>(openContextMenu(event, resource));
177     };
178
179 export const resourceKindToContextMenuKind = (uuid: string) => {
180     const kind = extractUuidKind(uuid);
181     switch (kind) {
182         case ResourceKind.PROJECT:
183             return ContextMenuKind.PROJECT;
184         case ResourceKind.COLLECTION:
185             return ContextMenuKind.COLLECTION_RESOURCE;
186         case ResourceKind.PROCESS:
187             return ContextMenuKind.PROCESS_RESOURCE;
188         case ResourceKind.USER:
189             return ContextMenuKind.ROOT_PROJECT;
190         default:
191             return;
192     }
193 };