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 { NodeResource } from '~/models/node';
21 import { ApiClientAuthorization } from '~/models/api-client-authorization';
23 export const contextMenuActions = unionize({
24 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
25 CLOSE_CONTEXT_MENU: ofType<{}>()
28 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
30 export type ContextMenuResource = {
36 menuKind: ContextMenuKind;
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>, computeNode: NodeResource) =>
115 (dispatch: Dispatch) => {
116 dispatch<any>(openContextMenu(event, {
118 uuid: computeNode.uuid,
119 ownerUuid: computeNode.ownerUuid,
120 kind: ResourceKind.NODE,
121 menuKind: ContextMenuKind.NODE
125 export const openApiClientAuthorizationContextMenu =
126 (event: React.MouseEvent<HTMLElement>, apiClientAuthorization: ApiClientAuthorization) =>
127 (dispatch: Dispatch) => {
128 dispatch<any>(openContextMenu(event, {
130 uuid: apiClientAuthorization.uuid,
131 ownerUuid: apiClientAuthorization.ownerUuid,
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) => {
182 uuid: process.containerRequest.uuid,
183 ownerUuid: process.containerRequest.ownerUuid,
184 kind: ResourceKind.PROCESS,
185 name: process.containerRequest.name,
186 description: process.containerRequest.description,
187 menuKind: ContextMenuKind.PROCESS
189 dispatch<any>(openContextMenu(event, resource));
192 export const resourceKindToContextMenuKind = (uuid: string) => {
193 const kind = extractUuidKind(uuid);
195 case ResourceKind.PROJECT:
196 return ContextMenuKind.PROJECT;
197 case ResourceKind.COLLECTION:
198 return ContextMenuKind.COLLECTION_RESOURCE;
199 case ResourceKind.PROCESS:
200 return ContextMenuKind.PROCESS_RESOURCE;
201 case ResourceKind.USER:
202 return ContextMenuKind.ROOT_PROJECT;