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