1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { UserResource } from 'models/user';
12 import { isSidePanelTreeCategory } from 'store/side-panel-tree/side-panel-tree-actions';
13 import { extractUuidKind, ResourceKind, EditableResource } from 'models/resource';
14 import { Process } from 'store/processes/process';
15 import { RepositoryResource } from 'models/repositories';
16 import { SshKeyResource } from 'models/ssh-key';
17 import { VirtualMachinesResource } from 'models/virtual-machines';
18 import { KeepServiceResource } from 'models/keep-services';
19 import { ProcessResource } from 'models/process';
20 import { CollectionResource } from 'models/collection';
21 import { GroupClass, GroupResource } from 'models/group';
22 import { GroupContentsResource } from 'services/groups-service/groups-service';
23 import { LinkResource } from 'models/link';
25 export const contextMenuActions = unionize({
26 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
27 CLOSE_CONTEXT_MENU: ofType<{}>()
30 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
32 export type ContextMenuResource = {
38 menuKind: ContextMenuKind | string;
42 workflowUuid?: string;
43 storageClassesDesired?: string[];
44 properties?: { [key: string]: string | string[] };
47 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
49 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
50 (dispatch: Dispatch) => {
51 event.preventDefault();
52 const { left, top } = event.currentTarget.getBoundingClientRect();
54 contextMenuActions.OPEN_CONTEXT_MENU({
56 x: event.clientX || left,
57 y: event.clientY || top,
64 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
65 (dispatch: Dispatch, getState: () => RootState) => {
66 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
67 dispatch<any>(openContextMenu(event, {
72 kind: ResourceKind.COLLECTION,
73 menuKind: isCollectionFileSelected
75 ? ContextMenuKind.COLLECTION_FILES
76 : ContextMenuKind.READONLY_COLLECTION_FILES
77 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
81 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
82 (dispatch: Dispatch, getState: () => RootState) => {
83 dispatch<any>(openContextMenu(event, {
85 uuid: repository.uuid,
86 ownerUuid: repository.ownerUuid,
87 kind: ResourceKind.REPOSITORY,
88 menuKind: ContextMenuKind.REPOSITORY
92 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
93 (dispatch: Dispatch, getState: () => RootState) => {
94 dispatch<any>(openContextMenu(event, {
96 uuid: repository.uuid,
97 ownerUuid: repository.ownerUuid,
98 kind: ResourceKind.VIRTUAL_MACHINE,
99 menuKind: ContextMenuKind.VIRTUAL_MACHINE
103 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
104 (dispatch: Dispatch) => {
105 dispatch<any>(openContextMenu(event, {
108 ownerUuid: sshKey.ownerUuid,
109 kind: ResourceKind.SSH_KEY,
110 menuKind: ContextMenuKind.SSH_KEY
114 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
115 (dispatch: Dispatch) => {
116 dispatch<any>(openContextMenu(event, {
118 uuid: keepService.uuid,
119 ownerUuid: keepService.ownerUuid,
120 kind: ResourceKind.KEEP_SERVICE,
121 menuKind: ContextMenuKind.KEEP_SERVICE
125 export const openApiClientAuthorizationContextMenu =
126 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
127 (dispatch: Dispatch) => {
128 dispatch<any>(openContextMenu(event, {
132 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
133 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
137 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
138 (dispatch: Dispatch, getState: () => RootState) => {
139 const res = getResource<UserResource>(projectUuid)(getState().resources);
141 dispatch<any>(openContextMenu(event, {
146 menuKind: ContextMenuKind.ROOT_PROJECT,
152 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
153 (dispatch: Dispatch, getState: () => RootState) => {
154 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
155 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
156 if (res && menuKind) {
157 dispatch<any>(openContextMenu(event, {
162 description: res.description,
163 ownerUuid: res.ownerUuid,
164 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
169 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
170 (dispatch: Dispatch, getState: () => RootState) => {
171 if (!isSidePanelTreeCategory(id)) {
172 const kind = extractUuidKind(id);
173 if (kind === ResourceKind.USER) {
174 dispatch<any>(openRootProjectContextMenu(event, id));
175 } else if (kind === ResourceKind.PROJECT) {
176 dispatch<any>(openProjectContextMenu(event, id));
181 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
182 (dispatch: Dispatch, getState: () => RootState) => {
183 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
185 dispatch<any>(openContextMenu(event, {
187 ownerUuid: res.ownerUuid,
188 kind: ResourceKind.PROCESS,
190 description: res.description,
191 outputUuid: res.outputUuid || '',
192 workflowUuid: res.properties.workflowUuid || '',
193 menuKind: ContextMenuKind.PROCESS
198 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
199 (dispatch: Dispatch, getState: () => RootState) => {
201 dispatch<any>(openContextMenu(event, {
205 menuKind: ContextMenuKind.PERMISSION_EDIT,
206 ownerUuid: link.ownerUuid,
211 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
212 (dispatch: Dispatch, getState: () => RootState) => {
213 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
214 const kind = extractUuidKind(uuid);
215 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
217 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
219 case ResourceKind.PROJECT:
220 return (isAdminUser && !readonly)
221 ? (resource && resource.groupClass !== GroupClass.FILTER)
222 ? ContextMenuKind.PROJECT_ADMIN
223 : ContextMenuKind.FILTER_GROUP_ADMIN
225 ? (resource && resource.groupClass !== GroupClass.FILTER)
226 ? ContextMenuKind.PROJECT
227 : ContextMenuKind.FILTER_GROUP
228 : ContextMenuKind.READONLY_PROJECT;
229 case ResourceKind.COLLECTION:
230 const c = getResource<CollectionResource>(uuid)(getState().resources);
231 if (c === undefined) { return; }
232 const isOldVersion = c.uuid !== c.currentVersionUuid;
233 const isTrashed = c.isTrashed;
235 ? ContextMenuKind.OLD_VERSION_COLLECTION
236 : (isTrashed && isEditable)
237 ? ContextMenuKind.TRASHED_COLLECTION
238 : (isAdminUser && !readonly)
239 ? ContextMenuKind.COLLECTION_ADMIN
241 ? ContextMenuKind.COLLECTION
242 : ContextMenuKind.READONLY_COLLECTION;
243 case ResourceKind.PROCESS:
244 return (isAdminUser && !readonly)
245 ? ContextMenuKind.PROCESS_ADMIN
247 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
248 : ContextMenuKind.PROCESS_RESOURCE;
249 case ResourceKind.USER:
250 return ContextMenuKind.ROOT_PROJECT;
251 case ResourceKind.LINK:
252 return ContextMenuKind.LINK;