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';
23 import { getProjectPanelCurrentUuid } from '~/store/project-panel/project-panel-action';
25 export const contextMenuActions = unionize({
26 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
27 CLOSE_CONTEXT_MENU: ofType<{}>()
30 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
32 export type ContextMenuResource = {
38 menuKind: ContextMenuKind;
42 workflowUuid?: 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, {
69 kind: ResourceKind.COLLECTION,
70 menuKind: isCollectionFileSelected
72 ? ContextMenuKind.COLLECTION_FILES
73 : ContextMenuKind.READONLY_COLLECTION_FILES
74 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
78 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
79 (dispatch: Dispatch, getState: () => RootState) => {
80 dispatch<any>(openContextMenu(event, {
82 uuid: repository.uuid,
83 ownerUuid: repository.ownerUuid,
84 kind: ResourceKind.REPOSITORY,
85 menuKind: ContextMenuKind.REPOSITORY
89 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
90 (dispatch: Dispatch, getState: () => RootState) => {
91 dispatch<any>(openContextMenu(event, {
93 uuid: repository.uuid,
94 ownerUuid: repository.ownerUuid,
95 kind: ResourceKind.VIRTUAL_MACHINE,
96 menuKind: ContextMenuKind.VIRTUAL_MACHINE
100 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
101 (dispatch: Dispatch) => {
102 dispatch<any>(openContextMenu(event, {
105 ownerUuid: sshKey.ownerUuid,
106 kind: ResourceKind.SSH_KEY,
107 menuKind: ContextMenuKind.SSH_KEY
111 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
112 (dispatch: Dispatch) => {
113 dispatch<any>(openContextMenu(event, {
115 uuid: keepService.uuid,
116 ownerUuid: keepService.ownerUuid,
117 kind: ResourceKind.KEEP_SERVICE,
118 menuKind: ContextMenuKind.KEEP_SERVICE
122 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
123 (dispatch: Dispatch) => {
124 dispatch<any>(openContextMenu(event, {
128 kind: ResourceKind.NODE,
129 menuKind: ContextMenuKind.NODE
133 export const openApiClientAuthorizationContextMenu =
134 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
135 (dispatch: Dispatch) => {
136 dispatch<any>(openContextMenu(event, {
140 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
141 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
145 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
146 (dispatch: Dispatch, getState: () => RootState) => {
147 const res = getResource<UserResource>(projectUuid)(getState().resources);
149 dispatch<any>(openContextMenu(event, {
154 menuKind: ContextMenuKind.ROOT_PROJECT,
160 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
161 (dispatch: Dispatch, getState: () => RootState) => {
162 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
163 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
164 if (res && menuKind) {
165 dispatch<any>(openContextMenu(event, {
170 ownerUuid: res.ownerUuid,
171 isTrashed: ('isTrashed' in res) ? res.isTrashed: false,
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.workflowUuid || '',
200 menuKind: ContextMenuKind.PROCESS
205 export const resourceUuidToContextMenuKind = (uuid: string) =>
206 (dispatch: Dispatch, getState: () => RootState) => {
207 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
208 const kind = extractUuidKind(uuid);
209 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
210 // When viewing the contents of a filter group, all contents should be treated as read only.
211 let inFilterGroup = false;
212 const projectUuid = getProjectPanelCurrentUuid(getState());
213 if (projectUuid !== undefined) {
214 const project = getResource<GroupResource>(projectUuid)(getState().resources);
216 if (project.groupClass === GroupClass.FILTER) {
217 inFilterGroup = true;
221 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !inFilterGroup;
224 case ResourceKind.PROJECT:
225 return (isAdminUser && !inFilterGroup)
226 ? (resource && resource.groupClass === GroupClass.PROJECT)
227 ? ContextMenuKind.PROJECT_ADMIN
228 : ContextMenuKind.READONLY_PROJECT
230 ? ContextMenuKind.PROJECT
231 : ContextMenuKind.READONLY_PROJECT;
232 case ResourceKind.COLLECTION:
233 const c = getResource<CollectionResource>(uuid)(getState().resources);
234 if (c === undefined) { return; }
235 const isOldVersion = c.uuid !== c.currentVersionUuid;
236 const isTrashed = c.isTrashed;
238 ? ContextMenuKind.OLD_VERSION_COLLECTION
239 : (isTrashed && isEditable)
240 ? ContextMenuKind.TRASHED_COLLECTION
241 : (isAdminUser && !inFilterGroup)
242 ? ContextMenuKind.COLLECTION_ADMIN
244 ? ContextMenuKind.COLLECTION
245 : ContextMenuKind.READONLY_COLLECTION;
246 case ResourceKind.PROCESS:
247 return (isAdminUser && !inFilterGroup)
248 ? ContextMenuKind.PROCESS_ADMIN
250 ? ContextMenuKind.PROCESS_RESOURCE
251 : ContextMenuKind.READONLY_PROCESS_RESOURCE;
252 case ResourceKind.USER:
253 return ContextMenuKind.ROOT_PROJECT;
254 case ResourceKind.LINK:
255 return ContextMenuKind.LINK;