Merge branch '17951-remove-compute-nodes-ui' into main. Closes #17951
[arvados.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, getResourceWithEditableStatus } from '../resources/resources';
11 import { UserResource } from 'models/user';
12 import { isSidePanelTreeCategory } from 'store/side-panel-tree/side-panel-tree-actions';
13 import { extractUuidKind, ResourceKind, EditableResource } from 'models/resource';
14 import { Process } from 'store/processes/process';
15 import { RepositoryResource } from 'models/repositories';
16 import { SshKeyResource } from 'models/ssh-key';
17 import { VirtualMachinesResource } from 'models/virtual-machines';
18 import { KeepServiceResource } from 'models/keep-services';
19 import { ProcessResource } from 'models/process';
20 import { CollectionResource } from 'models/collection';
21 import { GroupClass, GroupResource } from 'models/group';
22 import { GroupContentsResource } from 'services/groups-service/groups-service';
23
24 export const contextMenuActions = unionize({
25     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
26     CLOSE_CONTEXT_MENU: ofType<{}>()
27 });
28
29 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
30
31 export type ContextMenuResource = {
32     name: string;
33     uuid: string;
34     ownerUuid: string;
35     description?: string;
36     kind: ResourceKind,
37     menuKind: ContextMenuKind | string;
38     isTrashed?: boolean;
39     isEditable?: boolean;
40     outputUuid?: string;
41     workflowUuid?: string;
42     storageClassesDesired?: string[];
43 };
44
45 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
46
47 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
48     (dispatch: Dispatch) => {
49         event.preventDefault();
50         const { left, top } = event.currentTarget.getBoundingClientRect();
51         dispatch(
52             contextMenuActions.OPEN_CONTEXT_MENU({
53                 position: {
54                     x: event.clientX || left,
55                     y: event.clientY || top,
56                 },
57                 resource
58             })
59         );
60     };
61
62 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
63     (dispatch: Dispatch, getState: () => RootState) => {
64         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
65         dispatch<any>(openContextMenu(event, {
66             name: '',
67             uuid: '',
68             ownerUuid: '',
69             description: '',
70             kind: ResourceKind.COLLECTION,
71             menuKind: isCollectionFileSelected
72                 ? isWritable
73                     ? ContextMenuKind.COLLECTION_FILES
74                     : ContextMenuKind.READONLY_COLLECTION_FILES
75                 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
76         }));
77     };
78
79 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
80     (dispatch: Dispatch, getState: () => RootState) => {
81         dispatch<any>(openContextMenu(event, {
82             name: '',
83             uuid: repository.uuid,
84             ownerUuid: repository.ownerUuid,
85             kind: ResourceKind.REPOSITORY,
86             menuKind: ContextMenuKind.REPOSITORY
87         }));
88     };
89
90 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
91     (dispatch: Dispatch, getState: () => RootState) => {
92         dispatch<any>(openContextMenu(event, {
93             name: '',
94             uuid: repository.uuid,
95             ownerUuid: repository.ownerUuid,
96             kind: ResourceKind.VIRTUAL_MACHINE,
97             menuKind: ContextMenuKind.VIRTUAL_MACHINE
98         }));
99     };
100
101 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
102     (dispatch: Dispatch) => {
103         dispatch<any>(openContextMenu(event, {
104             name: '',
105             uuid: sshKey.uuid,
106             ownerUuid: sshKey.ownerUuid,
107             kind: ResourceKind.SSH_KEY,
108             menuKind: ContextMenuKind.SSH_KEY
109         }));
110     };
111
112 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
113     (dispatch: Dispatch) => {
114         dispatch<any>(openContextMenu(event, {
115             name: '',
116             uuid: keepService.uuid,
117             ownerUuid: keepService.ownerUuid,
118             kind: ResourceKind.KEEP_SERVICE,
119             menuKind: ContextMenuKind.KEEP_SERVICE
120         }));
121     };
122
123 export const openApiClientAuthorizationContextMenu =
124     (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
125         (dispatch: Dispatch) => {
126             dispatch<any>(openContextMenu(event, {
127                 name: '',
128                 uuid: resourceUuid,
129                 ownerUuid: '',
130                 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
131                 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
132             }));
133         };
134
135 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
136     (dispatch: Dispatch, getState: () => RootState) => {
137         const res = getResource<UserResource>(projectUuid)(getState().resources);
138         if (res) {
139             dispatch<any>(openContextMenu(event, {
140                 name: '',
141                 uuid: res.uuid,
142                 ownerUuid: res.uuid,
143                 kind: res.kind,
144                 menuKind: ContextMenuKind.ROOT_PROJECT,
145                 isTrashed: false
146             }));
147         }
148     };
149
150 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
151     (dispatch: Dispatch, getState: () => RootState) => {
152         const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
153         const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
154         if (res && menuKind) {
155             dispatch<any>(openContextMenu(event, {
156                 name: res.name,
157                 uuid: res.uuid,
158                 kind: res.kind,
159                 menuKind,
160                 description: res.description,
161                 ownerUuid: res.ownerUuid,
162                 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
163             }));
164         }
165     };
166
167 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
168     (dispatch: Dispatch, getState: () => RootState) => {
169         if (!isSidePanelTreeCategory(id)) {
170             const kind = extractUuidKind(id);
171             if (kind === ResourceKind.USER) {
172                 dispatch<any>(openRootProjectContextMenu(event, id));
173             } else if (kind === ResourceKind.PROJECT) {
174                 dispatch<any>(openProjectContextMenu(event, id));
175             }
176         }
177     };
178
179 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
180     (dispatch: Dispatch, getState: () => RootState) => {
181         const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
182         if (res) {
183             dispatch<any>(openContextMenu(event, {
184                 uuid: res.uuid,
185                 ownerUuid: res.ownerUuid,
186                 kind: ResourceKind.PROCESS,
187                 name: res.name,
188                 description: res.description,
189                 outputUuid: res.outputUuid || '',
190                 workflowUuid: res.properties.workflowUuid || '',
191                 menuKind: ContextMenuKind.PROCESS
192             }));
193         }
194     };
195
196 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
197     (dispatch: Dispatch, getState: () => RootState) => {
198         const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
199         const kind = extractUuidKind(uuid);
200         const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
201
202         const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
203         switch (kind) {
204             case ResourceKind.PROJECT:
205                 return (isAdminUser && !readonly)
206                     ? (resource && resource.groupClass !== GroupClass.FILTER)
207                         ? ContextMenuKind.PROJECT_ADMIN
208                         : ContextMenuKind.FILTER_GROUP_ADMIN
209                     : isEditable
210                         ? (resource && resource.groupClass !== GroupClass.FILTER)
211                             ? ContextMenuKind.PROJECT
212                             : ContextMenuKind.FILTER_GROUP
213                         : ContextMenuKind.READONLY_PROJECT;
214             case ResourceKind.COLLECTION:
215                 const c = getResource<CollectionResource>(uuid)(getState().resources);
216                 if (c === undefined) { return; }
217                 const isOldVersion = c.uuid !== c.currentVersionUuid;
218                 const isTrashed = c.isTrashed;
219                 return isOldVersion
220                     ? ContextMenuKind.OLD_VERSION_COLLECTION
221                     : (isTrashed && isEditable)
222                         ? ContextMenuKind.TRASHED_COLLECTION
223                         : (isAdminUser && !readonly)
224                             ? ContextMenuKind.COLLECTION_ADMIN
225                             : isEditable
226                                 ? ContextMenuKind.COLLECTION
227                                 : ContextMenuKind.READONLY_COLLECTION;
228             case ResourceKind.PROCESS:
229                 return (isAdminUser && !readonly)
230                     ? ContextMenuKind.PROCESS_ADMIN
231                     : readonly
232                         ? ContextMenuKind.READONLY_PROCESS_RESOURCE
233                         : ContextMenuKind.PROCESS_RESOURCE;
234             case ResourceKind.USER:
235                 return ContextMenuKind.ROOT_PROJECT;
236             case ResourceKind.LINK:
237                 return ContextMenuKind.LINK;
238             default:
239                 return;
240         }
241     };