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 { ProjectResource } from '~/models/project';
12 import { UserResource } from '~/models/user';
13 import { isSidePanelTreeCategory } from '~/store/side-panel-tree/side-panel-tree-actions';
14 import { extractUuidKind, ResourceKind, EditableResource } from '~/models/resource';
15 import { Process } from '~/store/processes/process';
16 import { RepositoryResource } from '~/models/repositories';
17 import { SshKeyResource } from '~/models/ssh-key';
18 import { VirtualMachinesResource } from '~/models/virtual-machines';
19 import { KeepServiceResource } from '~/models/keep-services';
20 import { ProcessResource } from '~/models/process';
22 export const contextMenuActions = unionize({
23 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
24 CLOSE_CONTEXT_MENU: ofType<{}>()
27 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
29 export type ContextMenuResource = {
35 menuKind: ContextMenuKind;
39 workflowUuid?: string;
42 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
44 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
45 (dispatch: Dispatch) => {
46 event.preventDefault();
47 const { left, top } = event.currentTarget.getBoundingClientRect();
49 contextMenuActions.OPEN_CONTEXT_MENU({
51 x: event.clientX || left,
52 y: event.clientY || top,
59 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
60 (dispatch: Dispatch, getState: () => RootState) => {
61 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
62 dispatch<any>(openContextMenu(event, {
66 kind: ResourceKind.COLLECTION,
67 menuKind: isCollectionFileSelected
69 ? ContextMenuKind.COLLECTION_FILES
70 : ContextMenuKind.READONLY_COLLECTION_FILES
71 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
75 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
76 (dispatch: Dispatch, getState: () => RootState) => {
77 dispatch<any>(openContextMenu(event, {
79 uuid: repository.uuid,
80 ownerUuid: repository.ownerUuid,
81 kind: ResourceKind.REPOSITORY,
82 menuKind: ContextMenuKind.REPOSITORY
86 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
87 (dispatch: Dispatch, getState: () => RootState) => {
88 dispatch<any>(openContextMenu(event, {
90 uuid: repository.uuid,
91 ownerUuid: repository.ownerUuid,
92 kind: ResourceKind.VIRTUAL_MACHINE,
93 menuKind: ContextMenuKind.VIRTUAL_MACHINE
97 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
98 (dispatch: Dispatch) => {
99 dispatch<any>(openContextMenu(event, {
102 ownerUuid: sshKey.ownerUuid,
103 kind: ResourceKind.SSH_KEY,
104 menuKind: ContextMenuKind.SSH_KEY
108 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
109 (dispatch: Dispatch) => {
110 dispatch<any>(openContextMenu(event, {
112 uuid: keepService.uuid,
113 ownerUuid: keepService.ownerUuid,
114 kind: ResourceKind.KEEP_SERVICE,
115 menuKind: ContextMenuKind.KEEP_SERVICE
119 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
120 (dispatch: Dispatch) => {
121 dispatch<any>(openContextMenu(event, {
125 kind: ResourceKind.NODE,
126 menuKind: ContextMenuKind.NODE
130 export const openApiClientAuthorizationContextMenu =
131 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
132 (dispatch: Dispatch) => {
133 dispatch<any>(openContextMenu(event, {
137 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
138 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
142 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
143 (dispatch: Dispatch, getState: () => RootState) => {
144 const res = getResource<UserResource>(projectUuid)(getState().resources);
146 dispatch<any>(openContextMenu(event, {
151 menuKind: ContextMenuKind.ROOT_PROJECT,
157 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
158 (dispatch: Dispatch, getState: () => RootState) => {
159 const { isAdmin, uuid: userUuid } = getState().auth.user!;
160 const res = getResourceWithEditableStatus<ProjectResource & EditableResource>(resourceUuid, userUuid)(getState().resources);
161 const menuKind = resourceKindToContextMenuKind(resourceUuid, isAdmin, (res || {} as EditableResource).isEditable);
162 if (res && menuKind) {
163 dispatch<any>(openContextMenu(event, {
168 ownerUuid: res.ownerUuid,
169 isTrashed: res.isTrashed
174 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
175 (dispatch: Dispatch, getState: () => RootState) => {
176 if (!isSidePanelTreeCategory(id)) {
177 const kind = extractUuidKind(id);
178 if (kind === ResourceKind.USER) {
179 dispatch<any>(openRootProjectContextMenu(event, id));
180 } else if (kind === ResourceKind.PROJECT) {
181 dispatch<any>(openProjectContextMenu(event, id));
186 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
187 (dispatch: Dispatch, getState: () => RootState) => {
188 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
190 dispatch<any>(openContextMenu(event, {
192 ownerUuid: res.ownerUuid,
193 kind: ResourceKind.PROCESS,
195 description: res.description,
196 outputUuid: res.outputUuid || '',
197 workflowUuid: res.properties.workflowUuid || '',
198 menuKind: ContextMenuKind.PROCESS
203 export const resourceKindToContextMenuKind = (uuid: string, isAdmin?: boolean, isEditable?: boolean) => {
204 const kind = extractUuidKind(uuid);
206 case ResourceKind.PROJECT:
209 ? ContextMenuKind.PROJECT
210 : ContextMenuKind.READONLY_PROJECT
211 : ContextMenuKind.PROJECT_ADMIN;
212 case ResourceKind.COLLECTION:
215 ? ContextMenuKind.COLLECTION
216 : ContextMenuKind.READONLY_COLLECTION
217 : ContextMenuKind.COLLECTION_ADMIN;
218 case ResourceKind.PROCESS:
220 ? ContextMenuKind.PROCESS_RESOURCE
221 : ContextMenuKind.PROCESS_ADMIN;
222 case ResourceKind.USER:
223 return ContextMenuKind.ROOT_PROJECT;
224 case ResourceKind.LINK:
225 return ContextMenuKind.LINK;