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';
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;
40 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
42 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
43 (dispatch: Dispatch) => {
44 event.preventDefault();
45 const { left, top } = event.currentTarget.getBoundingClientRect();
47 contextMenuActions.OPEN_CONTEXT_MENU({
49 x: event.clientX || left,
50 y: event.clientY || top,
57 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
58 (dispatch: Dispatch, getState: () => RootState) => {
59 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
60 dispatch<any>(openContextMenu(event, {
64 kind: ResourceKind.COLLECTION,
65 menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
69 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
70 (dispatch: Dispatch, getState: () => RootState) => {
71 dispatch<any>(openContextMenu(event, {
73 uuid: repository.uuid,
74 ownerUuid: repository.ownerUuid,
75 kind: ResourceKind.REPOSITORY,
76 menuKind: ContextMenuKind.REPOSITORY
80 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
81 (dispatch: Dispatch, getState: () => RootState) => {
82 dispatch<any>(openContextMenu(event, {
84 uuid: repository.uuid,
85 ownerUuid: repository.ownerUuid,
86 kind: ResourceKind.VIRTUAL_MACHINE,
87 menuKind: ContextMenuKind.VIRTUAL_MACHINE
91 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
92 (dispatch: Dispatch) => {
93 dispatch<any>(openContextMenu(event, {
96 ownerUuid: sshKey.ownerUuid,
97 kind: ResourceKind.SSH_KEY,
98 menuKind: ContextMenuKind.SSH_KEY
102 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
103 (dispatch: Dispatch) => {
104 dispatch<any>(openContextMenu(event, {
106 uuid: keepService.uuid,
107 ownerUuid: keepService.ownerUuid,
108 kind: ResourceKind.KEEP_SERVICE,
109 menuKind: ContextMenuKind.KEEP_SERVICE
113 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, computeNode: NodeResource) =>
114 (dispatch: Dispatch) => {
115 dispatch<any>(openContextMenu(event, {
117 uuid: computeNode.uuid,
118 ownerUuid: computeNode.ownerUuid,
119 kind: ResourceKind.NODE,
120 menuKind: ContextMenuKind.NODE
124 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
125 (dispatch: Dispatch, getState: () => RootState) => {
126 const res = getResource<UserResource>(projectUuid)(getState().resources);
128 dispatch<any>(openContextMenu(event, {
133 menuKind: ContextMenuKind.ROOT_PROJECT,
139 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
140 (dispatch: Dispatch, getState: () => RootState) => {
141 const res = getResource<ProjectResource>(projectUuid)(getState().resources);
143 dispatch<any>(openContextMenu(event, {
147 menuKind: ContextMenuKind.PROJECT,
148 ownerUuid: res.ownerUuid,
149 isTrashed: res.isTrashed
154 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
155 (dispatch: Dispatch, getState: () => RootState) => {
156 if (!isSidePanelTreeCategory(id)) {
157 const kind = extractUuidKind(id);
158 if (kind === ResourceKind.USER) {
159 dispatch<any>(openRootProjectContextMenu(event, id));
160 } else if (kind === ResourceKind.PROJECT) {
161 dispatch<any>(openProjectContextMenu(event, id));
166 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
167 (dispatch: Dispatch, getState: () => RootState) => {
169 uuid: process.containerRequest.uuid,
170 ownerUuid: process.containerRequest.ownerUuid,
171 kind: ResourceKind.PROCESS,
172 name: process.containerRequest.name,
173 description: process.containerRequest.description,
174 menuKind: ContextMenuKind.PROCESS
176 dispatch<any>(openContextMenu(event, resource));
179 export const resourceKindToContextMenuKind = (uuid: string) => {
180 const kind = extractUuidKind(uuid);
182 case ResourceKind.PROJECT:
183 return ContextMenuKind.PROJECT;
184 case ResourceKind.COLLECTION:
185 return ContextMenuKind.COLLECTION_RESOURCE;
186 case ResourceKind.PROCESS:
187 return ContextMenuKind.PROCESS_RESOURCE;
188 case ResourceKind.USER:
189 return ContextMenuKind.ROOT_PROJECT;