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