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 } 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';
24 export const contextMenuActions = unionize({
25 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
26 CLOSE_CONTEXT_MENU: ofType<{}>()
29 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
31 export type ContextMenuResource = {
37 menuKind: ContextMenuKind | string;
41 workflowUuid?: string;
42 storageClassesDesired?: string[];
45 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
47 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
48 (dispatch: Dispatch) => {
49 event.preventDefault();
50 const { left, top } = event.currentTarget.getBoundingClientRect();
52 contextMenuActions.OPEN_CONTEXT_MENU({
54 x: event.clientX || left,
55 y: event.clientY || top,
62 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
63 (dispatch: Dispatch, getState: () => RootState) => {
64 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
65 dispatch<any>(openContextMenu(event, {
70 kind: ResourceKind.COLLECTION,
71 menuKind: isCollectionFileSelected
73 ? ContextMenuKind.COLLECTION_FILES
74 : ContextMenuKind.READONLY_COLLECTION_FILES
75 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
79 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
80 (dispatch: Dispatch, getState: () => RootState) => {
81 dispatch<any>(openContextMenu(event, {
83 uuid: repository.uuid,
84 ownerUuid: repository.ownerUuid,
85 kind: ResourceKind.REPOSITORY,
86 menuKind: ContextMenuKind.REPOSITORY
90 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
91 (dispatch: Dispatch, getState: () => RootState) => {
92 dispatch<any>(openContextMenu(event, {
94 uuid: repository.uuid,
95 ownerUuid: repository.ownerUuid,
96 kind: ResourceKind.VIRTUAL_MACHINE,
97 menuKind: ContextMenuKind.VIRTUAL_MACHINE
101 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
102 (dispatch: Dispatch) => {
103 dispatch<any>(openContextMenu(event, {
106 ownerUuid: sshKey.ownerUuid,
107 kind: ResourceKind.SSH_KEY,
108 menuKind: ContextMenuKind.SSH_KEY
112 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
113 (dispatch: Dispatch) => {
114 dispatch<any>(openContextMenu(event, {
116 uuid: keepService.uuid,
117 ownerUuid: keepService.ownerUuid,
118 kind: ResourceKind.KEEP_SERVICE,
119 menuKind: ContextMenuKind.KEEP_SERVICE
123 export const openApiClientAuthorizationContextMenu =
124 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
125 (dispatch: Dispatch) => {
126 dispatch<any>(openContextMenu(event, {
130 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
131 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
135 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
136 (dispatch: Dispatch, getState: () => RootState) => {
137 const res = getResource<UserResource>(projectUuid)(getState().resources);
139 dispatch<any>(openContextMenu(event, {
144 menuKind: ContextMenuKind.ROOT_PROJECT,
150 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
151 (dispatch: Dispatch, getState: () => RootState) => {
152 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
153 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
154 if (res && menuKind) {
155 dispatch<any>(openContextMenu(event, {
160 description: res.description,
161 ownerUuid: res.ownerUuid,
162 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
167 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
168 (dispatch: Dispatch, getState: () => RootState) => {
169 if (!isSidePanelTreeCategory(id)) {
170 const kind = extractUuidKind(id);
171 if (kind === ResourceKind.USER) {
172 dispatch<any>(openRootProjectContextMenu(event, id));
173 } else if (kind === ResourceKind.PROJECT) {
174 dispatch<any>(openProjectContextMenu(event, id));
179 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
180 (dispatch: Dispatch, getState: () => RootState) => {
181 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
183 dispatch<any>(openContextMenu(event, {
185 ownerUuid: res.ownerUuid,
186 kind: ResourceKind.PROCESS,
188 description: res.description,
189 outputUuid: res.outputUuid || '',
190 workflowUuid: res.properties.workflowUuid || '',
191 menuKind: ContextMenuKind.PROCESS
196 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
197 (dispatch: Dispatch, getState: () => RootState) => {
198 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
199 const kind = extractUuidKind(uuid);
200 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
202 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
204 case ResourceKind.PROJECT:
205 return (isAdminUser && !readonly)
206 ? (resource && resource.groupClass !== GroupClass.FILTER)
207 ? ContextMenuKind.PROJECT_ADMIN
208 : ContextMenuKind.FILTER_GROUP_ADMIN
210 ? (resource && resource.groupClass !== GroupClass.FILTER)
211 ? ContextMenuKind.PROJECT
212 : ContextMenuKind.FILTER_GROUP
213 : ContextMenuKind.READONLY_PROJECT;
214 case ResourceKind.COLLECTION:
215 const c = getResource<CollectionResource>(uuid)(getState().resources);
216 if (c === undefined) { return; }
217 const isOldVersion = c.uuid !== c.currentVersionUuid;
218 const isTrashed = c.isTrashed;
220 ? ContextMenuKind.OLD_VERSION_COLLECTION
221 : (isTrashed && isEditable)
222 ? ContextMenuKind.TRASHED_COLLECTION
223 : (isAdminUser && !readonly)
224 ? ContextMenuKind.COLLECTION_ADMIN
226 ? ContextMenuKind.COLLECTION
227 : ContextMenuKind.READONLY_COLLECTION;
228 case ResourceKind.PROCESS:
229 return (isAdminUser && !readonly)
230 ? ContextMenuKind.PROCESS_ADMIN
232 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
233 : ContextMenuKind.PROCESS_RESOURCE;
234 case ResourceKind.USER:
235 return ContextMenuKind.ROOT_PROJECT;
236 case ResourceKind.LINK:
237 return ContextMenuKind.LINK;