Merge branch '17914-peer-federation-login-fix'. Closes #17914
[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     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.workflowUuid || '',
193                 menuKind: ContextMenuKind.PROCESS
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 resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
212     (dispatch: Dispatch, getState: () => RootState) => {
213         const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
214         const kind = extractUuidKind(uuid);
215         const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
216
217         const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
218         switch (kind) {
219             case ResourceKind.PROJECT:
220                 return (isAdminUser && !readonly)
221                     ? (resource && resource.groupClass !== GroupClass.FILTER)
222                         ? ContextMenuKind.PROJECT_ADMIN
223                         : ContextMenuKind.FILTER_GROUP_ADMIN
224                     : isEditable
225                         ? (resource && resource.groupClass !== GroupClass.FILTER)
226                             ? ContextMenuKind.PROJECT
227                             : ContextMenuKind.FILTER_GROUP
228                         : ContextMenuKind.READONLY_PROJECT;
229             case ResourceKind.COLLECTION:
230                 const c = getResource<CollectionResource>(uuid)(getState().resources);
231                 if (c === undefined) { return; }
232                 const isOldVersion = c.uuid !== c.currentVersionUuid;
233                 const isTrashed = c.isTrashed;
234                 return isOldVersion
235                     ? ContextMenuKind.OLD_VERSION_COLLECTION
236                     : (isTrashed && isEditable)
237                         ? ContextMenuKind.TRASHED_COLLECTION
238                         : (isAdminUser && !readonly)
239                             ? ContextMenuKind.COLLECTION_ADMIN
240                             : isEditable
241                                 ? ContextMenuKind.COLLECTION
242                                 : ContextMenuKind.READONLY_COLLECTION;
243             case ResourceKind.PROCESS:
244                 return (isAdminUser && !readonly)
245                     ? ContextMenuKind.PROCESS_ADMIN
246                     : readonly
247                         ? ContextMenuKind.READONLY_PROCESS_RESOURCE
248                         : ContextMenuKind.PROCESS_RESOURCE;
249             case ResourceKind.USER:
250                 return ContextMenuKind.ROOT_PROJECT;
251             case ResourceKind.LINK:
252                 return ContextMenuKind.LINK;
253             default:
254                 return;
255         }
256     };