Merge branch '14503_keep_services_panel'
[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 } 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';
18 import { KeepServiceResource } from '~/models/keep-services';
19
20 export const contextMenuActions = unionize({
21     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
22     CLOSE_CONTEXT_MENU: ofType<{}>()
23 });
24
25 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
26
27 export type ContextMenuResource = {
28     name: string;
29     uuid: string;
30     ownerUuid: string;
31     description?: string;
32     kind: ResourceKind,
33     menuKind: ContextMenuKind;
34     isTrashed?: boolean;
35     index?: number
36 };
37
38 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
39
40 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
41     (dispatch: Dispatch) => {
42         event.preventDefault();
43         const { left, top } = event.currentTarget.getBoundingClientRect();
44         dispatch(
45             contextMenuActions.OPEN_CONTEXT_MENU({
46                 position: {
47                     x: event.clientX || left,
48                     y: event.clientY || top,
49                 },
50                 resource
51             })
52         );
53     };
54
55 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
56     (dispatch: Dispatch, getState: () => RootState) => {
57         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
58         dispatch<any>(openContextMenu(event, {
59             name: '',
60             uuid: '',
61             ownerUuid: '',
62             kind: ResourceKind.COLLECTION,
63             menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
64         }));
65     };
66
67 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, repository: RepositoryResource) =>
68     (dispatch: Dispatch, getState: () => RootState) => {
69             dispatch<any>(openContextMenu(event, {
70                 name: '',
71                 uuid: repository.uuid,
72                 ownerUuid: repository.ownerUuid,
73                 kind: ResourceKind.REPOSITORY,
74                 menuKind: ContextMenuKind.REPOSITORY,
75                 index
76             }));
77     };
78
79 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, sshKey: SshKeyResource) =>
80     (dispatch: Dispatch) => {
81         dispatch<any>(openContextMenu(event, {
82             name: '',
83             uuid: sshKey.uuid,
84             ownerUuid: sshKey.ownerUuid,
85             kind: ResourceKind.SSH_KEY,
86             menuKind: ContextMenuKind.SSH_KEY,
87             index
88         }));
89     };
90
91 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, keepService: KeepServiceResource) =>
92     (dispatch: Dispatch) => {
93         dispatch<any>(openContextMenu(event, {
94             name: '',
95             uuid: keepService.uuid,
96             ownerUuid: keepService.ownerUuid,
97             kind: ResourceKind.KEEP_SERVICE,
98             menuKind: ContextMenuKind.KEEP_SERVICE,
99             index
100         }));
101     };
102
103 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
104     (dispatch: Dispatch, getState: () => RootState) => {
105         const res = getResource<UserResource>(projectUuid)(getState().resources);
106         if (res) {
107             dispatch<any>(openContextMenu(event, {
108                 name: '',
109                 uuid: res.uuid,
110                 ownerUuid: res.uuid,
111                 kind: res.kind,
112                 menuKind: ContextMenuKind.ROOT_PROJECT,
113                 isTrashed: false
114             }));
115         }
116     };
117
118 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
119     (dispatch: Dispatch, getState: () => RootState) => {
120         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
121         if (res) {
122             dispatch<any>(openContextMenu(event, {
123                 name: res.name,
124                 uuid: res.uuid,
125                 kind: res.kind,
126                 menuKind: ContextMenuKind.PROJECT,
127                 ownerUuid: res.ownerUuid,
128                 isTrashed: res.isTrashed
129             }));
130         }
131     };
132
133 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
134     (dispatch: Dispatch, getState: () => RootState) => {
135         if (!isSidePanelTreeCategory(id)) {
136             const kind = extractUuidKind(id);
137             if (kind === ResourceKind.USER) {
138                 dispatch<any>(openRootProjectContextMenu(event, id));
139             } else if (kind === ResourceKind.PROJECT) {
140                 dispatch<any>(openProjectContextMenu(event, id));
141             }
142         }
143     };
144
145 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
146     (dispatch: Dispatch, getState: () => RootState) => {
147         const resource = {
148             uuid: process.containerRequest.uuid,
149             ownerUuid: process.containerRequest.ownerUuid,
150             kind: ResourceKind.PROCESS,
151             name: process.containerRequest.name,
152             description: process.containerRequest.description,
153             menuKind: ContextMenuKind.PROCESS
154         };
155         dispatch<any>(openContextMenu(event, resource));
156     };
157
158 export const resourceKindToContextMenuKind = (uuid: string) => {
159     const kind = extractUuidKind(uuid);
160     switch (kind) {
161         case ResourceKind.PROJECT:
162             return ContextMenuKind.PROJECT;
163         case ResourceKind.COLLECTION:
164             return ContextMenuKind.COLLECTION_RESOURCE;
165         case ResourceKind.PROCESS:
166             return ContextMenuKind.PROCESS_RESOURCE;
167         case ResourceKind.USER:
168             return ContextMenuKind.ROOT_PROJECT;
169         default:
170             return;
171     }
172 };