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';
19 export const contextMenuActions = unionize({
20 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
21 CLOSE_CONTEXT_MENU: ofType<{}>()
24 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
26 export type ContextMenuResource = {
32 menuKind: ContextMenuKind;
36 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) =>
37 event.nativeEvent.detail === 0;
38 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
39 (dispatch: Dispatch) => {
40 event.preventDefault();
41 const { left, top } = event.currentTarget.getBoundingClientRect();
43 contextMenuActions.OPEN_CONTEXT_MENU({
45 x: event.clientX || left,
46 y: event.clientY || top,
53 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
54 (dispatch: Dispatch, getState: () => RootState) => {
55 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
56 dispatch<any>(openContextMenu(event, {
60 kind: ResourceKind.COLLECTION,
61 menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
65 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, repository: RepositoryResource) =>
66 (dispatch: Dispatch, getState: () => RootState) => {
67 dispatch<any>(openContextMenu(event, {
69 uuid: repository.uuid,
70 ownerUuid: repository.ownerUuid,
71 kind: ResourceKind.REPOSITORY,
72 menuKind: ContextMenuKind.REPOSITORY,
77 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, sshKey: SshKeyResource) =>
78 (dispatch: Dispatch) => {
79 dispatch<any>(openContextMenu(event, {
82 ownerUuid: sshKey.ownerUuid,
83 kind: ResourceKind.SSH_KEY,
84 menuKind: ContextMenuKind.SSH_KEY,
89 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
90 (dispatch: Dispatch, getState: () => RootState) => {
91 const res = getResource<UserResource>(projectUuid)(getState().resources);
93 dispatch<any>(openContextMenu(event, {
98 menuKind: ContextMenuKind.ROOT_PROJECT,
104 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
105 (dispatch: Dispatch, getState: () => RootState) => {
106 const res = getResource<ProjectResource>(projectUuid)(getState().resources);
108 dispatch<any>(openContextMenu(event, {
112 menuKind: ContextMenuKind.PROJECT,
113 ownerUuid: res.ownerUuid,
114 isTrashed: res.isTrashed
119 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
120 (dispatch: Dispatch, getState: () => RootState) => {
121 if (!isSidePanelTreeCategory(id)) {
122 const kind = extractUuidKind(id);
123 if (kind === ResourceKind.USER) {
124 dispatch<any>(openRootProjectContextMenu(event, id));
125 } else if (kind === ResourceKind.PROJECT) {
126 dispatch<any>(openProjectContextMenu(event, id));
131 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
132 (dispatch: Dispatch, getState: () => RootState) => {
134 uuid: process.containerRequest.uuid,
136 kind: ResourceKind.PROCESS,
139 menuKind: ContextMenuKind.PROCESS
141 dispatch<any>(openContextMenu(event, resource));
144 export const resourceKindToContextMenuKind = (uuid: string) => {
145 const kind = extractUuidKind(uuid);
147 case ResourceKind.PROJECT:
148 return ContextMenuKind.PROJECT;
149 case ResourceKind.COLLECTION:
150 return ContextMenuKind.COLLECTION_RESOURCE;
151 case ResourceKind.PROCESS:
152 return ContextMenuKind.PROCESS_RESOURCE;
153 case ResourceKind.USER:
154 return ContextMenuKind.ROOT_PROJECT;