Merge branch '14098-log-view'
[arvados-workbench2.git] / src / store / context-menu / context-menu-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
16
17 export const contextMenuActions = unionize({
18     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
19     CLOSE_CONTEXT_MENU: ofType<{}>()
20 });
21
22 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
23
24 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; description?: string; kind: ContextMenuKind; }) =>
25     (dispatch: Dispatch) => {
26         event.preventDefault();
27         dispatch(
28             contextMenuActions.OPEN_CONTEXT_MENU({
29                 position: { x: event.clientX, y: event.clientY },
30                 resource
31             })
32         );
33     };
34
35 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
36     (dispatch: Dispatch, getState: () => RootState) => {
37         const userResource = getResource<UserResource>(projectUuid)(getState().resources);
38         if (userResource) {
39             dispatch<any>(openContextMenu(event, {
40                 name: '',
41                 uuid: userResource.uuid,
42                 kind: ContextMenuKind.ROOT_PROJECT
43             }));
44         }
45     };
46
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
55             }));
56         }
57     };
58
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));
67             }
68         }
69     };
70
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];
80         const resource = {
81             uuid,
82             name: '',
83             description: '',
84             kind: ContextMenuKind.PROCESS
85         };
86         dispatch<any>(openContextMenu(event, resource));
87     };
88
89 export const resourceKindToContextMenuKind = (uuid: string) => {
90     const kind = extractUuidKind(uuid);
91     switch (kind) {
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;
98         default:
99             return;
100     }
101 };