1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 import { resourceIsFrozen } from 'common/frozen-resources';
25 import { ProjectResource } from 'models/project';
26 import { filterCollectionFilesBySelection } from 'store/collection-panel/collection-panel-files/collection-panel-files-state';
28 export const contextMenuActions = unionize({
29 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
30 CLOSE_CONTEXT_MENU: ofType<{}>()
33 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
35 export type ContextMenuResource = {
41 menuKind: ContextMenuKind | string;
45 workflowUuid?: string;
48 storageClassesDesired?: string[];
49 properties?: { [key: string]: string | string[] };
52 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
54 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
55 (dispatch: Dispatch) => {
56 event.preventDefault();
57 const { left, top } = event.currentTarget.getBoundingClientRect();
59 contextMenuActions.OPEN_CONTEXT_MENU({
61 x: event.clientX || left,
62 y: event.clientY || top,
69 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
70 (dispatch: Dispatch, getState: () => RootState) => {
71 const selectedCount = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).length;
72 const multiple = selectedCount > 1;
73 dispatch<any>(openContextMenu(event, {
78 kind: ResourceKind.COLLECTION,
79 menuKind: selectedCount > 0
81 ? multiple ? ContextMenuKind.COLLECTION_FILES_MULTIPLE : ContextMenuKind.COLLECTION_FILES
82 : multiple ? ContextMenuKind.READONLY_COLLECTION_FILES_MULTIPLE : ContextMenuKind.READONLY_COLLECTION_FILES
83 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
87 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
88 (dispatch: Dispatch, getState: () => RootState) => {
89 dispatch<any>(openContextMenu(event, {
91 uuid: repository.uuid,
92 ownerUuid: repository.ownerUuid,
93 kind: ResourceKind.REPOSITORY,
94 menuKind: ContextMenuKind.REPOSITORY
98 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
99 (dispatch: Dispatch, getState: () => RootState) => {
100 dispatch<any>(openContextMenu(event, {
102 uuid: repository.uuid,
103 ownerUuid: repository.ownerUuid,
104 kind: ResourceKind.VIRTUAL_MACHINE,
105 menuKind: ContextMenuKind.VIRTUAL_MACHINE
109 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
110 (dispatch: Dispatch) => {
111 dispatch<any>(openContextMenu(event, {
114 ownerUuid: sshKey.ownerUuid,
115 kind: ResourceKind.SSH_KEY,
116 menuKind: ContextMenuKind.SSH_KEY
120 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
121 (dispatch: Dispatch) => {
122 dispatch<any>(openContextMenu(event, {
124 uuid: keepService.uuid,
125 ownerUuid: keepService.ownerUuid,
126 kind: ResourceKind.KEEP_SERVICE,
127 menuKind: ContextMenuKind.KEEP_SERVICE
131 export const openApiClientAuthorizationContextMenu =
132 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
133 (dispatch: Dispatch) => {
134 dispatch<any>(openContextMenu(event, {
138 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
139 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
143 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
144 (dispatch: Dispatch, getState: () => RootState) => {
145 const res = getResource<UserResource>(projectUuid)(getState().resources);
147 dispatch<any>(openContextMenu(event, {
152 menuKind: ContextMenuKind.ROOT_PROJECT,
158 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
159 (dispatch: Dispatch, getState: () => RootState) => {
160 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
161 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
162 if (res && menuKind) {
163 dispatch<any>(openContextMenu(event, {
168 description: res.description,
169 ownerUuid: res.ownerUuid,
170 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
171 isFrozen: !!(res as ProjectResource).frozenByUuid,
176 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
177 (dispatch: Dispatch, getState: () => RootState) => {
178 if (!isSidePanelTreeCategory(id)) {
179 const kind = extractUuidKind(id);
180 if (kind === ResourceKind.USER) {
181 dispatch<any>(openRootProjectContextMenu(event, id));
182 } else if (kind === ResourceKind.PROJECT) {
183 dispatch<any>(openProjectContextMenu(event, id));
188 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
189 (dispatch: Dispatch, getState: () => RootState) => {
190 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
192 dispatch<any>(openContextMenu(event, {
194 ownerUuid: res.ownerUuid,
195 kind: ResourceKind.PROCESS,
197 description: res.description,
198 outputUuid: res.outputUuid || '',
199 workflowUuid: res.properties.template_uuid || '',
200 menuKind: ContextMenuKind.PROCESS_RESOURCE
205 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
206 (dispatch: Dispatch, getState: () => RootState) => {
208 dispatch<any>(openContextMenu(event, {
212 menuKind: ContextMenuKind.PERMISSION_EDIT,
213 ownerUuid: link.ownerUuid,
218 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) =>
219 (dispatch: Dispatch, getState: () => RootState) => {
220 dispatch<any>(openContextMenu(event, {
223 ownerUuid: user.ownerUuid,
225 menuKind: ContextMenuKind.USER
229 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
230 (dispatch: Dispatch, getState: () => RootState) => {
231 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
232 const kind = extractUuidKind(uuid);
233 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
234 const isFrozen = resourceIsFrozen(resource, getState().resources);
235 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly && !isFrozen;
238 case ResourceKind.PROJECT:
240 return isAdminUser ? ContextMenuKind.FROZEN_PROJECT_ADMIN : ContextMenuKind.FROZEN_PROJECT;
243 return (isAdminUser && !readonly)
244 ? (resource && resource.groupClass !== GroupClass.FILTER)
245 ? ContextMenuKind.PROJECT_ADMIN
246 : ContextMenuKind.FILTER_GROUP_ADMIN
248 ? (resource && resource.groupClass !== GroupClass.FILTER)
249 ? ContextMenuKind.PROJECT
250 : ContextMenuKind.FILTER_GROUP
251 : ContextMenuKind.READONLY_PROJECT;
252 case ResourceKind.COLLECTION:
253 const c = getResource<CollectionResource>(uuid)(getState().resources);
254 if (c === undefined) { return; }
255 const isOldVersion = c.uuid !== c.currentVersionUuid;
256 const isTrashed = c.isTrashed;
258 ? ContextMenuKind.OLD_VERSION_COLLECTION
259 : (isTrashed && isEditable)
260 ? ContextMenuKind.TRASHED_COLLECTION
261 : (isAdminUser && isEditable)
262 ? ContextMenuKind.COLLECTION_ADMIN
264 ? ContextMenuKind.COLLECTION
265 : ContextMenuKind.READONLY_COLLECTION;
266 case ResourceKind.PROCESS:
267 return (isAdminUser && isEditable)
268 ? ContextMenuKind.PROCESS_ADMIN
270 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
271 : ContextMenuKind.PROCESS_RESOURCE;
272 case ResourceKind.USER:
273 return ContextMenuKind.ROOT_PROJECT;
274 case ResourceKind.LINK:
275 return ContextMenuKind.LINK;
276 case ResourceKind.WORKFLOW:
277 return ContextMenuKind.WORKFLOW;
283 export const openSearchResultsContextMenu = (event: React.MouseEvent<HTMLElement>, uuid: string) =>
284 (dispatch: Dispatch, getState: () => RootState) => {
285 const res = getResource<Resource>(uuid)(getState().resources);
287 dispatch<any>(openContextMenu(event, {
292 menuKind: ContextMenuKind.SEARCH_RESULTS,