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;
44 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
46 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
47 (dispatch: Dispatch) => {
48 event.preventDefault();
49 const { left, top } = event.currentTarget.getBoundingClientRect();
51 contextMenuActions.OPEN_CONTEXT_MENU({
53 x: event.clientX || left,
54 y: event.clientY || top,
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, {
68 kind: ResourceKind.COLLECTION,
69 menuKind: isCollectionFileSelected
71 ? ContextMenuKind.COLLECTION_FILES
72 : ContextMenuKind.READONLY_COLLECTION_FILES
73 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
77 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
78 (dispatch: Dispatch, getState: () => RootState) => {
79 dispatch<any>(openContextMenu(event, {
81 uuid: repository.uuid,
82 ownerUuid: repository.ownerUuid,
83 kind: ResourceKind.REPOSITORY,
84 menuKind: ContextMenuKind.REPOSITORY
88 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
89 (dispatch: Dispatch, getState: () => RootState) => {
90 dispatch<any>(openContextMenu(event, {
92 uuid: repository.uuid,
93 ownerUuid: repository.ownerUuid,
94 kind: ResourceKind.VIRTUAL_MACHINE,
95 menuKind: ContextMenuKind.VIRTUAL_MACHINE
99 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
100 (dispatch: Dispatch) => {
101 dispatch<any>(openContextMenu(event, {
104 ownerUuid: sshKey.ownerUuid,
105 kind: ResourceKind.SSH_KEY,
106 menuKind: ContextMenuKind.SSH_KEY
110 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
111 (dispatch: Dispatch) => {
112 dispatch<any>(openContextMenu(event, {
114 uuid: keepService.uuid,
115 ownerUuid: keepService.ownerUuid,
116 kind: ResourceKind.KEEP_SERVICE,
117 menuKind: ContextMenuKind.KEEP_SERVICE
121 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
122 (dispatch: Dispatch) => {
123 dispatch<any>(openContextMenu(event, {
127 kind: ResourceKind.NODE,
128 menuKind: ContextMenuKind.NODE
132 export const openApiClientAuthorizationContextMenu =
133 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
134 (dispatch: Dispatch) => {
135 dispatch<any>(openContextMenu(event, {
139 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
140 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
144 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
145 (dispatch: Dispatch, getState: () => RootState) => {
146 const res = getResource<UserResource>(projectUuid)(getState().resources);
148 dispatch<any>(openContextMenu(event, {
153 menuKind: ContextMenuKind.ROOT_PROJECT,
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, {
169 ownerUuid: res.ownerUuid,
170 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
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));
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);
191 dispatch<any>(openContextMenu(event, {
193 ownerUuid: res.ownerUuid,
194 kind: ResourceKind.PROCESS,
196 description: res.description,
197 outputUuid: res.outputUuid || '',
198 workflowUuid: res.properties.workflowUuid || '',
199 menuKind: ContextMenuKind.PROCESS
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);
210 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
212 case ResourceKind.PROJECT:
213 return (isAdminUser && !readonly)
214 ? (resource && resource.groupClass !== GroupClass.FILTER)
215 ? ContextMenuKind.PROJECT_ADMIN
216 : ContextMenuKind.FILTER_GROUP_ADMIN
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;
228 ? ContextMenuKind.OLD_VERSION_COLLECTION
229 : (isTrashed && isEditable)
230 ? ContextMenuKind.TRASHED_COLLECTION
231 : (isAdminUser && !readonly)
232 ? ContextMenuKind.COLLECTION_ADMIN
234 ? ContextMenuKind.COLLECTION
235 : ContextMenuKind.READONLY_COLLECTION;
236 case ResourceKind.PROCESS:
237 return (isAdminUser && !readonly)
238 ? ContextMenuKind.PROCESS_ADMIN
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;