Merge remote-tracking branch 'origin/main' into 18692-frozen-projects-workbench-support
[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, Resource } 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 import { LinkResource } from 'models/link';
24 import { resourceIsFrozen } from 'common/frozen-resources';
25 import { ProjectResource } from 'models/project';
26
27 export const contextMenuActions = unionize({
28     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
29     CLOSE_CONTEXT_MENU: ofType<{}>()
30 });
31
32 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
33
34 export type ContextMenuResource = {
35     name: string;
36     uuid: string;
37     ownerUuid: string;
38     description?: string;
39     kind: ResourceKind,
40     menuKind: ContextMenuKind | string;
41     isTrashed?: boolean;
42     isEditable?: boolean;
43     outputUuid?: string;
44     workflowUuid?: string;
45     isAdmin?: boolean;
46     isFrozen?: boolean;
47     storageClassesDesired?: string[];
48     properties?: { [key: string]: string | string[] };
49 };
50
51 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
52
53 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
54     (dispatch: Dispatch) => {
55         event.preventDefault();
56         const { left, top } = event.currentTarget.getBoundingClientRect();
57         dispatch(
58             contextMenuActions.OPEN_CONTEXT_MENU({
59                 position: {
60                     x: event.clientX || left,
61                     y: event.clientY || top,
62                 },
63                 resource
64             })
65         );
66     };
67
68 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
69     (dispatch: Dispatch, getState: () => RootState) => {
70         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
71         dispatch<any>(openContextMenu(event, {
72             name: '',
73             uuid: '',
74             ownerUuid: '',
75             description: '',
76             kind: ResourceKind.COLLECTION,
77             menuKind: isCollectionFileSelected
78                 ? isWritable
79                     ? ContextMenuKind.COLLECTION_FILES
80                     : ContextMenuKind.READONLY_COLLECTION_FILES
81                 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
82         }));
83     };
84
85 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
86     (dispatch: Dispatch, getState: () => RootState) => {
87         dispatch<any>(openContextMenu(event, {
88             name: '',
89             uuid: repository.uuid,
90             ownerUuid: repository.ownerUuid,
91             kind: ResourceKind.REPOSITORY,
92             menuKind: ContextMenuKind.REPOSITORY
93         }));
94     };
95
96 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
97     (dispatch: Dispatch, getState: () => RootState) => {
98         dispatch<any>(openContextMenu(event, {
99             name: '',
100             uuid: repository.uuid,
101             ownerUuid: repository.ownerUuid,
102             kind: ResourceKind.VIRTUAL_MACHINE,
103             menuKind: ContextMenuKind.VIRTUAL_MACHINE
104         }));
105     };
106
107 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
108     (dispatch: Dispatch) => {
109         dispatch<any>(openContextMenu(event, {
110             name: '',
111             uuid: sshKey.uuid,
112             ownerUuid: sshKey.ownerUuid,
113             kind: ResourceKind.SSH_KEY,
114             menuKind: ContextMenuKind.SSH_KEY
115         }));
116     };
117
118 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
119     (dispatch: Dispatch) => {
120         dispatch<any>(openContextMenu(event, {
121             name: '',
122             uuid: keepService.uuid,
123             ownerUuid: keepService.ownerUuid,
124             kind: ResourceKind.KEEP_SERVICE,
125             menuKind: ContextMenuKind.KEEP_SERVICE
126         }));
127     };
128
129 export const openApiClientAuthorizationContextMenu =
130     (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
131         (dispatch: Dispatch) => {
132             dispatch<any>(openContextMenu(event, {
133                 name: '',
134                 uuid: resourceUuid,
135                 ownerUuid: '',
136                 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
137                 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
138             }));
139         };
140
141 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
142     (dispatch: Dispatch, getState: () => RootState) => {
143         const res = getResource<UserResource>(projectUuid)(getState().resources);
144         if (res) {
145             dispatch<any>(openContextMenu(event, {
146                 name: '',
147                 uuid: res.uuid,
148                 ownerUuid: res.uuid,
149                 kind: res.kind,
150                 menuKind: ContextMenuKind.ROOT_PROJECT,
151                 isTrashed: false
152             }));
153         }
154     };
155
156 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
157     (dispatch: Dispatch, getState: () => RootState) => {
158         const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
159         const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
160         if (res && menuKind) {
161             dispatch<any>(openContextMenu(event, {
162                 name: res.name,
163                 uuid: res.uuid,
164                 kind: res.kind,
165                 menuKind,
166                 description: res.description,
167                 ownerUuid: res.ownerUuid,
168                 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
169                 isFrozen: !!(res as ProjectResource).frozenByUuid,
170             }));
171         }
172     };
173
174 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
175     (dispatch: Dispatch, getState: () => RootState) => {
176         if (!isSidePanelTreeCategory(id)) {
177             const kind = extractUuidKind(id);
178             if (kind === ResourceKind.USER) {
179                 dispatch<any>(openRootProjectContextMenu(event, id));
180             } else if (kind === ResourceKind.PROJECT) {
181                 dispatch<any>(openProjectContextMenu(event, id));
182             }
183         }
184     };
185
186 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
187     (dispatch: Dispatch, getState: () => RootState) => {
188         const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
189         if (res) {
190             dispatch<any>(openContextMenu(event, {
191                 uuid: res.uuid,
192                 ownerUuid: res.ownerUuid,
193                 kind: ResourceKind.PROCESS,
194                 name: res.name,
195                 description: res.description,
196                 outputUuid: res.outputUuid || '',
197                 workflowUuid: res.properties.template_uuid || '',
198                 menuKind: ContextMenuKind.PROCESS_RESOURCE
199             }));
200         }
201     };
202
203 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
204     (dispatch: Dispatch, getState: () => RootState) => {
205         if (link) {
206             dispatch<any>(openContextMenu(event, {
207                 name: link.name,
208                 uuid: link.uuid,
209                 kind: link.kind,
210                 menuKind: ContextMenuKind.PERMISSION_EDIT,
211                 ownerUuid: link.ownerUuid,
212             }));
213         }
214     };
215
216 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) =>
217     (dispatch: Dispatch, getState: () => RootState) => {
218         dispatch<any>(openContextMenu(event, {
219             name: '',
220             uuid: user.uuid,
221             ownerUuid: user.ownerUuid,
222             kind: user.kind,
223             menuKind: ContextMenuKind.USER
224         }));
225     };
226
227 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
228     (dispatch: Dispatch, getState: () => RootState) => {
229         const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
230         const kind = extractUuidKind(uuid);
231         const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
232         const isFrozen = resourceIsFrozen(resource, getState().resources);
233         const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly && !isFrozen;
234
235         switch (kind) {
236             case ResourceKind.PROJECT:
237                 if (isFrozen) {
238                     return isAdminUser ? ContextMenuKind.FROZEN_PROJECT_ADMIN : ContextMenuKind.FROZEN_PROJECT;
239                 }
240
241                 return (isAdminUser && !readonly)
242                     ? (resource && resource.groupClass !== GroupClass.FILTER)
243                         ? ContextMenuKind.PROJECT_ADMIN
244                         : ContextMenuKind.FILTER_GROUP_ADMIN
245                     : isEditable
246                         ? (resource && resource.groupClass !== GroupClass.FILTER)
247                             ? ContextMenuKind.PROJECT
248                             : ContextMenuKind.FILTER_GROUP
249                         : ContextMenuKind.READONLY_PROJECT;
250             case ResourceKind.COLLECTION:
251                 const c = getResource<CollectionResource>(uuid)(getState().resources);
252                 if (c === undefined) { return; }
253                 const isOldVersion = c.uuid !== c.currentVersionUuid;
254                 const isTrashed = c.isTrashed;
255                 return isOldVersion
256                     ? ContextMenuKind.OLD_VERSION_COLLECTION
257                     : (isTrashed && isEditable)
258                         ? ContextMenuKind.TRASHED_COLLECTION
259                         : (isAdminUser && isEditable)
260                             ? ContextMenuKind.COLLECTION_ADMIN
261                             : isEditable
262                                 ? ContextMenuKind.COLLECTION
263                                 : ContextMenuKind.READONLY_COLLECTION;
264             case ResourceKind.PROCESS:
265                 return (isAdminUser && isEditable)
266                     ? ContextMenuKind.PROCESS_ADMIN
267                     : readonly
268                         ? ContextMenuKind.READONLY_PROCESS_RESOURCE
269                         : ContextMenuKind.PROCESS_RESOURCE;
270             case ResourceKind.USER:
271                 return ContextMenuKind.ROOT_PROJECT;
272             case ResourceKind.LINK:
273                 return ContextMenuKind.LINK;
274             case ResourceKind.WORKFLOW:
275                 return ContextMenuKind.WORKFLOW;
276             default:
277                 return;
278         }
279     };
280
281 export const openSearchResultsContextMenu = (event: React.MouseEvent<HTMLElement>, uuid: string) =>
282     (dispatch: Dispatch, getState: () => RootState) => {
283         const res = getResource<Resource>(uuid)(getState().resources);
284         if (res) {
285             dispatch<any>(openContextMenu(event, {
286                 name: '',
287                 uuid: res.uuid,
288                 ownerUuid: '',
289                 kind: res.kind,
290                 menuKind: ContextMenuKind.SEARCH_RESULTS,
291             }));
292         }
293     };