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 { matchProcessRoute } from '~/routes/routes';
16 import { Process } from '~/store/processes/process';
18 export const contextMenuActions = unionize({
19 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
20 CLOSE_CONTEXT_MENU: ofType<{}>()
23 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
25 export type ContextMenuResource = {
31 menuKind: ContextMenuKind;
34 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) =>
35 event.nativeEvent.detail === 0;
36 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
37 (dispatch: Dispatch) => {
38 event.preventDefault();
39 const { left, top } = event.currentTarget.getBoundingClientRect();
41 contextMenuActions.OPEN_CONTEXT_MENU({
43 x: event.clientX || left,
44 y: event.clientY || top,
51 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
52 (dispatch: Dispatch, getState: () => RootState) => {
53 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
54 dispatch<any>(openContextMenu(event, {
58 kind: ResourceKind.COLLECTION,
59 menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
63 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
64 (dispatch: Dispatch, getState: () => RootState) => {
65 const res = getResource<UserResource>(projectUuid)(getState().resources);
67 dispatch<any>(openContextMenu(event, {
72 menuKind: ContextMenuKind.ROOT_PROJECT,
78 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
79 (dispatch: Dispatch, getState: () => RootState) => {
80 const res = getResource<ProjectResource>(projectUuid)(getState().resources);
82 dispatch<any>(openContextMenu(event, {
86 menuKind: ContextMenuKind.PROJECT,
87 ownerUuid: res.ownerUuid,
88 isTrashed: res.isTrashed
93 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
94 (dispatch: Dispatch, getState: () => RootState) => {
95 if (!isSidePanelTreeCategory(id)) {
96 const kind = extractUuidKind(id);
97 if (kind === ResourceKind.USER) {
98 dispatch<any>(openRootProjectContextMenu(event, id));
99 } else if (kind === ResourceKind.PROJECT) {
100 dispatch<any>(openProjectContextMenu(event, id));
105 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
106 (dispatch: Dispatch, getState: () => RootState) => {
108 uuid: process.containerRequest.uuid,
110 kind: ResourceKind.PROCESS,
113 menuKind: ContextMenuKind.PROCESS
115 dispatch<any>(openContextMenu(event, resource));
118 export const resourceKindToContextMenuKind = (uuid: string) => {
119 const kind = extractUuidKind(uuid);
121 case ResourceKind.PROJECT:
122 return ContextMenuKind.PROJECT;
123 case ResourceKind.COLLECTION:
124 return ContextMenuKind.COLLECTION_RESOURCE;
125 case ResourceKind.PROCESS:
126 return ContextMenuKind.PROCESS_RESOURCE;
127 case ResourceKind.USER:
128 return ContextMenuKind.ROOT_PROJECT;