init api token, add model and service, create dialogs and 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 { VirtualMachinesResource } from '~/models/virtual-machines';
19 import { KeepServiceResource } from '~/models/keep-services';
20 import { NodeResource } from '~/models/node';
21 import { ApiClientAuthorization } from '~/models/api-client-authorization';
22
23 export const contextMenuActions = unionize({
24     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
25     CLOSE_CONTEXT_MENU: ofType<{}>()
26 });
27
28 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
29
30 export type ContextMenuResource = {
31     name: string;
32     uuid: string;
33     ownerUuid: string;
34     description?: string;
35     kind: ResourceKind,
36     menuKind: ContextMenuKind;
37     isTrashed?: boolean;
38     index?: number
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>, computeNode: NodeResource) =>
115     (dispatch: Dispatch) => {
116         dispatch<any>(openContextMenu(event, {
117             name: '',
118             uuid: computeNode.uuid,
119             ownerUuid: computeNode.ownerUuid,
120             kind: ResourceKind.NODE,
121             menuKind: ContextMenuKind.NODE
122         }));
123     };
124
125 export const openApiClientAuthorizationContextMenu = 
126     (event: React.MouseEvent<HTMLElement>, apiClientAuthorization: ApiClientAuthorization) =>
127         (dispatch: Dispatch) => {
128             dispatch<any>(openContextMenu(event, {
129                 name: '',
130                 uuid: apiClientAuthorization.uuid,
131                 ownerUuid: apiClientAuthorization.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 resource = {
182             uuid: process.containerRequest.uuid,
183             ownerUuid: process.containerRequest.ownerUuid,
184             kind: ResourceKind.PROCESS,
185             name: process.containerRequest.name,
186             description: process.containerRequest.description,
187             menuKind: ContextMenuKind.PROCESS
188         };
189         dispatch<any>(openContextMenu(event, resource));
190     };
191
192 export const resourceKindToContextMenuKind = (uuid: string) => {
193     const kind = extractUuidKind(uuid);
194     switch (kind) {
195         case ResourceKind.PROJECT:
196             return ContextMenuKind.PROJECT;
197         case ResourceKind.COLLECTION:
198             return ContextMenuKind.COLLECTION_RESOURCE;
199         case ResourceKind.PROCESS:
200             return ContextMenuKind.PROCESS_RESOURCE;
201         case ResourceKind.USER:
202             return ContextMenuKind.ROOT_PROJECT;
203         default:
204             return;
205     }
206 };