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';
21 export const contextMenuActions = unionize({
22 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
23 CLOSE_CONTEXT_MENU: ofType<{}>()
26 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
28 export type ContextMenuResource = {
34 menuKind: ContextMenuKind;
39 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
41 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
42 (dispatch: Dispatch) => {
43 event.preventDefault();
44 const { left, top } = event.currentTarget.getBoundingClientRect();
46 contextMenuActions.OPEN_CONTEXT_MENU({
48 x: event.clientX || left,
49 y: event.clientY || top,
56 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
57 (dispatch: Dispatch, getState: () => RootState) => {
58 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
59 dispatch<any>(openContextMenu(event, {
63 kind: ResourceKind.COLLECTION,
64 menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
68 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
69 (dispatch: Dispatch, getState: () => RootState) => {
70 dispatch<any>(openContextMenu(event, {
72 uuid: repository.uuid,
73 ownerUuid: repository.ownerUuid,
74 kind: ResourceKind.REPOSITORY,
75 menuKind: ContextMenuKind.REPOSITORY
79 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
80 (dispatch: Dispatch, getState: () => RootState) => {
81 dispatch<any>(openContextMenu(event, {
83 uuid: repository.uuid,
84 ownerUuid: repository.ownerUuid,
85 kind: ResourceKind.VIRTUAL_MACHINE,
86 menuKind: ContextMenuKind.VIRTUAL_MACHINE
90 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
91 (dispatch: Dispatch) => {
92 dispatch<any>(openContextMenu(event, {
95 ownerUuid: sshKey.ownerUuid,
96 kind: ResourceKind.SSH_KEY,
97 menuKind: ContextMenuKind.SSH_KEY
101 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
102 (dispatch: Dispatch) => {
103 dispatch<any>(openContextMenu(event, {
105 uuid: keepService.uuid,
106 ownerUuid: keepService.ownerUuid,
107 kind: ResourceKind.KEEP_SERVICE,
108 menuKind: ContextMenuKind.KEEP_SERVICE
112 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
113 (dispatch: Dispatch, getState: () => RootState) => {
114 const res = getResource<UserResource>(projectUuid)(getState().resources);
116 dispatch<any>(openContextMenu(event, {
121 menuKind: ContextMenuKind.ROOT_PROJECT,
127 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
128 (dispatch: Dispatch, getState: () => RootState) => {
129 const res = getResource<ProjectResource>(projectUuid)(getState().resources);
131 dispatch<any>(openContextMenu(event, {
135 menuKind: ContextMenuKind.PROJECT,
136 ownerUuid: res.ownerUuid,
137 isTrashed: res.isTrashed
142 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
143 (dispatch: Dispatch, getState: () => RootState) => {
144 if (!isSidePanelTreeCategory(id)) {
145 const kind = extractUuidKind(id);
146 if (kind === ResourceKind.USER) {
147 dispatch<any>(openRootProjectContextMenu(event, id));
148 } else if (kind === ResourceKind.PROJECT) {
149 dispatch<any>(openProjectContextMenu(event, id));
154 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
155 (dispatch: Dispatch, getState: () => RootState) => {
157 uuid: process.containerRequest.uuid,
158 ownerUuid: process.containerRequest.ownerUuid,
159 kind: ResourceKind.PROCESS,
160 name: process.containerRequest.name,
161 description: process.containerRequest.description,
162 menuKind: ContextMenuKind.PROCESS
164 dispatch<any>(openContextMenu(event, resource));
167 export const resourceKindToContextMenuKind = (uuid: string) => {
168 const kind = extractUuidKind(uuid);
170 case ResourceKind.PROJECT:
171 return ContextMenuKind.PROJECT;
172 case ResourceKind.COLLECTION:
173 return ContextMenuKind.COLLECTION_RESOURCE;
174 case ResourceKind.PROCESS:
175 return ContextMenuKind.PROCESS_RESOURCE;
176 case ResourceKind.USER:
177 return ContextMenuKind.ROOT_PROJECT;