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