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