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