15088: Removes navigation after second login
[arvados.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 { VirtualMachinesResource } from '~/models/virtual-machines';
19 import { KeepServiceResource } from '~/models/keep-services';
20 import { ProcessResource } from '~/models/process';
21
22 export const contextMenuActions = unionize({
23     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
24     CLOSE_CONTEXT_MENU: ofType<{}>()
25 });
26
27 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
28
29 export type ContextMenuResource = {
30     name: string;
31     uuid: string;
32     ownerUuid: string;
33     description?: string;
34     kind: ResourceKind,
35     menuKind: ContextMenuKind;
36     isTrashed?: boolean;
37     outputUuid?: string;
38     workflowUuid?: string;
39 };
40
41 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
42
43 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
44     (dispatch: Dispatch) => {
45         event.preventDefault();
46         const { left, top } = event.currentTarget.getBoundingClientRect();
47         dispatch(
48             contextMenuActions.OPEN_CONTEXT_MENU({
49                 position: {
50                     x: event.clientX || left,
51                     y: event.clientY || top,
52                 },
53                 resource
54             })
55         );
56     };
57
58 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
59     (dispatch: Dispatch, getState: () => RootState) => {
60         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
61         dispatch<any>(openContextMenu(event, {
62             name: '',
63             uuid: '',
64             ownerUuid: '',
65             kind: ResourceKind.COLLECTION,
66             menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
67         }));
68     };
69
70 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
71     (dispatch: Dispatch, getState: () => RootState) => {
72         dispatch<any>(openContextMenu(event, {
73             name: '',
74             uuid: repository.uuid,
75             ownerUuid: repository.ownerUuid,
76             kind: ResourceKind.REPOSITORY,
77             menuKind: ContextMenuKind.REPOSITORY
78         }));
79     };
80
81 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
82     (dispatch: Dispatch, getState: () => RootState) => {
83         dispatch<any>(openContextMenu(event, {
84             name: '',
85             uuid: repository.uuid,
86             ownerUuid: repository.ownerUuid,
87             kind: ResourceKind.VIRTUAL_MACHINE,
88             menuKind: ContextMenuKind.VIRTUAL_MACHINE
89         }));
90     };
91
92 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
93     (dispatch: Dispatch) => {
94         dispatch<any>(openContextMenu(event, {
95             name: '',
96             uuid: sshKey.uuid,
97             ownerUuid: sshKey.ownerUuid,
98             kind: ResourceKind.SSH_KEY,
99             menuKind: ContextMenuKind.SSH_KEY
100         }));
101     };
102
103 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
104     (dispatch: Dispatch) => {
105         dispatch<any>(openContextMenu(event, {
106             name: '',
107             uuid: keepService.uuid,
108             ownerUuid: keepService.ownerUuid,
109             kind: ResourceKind.KEEP_SERVICE,
110             menuKind: ContextMenuKind.KEEP_SERVICE
111         }));
112     };
113
114 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
115     (dispatch: Dispatch) => {
116         dispatch<any>(openContextMenu(event, {
117             name: '',
118             uuid: resourceUuid,
119             ownerUuid: '',
120             kind: ResourceKind.NODE,
121             menuKind: ContextMenuKind.NODE
122         }));
123     };
124
125 export const openApiClientAuthorizationContextMenu =
126     (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
127         (dispatch: Dispatch) => {
128             dispatch<any>(openContextMenu(event, {
129                 name: '',
130                 uuid: resourceUuid,
131                 ownerUuid: '',
132                 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
133                 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
134             }));
135         };
136
137 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
138     (dispatch: Dispatch, getState: () => RootState) => {
139         const res = getResource<UserResource>(projectUuid)(getState().resources);
140         if (res) {
141             dispatch<any>(openContextMenu(event, {
142                 name: '',
143                 uuid: res.uuid,
144                 ownerUuid: res.uuid,
145                 kind: res.kind,
146                 menuKind: ContextMenuKind.ROOT_PROJECT,
147                 isTrashed: false
148             }));
149         }
150     };
151
152 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
153     (dispatch: Dispatch, getState: () => RootState) => {
154         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
155         if (res) {
156             dispatch<any>(openContextMenu(event, {
157                 name: res.name,
158                 uuid: res.uuid,
159                 kind: res.kind,
160                 menuKind: ContextMenuKind.PROJECT,
161                 ownerUuid: res.ownerUuid,
162                 isTrashed: res.isTrashed
163             }));
164         }
165     };
166
167 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
168     (dispatch: Dispatch, getState: () => RootState) => {
169         if (!isSidePanelTreeCategory(id)) {
170             const kind = extractUuidKind(id);
171             if (kind === ResourceKind.USER) {
172                 dispatch<any>(openRootProjectContextMenu(event, id));
173             } else if (kind === ResourceKind.PROJECT) {
174                 dispatch<any>(openProjectContextMenu(event, id));
175             }
176         }
177     };
178
179 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
180     (dispatch: Dispatch, getState: () => RootState) => {
181         const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
182         if (res) {
183             dispatch<any>(openContextMenu(event, {
184                 uuid: res.uuid,
185                 ownerUuid: res.ownerUuid,
186                 kind: ResourceKind.PROCESS,
187                 name: res.name,
188                 description: res.description,
189                 outputUuid: res.outputUuid || '',
190                 workflowUuid: res.properties.workflowUuid || '',
191                 menuKind: ContextMenuKind.PROCESS
192             }));
193         }
194     };
195
196 export const resourceKindToContextMenuKind = (uuid: string, isAdmin?: boolean) => {
197     const kind = extractUuidKind(uuid);
198     switch (kind) {
199         case ResourceKind.PROJECT:
200             return !isAdmin ? ContextMenuKind.PROJECT : ContextMenuKind.PROJECT_ADMIN;
201         case ResourceKind.COLLECTION:
202             return !isAdmin ? ContextMenuKind.COLLECTION_RESOURCE : ContextMenuKind.COLLECTION_ADMIN;
203         case ResourceKind.PROCESS:
204             return !isAdmin ? ContextMenuKind.PROCESS_RESOURCE : ContextMenuKind.PROCESS_ADMIN;
205         case ResourceKind.USER:
206             return ContextMenuKind.ROOT_PROJECT;
207         case ResourceKind.LINK:
208             return ContextMenuKind.LINK;
209         default:
210             return;
211     }
212 };