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, ContextMenuResource } 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';
16 export const contextMenuActions = unionize({
17 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
18 CLOSE_CONTEXT_MENU: ofType<{}>()
21 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
23 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; description?: string; kind: ContextMenuKind; }) =>
24 (dispatch: Dispatch) => {
25 event.preventDefault();
27 contextMenuActions.OPEN_CONTEXT_MENU({
28 position: { x: event.clientX, y: event.clientY },
34 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
35 (dispatch: Dispatch, getState: () => RootState) => {
36 const userResource = getResource<UserResource>(projectUuid)(getState().resources);
38 dispatch<any>(openContextMenu(event, {
40 uuid: userResource.uuid,
41 kind: ContextMenuKind.ROOT_PROJECT
46 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
47 (dispatch: Dispatch, getState: () => RootState) => {
48 const projectResource = getResource<ProjectResource>(projectUuid)(getState().resources);
49 if (projectResource) {
50 dispatch<any>(openContextMenu(event, {
51 name: projectResource.name,
52 uuid: projectResource.uuid,
53 kind: ContextMenuKind.PROJECT
58 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
59 (dispatch: Dispatch, getState: () => RootState) => {
60 if (!isSidePanelTreeCategory(id)) {
61 const kind = extractUuidKind(id);
62 if (kind === ResourceKind.USER) {
63 dispatch<any>(openRootProjectContextMenu(event, id));
64 } else if (kind === ResourceKind.PROJECT) {
65 dispatch<any>(openProjectContextMenu(event, id));
70 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>) =>
71 (dispatch: Dispatch, getState: () => RootState) => {
72 const { location } = getState().router;
73 const pathname = location ? location.pathname : '';
74 // ToDo: We get error from matchProcessRoute
75 // const match = matchProcessRoute(pathname);
76 // console.log('match: ', match);
77 // const uuid = match ? match.params.id : '';
78 const uuid = pathname.split('/').slice(-1)[0];
83 kind: ContextMenuKind.PROCESS
85 dispatch<any>(openContextMenu(event, resource));
88 export const resourceKindToContextMenuKind = (uuid: string) => {
89 const kind = extractUuidKind(uuid);
91 case ResourceKind.PROJECT:
92 return ContextMenuKind.PROJECT;
93 case ResourceKind.COLLECTION:
94 return ContextMenuKind.COLLECTION_RESOURCE;
95 case ResourceKind.USER:
96 return ContextMenuKind.ROOT_PROJECT;