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[];
46 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
48 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
49 (dispatch: Dispatch) => {
50 event.preventDefault();
51 const { left, top } = event.currentTarget.getBoundingClientRect();
53 contextMenuActions.OPEN_CONTEXT_MENU({
55 x: event.clientX || left,
56 y: event.clientY || top,
63 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
64 (dispatch: Dispatch, getState: () => RootState) => {
65 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
66 dispatch<any>(openContextMenu(event, {
71 kind: ResourceKind.COLLECTION,
72 menuKind: isCollectionFileSelected
74 ? ContextMenuKind.COLLECTION_FILES
75 : ContextMenuKind.READONLY_COLLECTION_FILES
76 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
80 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
81 (dispatch: Dispatch, getState: () => RootState) => {
82 dispatch<any>(openContextMenu(event, {
84 uuid: repository.uuid,
85 ownerUuid: repository.ownerUuid,
86 kind: ResourceKind.REPOSITORY,
87 menuKind: ContextMenuKind.REPOSITORY
91 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
92 (dispatch: Dispatch, getState: () => RootState) => {
93 dispatch<any>(openContextMenu(event, {
95 uuid: repository.uuid,
96 ownerUuid: repository.ownerUuid,
97 kind: ResourceKind.VIRTUAL_MACHINE,
98 menuKind: ContextMenuKind.VIRTUAL_MACHINE
102 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
103 (dispatch: Dispatch) => {
104 dispatch<any>(openContextMenu(event, {
107 ownerUuid: sshKey.ownerUuid,
108 kind: ResourceKind.SSH_KEY,
109 menuKind: ContextMenuKind.SSH_KEY
113 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
114 (dispatch: Dispatch) => {
115 dispatch<any>(openContextMenu(event, {
117 uuid: keepService.uuid,
118 ownerUuid: keepService.ownerUuid,
119 kind: ResourceKind.KEEP_SERVICE,
120 menuKind: ContextMenuKind.KEEP_SERVICE
124 export const openApiClientAuthorizationContextMenu =
125 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
126 (dispatch: Dispatch) => {
127 dispatch<any>(openContextMenu(event, {
131 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
132 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
136 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
137 (dispatch: Dispatch, getState: () => RootState) => {
138 const res = getResource<UserResource>(projectUuid)(getState().resources);
140 dispatch<any>(openContextMenu(event, {
145 menuKind: ContextMenuKind.ROOT_PROJECT,
151 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
152 (dispatch: Dispatch, getState: () => RootState) => {
153 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
154 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
155 if (res && menuKind) {
156 dispatch<any>(openContextMenu(event, {
161 description: res.description,
162 ownerUuid: res.ownerUuid,
163 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
168 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
169 (dispatch: Dispatch, getState: () => RootState) => {
170 if (!isSidePanelTreeCategory(id)) {
171 const kind = extractUuidKind(id);
172 if (kind === ResourceKind.USER) {
173 dispatch<any>(openRootProjectContextMenu(event, id));
174 } else if (kind === ResourceKind.PROJECT) {
175 dispatch<any>(openProjectContextMenu(event, id));
180 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
181 (dispatch: Dispatch, getState: () => RootState) => {
182 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
184 dispatch<any>(openContextMenu(event, {
186 ownerUuid: res.ownerUuid,
187 kind: ResourceKind.PROCESS,
189 description: res.description,
190 outputUuid: res.outputUuid || '',
191 workflowUuid: res.properties.workflowUuid || '',
192 menuKind: ContextMenuKind.PROCESS
197 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
198 (dispatch: Dispatch, getState: () => RootState) => {
200 dispatch<any>(openContextMenu(event, {
204 menuKind: ContextMenuKind.PERMISSION_EDIT,
205 ownerUuid: link.ownerUuid,
210 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
211 (dispatch: Dispatch, getState: () => RootState) => {
212 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
213 const kind = extractUuidKind(uuid);
214 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
216 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
218 case ResourceKind.PROJECT:
219 return (isAdminUser && !readonly)
220 ? (resource && resource.groupClass !== GroupClass.FILTER)
221 ? ContextMenuKind.PROJECT_ADMIN
222 : ContextMenuKind.FILTER_GROUP_ADMIN
224 ? (resource && resource.groupClass !== GroupClass.FILTER)
225 ? ContextMenuKind.PROJECT
226 : ContextMenuKind.FILTER_GROUP
227 : ContextMenuKind.READONLY_PROJECT;
228 case ResourceKind.COLLECTION:
229 const c = getResource<CollectionResource>(uuid)(getState().resources);
230 if (c === undefined) { return; }
231 const isOldVersion = c.uuid !== c.currentVersionUuid;
232 const isTrashed = c.isTrashed;
234 ? ContextMenuKind.OLD_VERSION_COLLECTION
235 : (isTrashed && isEditable)
236 ? ContextMenuKind.TRASHED_COLLECTION
237 : (isAdminUser && !readonly)
238 ? ContextMenuKind.COLLECTION_ADMIN
240 ? ContextMenuKind.COLLECTION
241 : ContextMenuKind.READONLY_COLLECTION;
242 case ResourceKind.PROCESS:
243 return (isAdminUser && !readonly)
244 ? ContextMenuKind.PROCESS_ADMIN
246 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
247 : ContextMenuKind.PROCESS_RESOURCE;
248 case ResourceKind.USER:
249 return ContextMenuKind.ROOT_PROJECT;
250 case ResourceKind.LINK:
251 return ContextMenuKind.LINK;