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 } 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 } 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;
38 workflowUuid?: string;
41 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
43 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
44 (dispatch: Dispatch) => {
45 event.preventDefault();
46 const { left, top } = event.currentTarget.getBoundingClientRect();
48 contextMenuActions.OPEN_CONTEXT_MENU({
50 x: event.clientX || left,
51 y: event.clientY || top,
58 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
59 (dispatch: Dispatch, getState: () => RootState) => {
60 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
61 dispatch<any>(openContextMenu(event, {
65 kind: ResourceKind.COLLECTION,
66 menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
70 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
71 (dispatch: Dispatch, getState: () => RootState) => {
72 dispatch<any>(openContextMenu(event, {
74 uuid: repository.uuid,
75 ownerUuid: repository.ownerUuid,
76 kind: ResourceKind.REPOSITORY,
77 menuKind: ContextMenuKind.REPOSITORY
81 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
82 (dispatch: Dispatch, getState: () => RootState) => {
83 dispatch<any>(openContextMenu(event, {
85 uuid: repository.uuid,
86 ownerUuid: repository.ownerUuid,
87 kind: ResourceKind.VIRTUAL_MACHINE,
88 menuKind: ContextMenuKind.VIRTUAL_MACHINE
92 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
93 (dispatch: Dispatch) => {
94 dispatch<any>(openContextMenu(event, {
97 ownerUuid: sshKey.ownerUuid,
98 kind: ResourceKind.SSH_KEY,
99 menuKind: ContextMenuKind.SSH_KEY
103 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
104 (dispatch: Dispatch) => {
105 dispatch<any>(openContextMenu(event, {
107 uuid: keepService.uuid,
108 ownerUuid: keepService.ownerUuid,
109 kind: ResourceKind.KEEP_SERVICE,
110 menuKind: ContextMenuKind.KEEP_SERVICE
114 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
115 (dispatch: Dispatch) => {
116 dispatch<any>(openContextMenu(event, {
120 kind: ResourceKind.NODE,
121 menuKind: ContextMenuKind.NODE
125 export const openApiClientAuthorizationContextMenu =
126 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
127 (dispatch: Dispatch) => {
128 dispatch<any>(openContextMenu(event, {
132 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
133 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
137 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
138 (dispatch: Dispatch, getState: () => RootState) => {
139 const res = getResource<UserResource>(projectUuid)(getState().resources);
141 dispatch<any>(openContextMenu(event, {
146 menuKind: ContextMenuKind.ROOT_PROJECT,
152 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
153 (dispatch: Dispatch, getState: () => RootState) => {
154 const res = getResource<ProjectResource>(projectUuid)(getState().resources);
156 dispatch<any>(openContextMenu(event, {
160 menuKind: ContextMenuKind.PROJECT,
161 ownerUuid: res.ownerUuid,
162 isTrashed: res.isTrashed
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 resourceKindToContextMenuKind = (uuid: string, isAdmin?: boolean) => {
197 const kind = extractUuidKind(uuid);
199 case ResourceKind.PROJECT:
200 return !isAdmin ? ContextMenuKind.PROJECT : ContextMenuKind.PROJECT_ADMIN;
201 case ResourceKind.COLLECTION:
202 return !isAdmin ? ContextMenuKind.COLLECTION_RESOURCE : ContextMenuKind.COLLECTION_ADMIN;
203 case ResourceKind.PROCESS:
204 return !isAdmin ? ContextMenuKind.PROCESS_RESOURCE : ContextMenuKind.PROCESS_ADMIN;
205 case ResourceKind.USER:
206 return ContextMenuKind.ROOT_PROJECT;
207 case ResourceKind.LINK:
208 return ContextMenuKind.LINK;