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';
24 import { resourceIsFrozen } from 'common/frozen-resources';
26 export const contextMenuActions = unionize({
27 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
28 CLOSE_CONTEXT_MENU: ofType<{}>()
31 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
33 export type ContextMenuResource = {
39 menuKind: ContextMenuKind | string;
43 workflowUuid?: string;
46 storageClassesDesired?: string[];
47 properties?: { [key: string]: string | string[] };
50 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
52 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
53 (dispatch: Dispatch) => {
54 event.preventDefault();
55 const { left, top } = event.currentTarget.getBoundingClientRect();
57 contextMenuActions.OPEN_CONTEXT_MENU({
59 x: event.clientX || left,
60 y: event.clientY || top,
67 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
68 (dispatch: Dispatch, getState: () => RootState) => {
69 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
70 dispatch<any>(openContextMenu(event, {
75 kind: ResourceKind.COLLECTION,
76 menuKind: isCollectionFileSelected
78 ? ContextMenuKind.COLLECTION_FILES
79 : ContextMenuKind.READONLY_COLLECTION_FILES
80 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
84 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
85 (dispatch: Dispatch, getState: () => RootState) => {
86 dispatch<any>(openContextMenu(event, {
88 uuid: repository.uuid,
89 ownerUuid: repository.ownerUuid,
90 kind: ResourceKind.REPOSITORY,
91 menuKind: ContextMenuKind.REPOSITORY
95 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
96 (dispatch: Dispatch, getState: () => RootState) => {
97 dispatch<any>(openContextMenu(event, {
99 uuid: repository.uuid,
100 ownerUuid: repository.ownerUuid,
101 kind: ResourceKind.VIRTUAL_MACHINE,
102 menuKind: ContextMenuKind.VIRTUAL_MACHINE
106 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
107 (dispatch: Dispatch) => {
108 dispatch<any>(openContextMenu(event, {
111 ownerUuid: sshKey.ownerUuid,
112 kind: ResourceKind.SSH_KEY,
113 menuKind: ContextMenuKind.SSH_KEY
117 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
118 (dispatch: Dispatch) => {
119 dispatch<any>(openContextMenu(event, {
121 uuid: keepService.uuid,
122 ownerUuid: keepService.ownerUuid,
123 kind: ResourceKind.KEEP_SERVICE,
124 menuKind: ContextMenuKind.KEEP_SERVICE
128 export const openApiClientAuthorizationContextMenu =
129 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
130 (dispatch: Dispatch) => {
131 dispatch<any>(openContextMenu(event, {
135 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
136 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
140 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
141 (dispatch: Dispatch, getState: () => RootState) => {
142 const res = getResource<UserResource>(projectUuid)(getState().resources);
144 dispatch<any>(openContextMenu(event, {
149 menuKind: ContextMenuKind.ROOT_PROJECT,
155 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
156 (dispatch: Dispatch, getState: () => RootState) => {
157 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
158 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
159 if (res && menuKind) {
160 dispatch<any>(openContextMenu(event, {
165 description: res.description,
166 ownerUuid: res.ownerUuid,
167 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
172 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
173 (dispatch: Dispatch, getState: () => RootState) => {
174 if (!isSidePanelTreeCategory(id)) {
175 const kind = extractUuidKind(id);
176 if (kind === ResourceKind.USER) {
177 dispatch<any>(openRootProjectContextMenu(event, id));
178 } else if (kind === ResourceKind.PROJECT) {
179 dispatch<any>(openProjectContextMenu(event, id));
184 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
185 (dispatch: Dispatch, getState: () => RootState) => {
186 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
188 dispatch<any>(openContextMenu(event, {
190 ownerUuid: res.ownerUuid,
191 kind: ResourceKind.PROCESS,
193 description: res.description,
194 outputUuid: res.outputUuid || '',
195 workflowUuid: res.properties.template_uuid || '',
196 menuKind: ContextMenuKind.PROCESS_RESOURCE
201 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
202 (dispatch: Dispatch, getState: () => RootState) => {
204 dispatch<any>(openContextMenu(event, {
208 menuKind: ContextMenuKind.PERMISSION_EDIT,
209 ownerUuid: link.ownerUuid,
214 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) =>
215 (dispatch: Dispatch, getState: () => RootState) => {
216 dispatch<any>(openContextMenu(event, {
219 ownerUuid: user.ownerUuid,
221 menuKind: ContextMenuKind.USER
225 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
226 (dispatch: Dispatch, getState: () => RootState) => {
227 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
228 const kind = extractUuidKind(uuid);
229 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
230 const isFrozen = resourceIsFrozen(resource, getState().resources);
231 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly && !isFrozen;
234 case ResourceKind.PROJECT:
235 if (resource && !!(resource as any).frozenByUuid) {
236 return ContextMenuKind.FROZEN_PROJECT;
239 return (isAdminUser && !readonly)
240 ? (resource && resource.groupClass !== GroupClass.FILTER)
241 ? ContextMenuKind.PROJECT_ADMIN
242 : ContextMenuKind.FILTER_GROUP_ADMIN
244 ? (resource && resource.groupClass !== GroupClass.FILTER)
245 ? ContextMenuKind.PROJECT
246 : ContextMenuKind.FILTER_GROUP
247 : ContextMenuKind.READONLY_PROJECT;
248 case ResourceKind.COLLECTION:
249 const c = getResource<CollectionResource>(uuid)(getState().resources);
250 if (c === undefined) { return; }
251 const isOldVersion = c.uuid !== c.currentVersionUuid;
252 const isTrashed = c.isTrashed;
254 ? ContextMenuKind.OLD_VERSION_COLLECTION
255 : (isTrashed && isEditable)
256 ? ContextMenuKind.TRASHED_COLLECTION
257 : (isAdminUser && !readonly)
258 ? ContextMenuKind.COLLECTION_ADMIN
260 ? ContextMenuKind.COLLECTION
261 : ContextMenuKind.READONLY_COLLECTION;
262 case ResourceKind.PROCESS:
263 return (isAdminUser && !readonly)
264 ? ContextMenuKind.PROCESS_ADMIN
266 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
267 : ContextMenuKind.PROCESS_RESOURCE;
268 case ResourceKind.USER:
269 return ContextMenuKind.ROOT_PROJECT;
270 case ResourceKind.LINK:
271 return ContextMenuKind.LINK;
272 case ResourceKind.WORKFLOW:
273 return ContextMenuKind.WORKFLOW;