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