15685: Adds file path validation on rename file dialog.
[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, getResourceWithEditableStatus } 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, EditableResource } 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     isEditable?: boolean;
38     outputUuid?: string;
39     workflowUuid?: string;
40 };
41
42 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
43
44 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
45     (dispatch: Dispatch) => {
46         event.preventDefault();
47         const { left, top } = event.currentTarget.getBoundingClientRect();
48         dispatch(
49             contextMenuActions.OPEN_CONTEXT_MENU({
50                 position: {
51                     x: event.clientX || left,
52                     y: event.clientY || top,
53                 },
54                 resource
55             })
56         );
57     };
58
59 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
60     (dispatch: Dispatch, getState: () => RootState) => {
61         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
62         dispatch<any>(openContextMenu(event, {
63             name: '',
64             uuid: '',
65             ownerUuid: '',
66             kind: ResourceKind.COLLECTION,
67             menuKind: isCollectionFileSelected
68                 ? isWritable
69                     ? ContextMenuKind.COLLECTION_FILES
70                     : ContextMenuKind.READONLY_COLLECTION_FILES
71                 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
72         }));
73     };
74
75 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
76     (dispatch: Dispatch, getState: () => RootState) => {
77         dispatch<any>(openContextMenu(event, {
78             name: '',
79             uuid: repository.uuid,
80             ownerUuid: repository.ownerUuid,
81             kind: ResourceKind.REPOSITORY,
82             menuKind: ContextMenuKind.REPOSITORY
83         }));
84     };
85
86 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
87     (dispatch: Dispatch, getState: () => RootState) => {
88         dispatch<any>(openContextMenu(event, {
89             name: '',
90             uuid: repository.uuid,
91             ownerUuid: repository.ownerUuid,
92             kind: ResourceKind.VIRTUAL_MACHINE,
93             menuKind: ContextMenuKind.VIRTUAL_MACHINE
94         }));
95     };
96
97 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
98     (dispatch: Dispatch) => {
99         dispatch<any>(openContextMenu(event, {
100             name: '',
101             uuid: sshKey.uuid,
102             ownerUuid: sshKey.ownerUuid,
103             kind: ResourceKind.SSH_KEY,
104             menuKind: ContextMenuKind.SSH_KEY
105         }));
106     };
107
108 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
109     (dispatch: Dispatch) => {
110         dispatch<any>(openContextMenu(event, {
111             name: '',
112             uuid: keepService.uuid,
113             ownerUuid: keepService.ownerUuid,
114             kind: ResourceKind.KEEP_SERVICE,
115             menuKind: ContextMenuKind.KEEP_SERVICE
116         }));
117     };
118
119 export const openComputeNodeContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
120     (dispatch: Dispatch) => {
121         dispatch<any>(openContextMenu(event, {
122             name: '',
123             uuid: resourceUuid,
124             ownerUuid: '',
125             kind: ResourceKind.NODE,
126             menuKind: ContextMenuKind.NODE
127         }));
128     };
129
130 export const openApiClientAuthorizationContextMenu =
131     (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
132         (dispatch: Dispatch) => {
133             dispatch<any>(openContextMenu(event, {
134                 name: '',
135                 uuid: resourceUuid,
136                 ownerUuid: '',
137                 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
138                 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
139             }));
140         };
141
142 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
143     (dispatch: Dispatch, getState: () => RootState) => {
144         const res = getResource<UserResource>(projectUuid)(getState().resources);
145         if (res) {
146             dispatch<any>(openContextMenu(event, {
147                 name: '',
148                 uuid: res.uuid,
149                 ownerUuid: res.uuid,
150                 kind: res.kind,
151                 menuKind: ContextMenuKind.ROOT_PROJECT,
152                 isTrashed: false
153             }));
154         }
155     };
156
157 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
158     (dispatch: Dispatch, getState: () => RootState) => {
159         const { isAdmin, uuid: userUuid } = getState().auth.user!;
160         const res = getResourceWithEditableStatus<ProjectResource & EditableResource>(resourceUuid, userUuid)(getState().resources);
161         const menuKind = resourceKindToContextMenuKind(resourceUuid, isAdmin, (res || {} as EditableResource).isEditable);
162         if (res && menuKind) {
163             dispatch<any>(openContextMenu(event, {
164                 name: res.name,
165                 uuid: res.uuid,
166                 kind: res.kind,
167                 menuKind,
168                 ownerUuid: res.ownerUuid,
169                 isTrashed: res.isTrashed
170             }));
171         }
172     };
173
174 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
175     (dispatch: Dispatch, getState: () => RootState) => {
176         if (!isSidePanelTreeCategory(id)) {
177             const kind = extractUuidKind(id);
178             if (kind === ResourceKind.USER) {
179                 dispatch<any>(openRootProjectContextMenu(event, id));
180             } else if (kind === ResourceKind.PROJECT) {
181                 dispatch<any>(openProjectContextMenu(event, id));
182             }
183         }
184     };
185
186 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
187     (dispatch: Dispatch, getState: () => RootState) => {
188         const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
189         if (res) {
190             dispatch<any>(openContextMenu(event, {
191                 uuid: res.uuid,
192                 ownerUuid: res.ownerUuid,
193                 kind: ResourceKind.PROCESS,
194                 name: res.name,
195                 description: res.description,
196                 outputUuid: res.outputUuid || '',
197                 workflowUuid: res.properties.workflowUuid || '',
198                 menuKind: ContextMenuKind.PROCESS
199             }));
200         }
201     };
202
203 export const resourceKindToContextMenuKind = (uuid: string, isAdmin?: boolean, isEditable?: boolean) => {
204     const kind = extractUuidKind(uuid);
205     switch (kind) {
206         case ResourceKind.PROJECT:
207             return !isAdmin
208                 ? isEditable
209                     ? ContextMenuKind.PROJECT
210                     : ContextMenuKind.READONLY_PROJECT
211                 : ContextMenuKind.PROJECT_ADMIN;
212         case ResourceKind.COLLECTION:
213             return !isAdmin
214                 ? isEditable
215                     ? ContextMenuKind.COLLECTION
216                     : ContextMenuKind.READONLY_COLLECTION
217                 : ContextMenuKind.COLLECTION_ADMIN;
218         case ResourceKind.PROCESS:
219             return !isAdmin
220                 ? ContextMenuKind.PROCESS_RESOURCE
221                 : ContextMenuKind.PROCESS_ADMIN;
222         case ResourceKind.USER:
223             return ContextMenuKind.ROOT_PROJECT;
224         case ResourceKind.LINK:
225             return ContextMenuKind.LINK;
226         default:
227             return;
228     }
229 };