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';
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;
35 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) =>
36 event.nativeEvent.detail === 0;
37 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
38 (dispatch: Dispatch) => {
39 event.preventDefault();
40 const { left, top } = event.currentTarget.getBoundingClientRect();
42 contextMenuActions.OPEN_CONTEXT_MENU({
44 x: event.clientX || left,
45 y: event.clientY || top,
52 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
53 (dispatch: Dispatch, getState: () => RootState) => {
54 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
55 dispatch<any>(openContextMenu(event, {
59 kind: ResourceKind.COLLECTION,
60 menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
64 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, repository: RepositoryResource) =>
65 (dispatch: Dispatch, getState: () => RootState) => {
66 dispatch<any>(openContextMenu(event, {
68 uuid: repository.uuid,
69 ownerUuid: repository.ownerUuid,
70 kind: ResourceKind.REPOSITORY,
71 menuKind: ContextMenuKind.REPOSITORY,
76 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
77 (dispatch: Dispatch, getState: () => RootState) => {
78 const res = getResource<UserResource>(projectUuid)(getState().resources);
80 dispatch<any>(openContextMenu(event, {
85 menuKind: ContextMenuKind.ROOT_PROJECT,
91 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
92 (dispatch: Dispatch, getState: () => RootState) => {
93 const res = getResource<ProjectResource>(projectUuid)(getState().resources);
95 dispatch<any>(openContextMenu(event, {
99 menuKind: ContextMenuKind.PROJECT,
100 ownerUuid: res.ownerUuid,
101 isTrashed: res.isTrashed
106 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
107 (dispatch: Dispatch, getState: () => RootState) => {
108 if (!isSidePanelTreeCategory(id)) {
109 const kind = extractUuidKind(id);
110 if (kind === ResourceKind.USER) {
111 dispatch<any>(openRootProjectContextMenu(event, id));
112 } else if (kind === ResourceKind.PROJECT) {
113 dispatch<any>(openProjectContextMenu(event, id));
118 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
119 (dispatch: Dispatch, getState: () => RootState) => {
121 uuid: process.containerRequest.uuid,
123 kind: ResourceKind.PROCESS,
126 menuKind: ContextMenuKind.PROCESS
128 dispatch<any>(openContextMenu(event, resource));
131 export const resourceKindToContextMenuKind = (uuid: string) => {
132 const kind = extractUuidKind(uuid);
134 case ResourceKind.PROJECT:
135 return ContextMenuKind.PROJECT;
136 case ResourceKind.COLLECTION:
137 return ContextMenuKind.COLLECTION_RESOURCE;
138 case ResourceKind.PROCESS:
139 return ContextMenuKind.PROCESS_RESOURCE;
140 case ResourceKind.USER:
141 return ContextMenuKind.ROOT_PROJECT;