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, Resource } from "models/resource";
14 import { Process, isProcessCancelable } 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";
25 import { ProjectResource } from "models/project";
26 import { getProcess } from "store/processes/process";
27 import { filterCollectionFilesBySelection } from "store/collection-panel/collection-panel-files/collection-panel-files-state";
29 export const contextMenuActions = unionize({
30 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition; resource: ContextMenuResource }>(),
31 CLOSE_CONTEXT_MENU: ofType<{}>(),
34 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
36 export type ContextMenuResource = {
42 menuKind: ContextMenuKind | string;
46 workflowUuid?: string;
49 storageClassesDesired?: string[];
50 properties?: { [key: string]: string | string[] };
52 fromContextMenu?: boolean;
55 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
57 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) => (dispatch: Dispatch) => {
58 event.preventDefault();
59 const { left, top } = event.currentTarget.getBoundingClientRect();
61 contextMenuActions.OPEN_CONTEXT_MENU({
63 x: event.clientX || left,
64 y: event.clientY || top,
71 export const openCollectionFilesContextMenu =
72 (event: React.MouseEvent<HTMLElement>, isWritable: boolean) => (dispatch: Dispatch, getState: () => RootState) => {
73 const selectedCount = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).length;
74 const multiple = selectedCount > 1;
76 openContextMenu(event, {
81 kind: ResourceKind.COLLECTION,
86 ? ContextMenuKind.COLLECTION_FILES_MULTIPLE
87 : ContextMenuKind.COLLECTION_FILES
89 ? ContextMenuKind.READONLY_COLLECTION_FILES_MULTIPLE
90 : ContextMenuKind.READONLY_COLLECTION_FILES
91 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED,
96 export const openRepositoryContextMenu =
97 (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) => (dispatch: Dispatch, getState: () => RootState) => {
99 openContextMenu(event, {
101 uuid: repository.uuid,
102 ownerUuid: repository.ownerUuid,
103 kind: ResourceKind.REPOSITORY,
104 menuKind: ContextMenuKind.REPOSITORY,
109 export const openVirtualMachinesContextMenu =
110 (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) => (dispatch: Dispatch, getState: () => RootState) => {
112 openContextMenu(event, {
114 uuid: repository.uuid,
115 ownerUuid: repository.ownerUuid,
116 kind: ResourceKind.VIRTUAL_MACHINE,
117 menuKind: ContextMenuKind.VIRTUAL_MACHINE,
122 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) => (dispatch: Dispatch) => {
124 openContextMenu(event, {
127 ownerUuid: sshKey.ownerUuid,
128 kind: ResourceKind.SSH_KEY,
129 menuKind: ContextMenuKind.SSH_KEY,
134 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) => (dispatch: Dispatch) => {
136 openContextMenu(event, {
138 uuid: keepService.uuid,
139 ownerUuid: keepService.ownerUuid,
140 kind: ResourceKind.KEEP_SERVICE,
141 menuKind: ContextMenuKind.KEEP_SERVICE,
146 export const openApiClientAuthorizationContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => (dispatch: Dispatch) => {
148 openContextMenu(event, {
152 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
153 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION,
158 export const openRootProjectContextMenu =
159 (event: React.MouseEvent<HTMLElement>, projectUuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
160 const res = getResource<UserResource>(projectUuid)(getState().resources);
163 openContextMenu(event, {
168 menuKind: ContextMenuKind.ROOT_PROJECT,
175 export const openProjectContextMenu =
176 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
177 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
178 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
179 if (res && menuKind) {
181 openContextMenu(event, {
186 description: res.description,
187 ownerUuid: res.ownerUuid,
188 isTrashed: "isTrashed" in res ? res.isTrashed : false,
189 isFrozen: !!(res as ProjectResource).frozenByUuid,
195 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) => (dispatch: Dispatch, getState: () => RootState) => {
196 if (!isSidePanelTreeCategory(id)) {
197 const kind = extractUuidKind(id);
198 if (kind === ResourceKind.USER) {
199 dispatch<any>(openRootProjectContextMenu(event, id));
200 } else if (kind === ResourceKind.PROJECT) {
201 dispatch<any>(openProjectContextMenu(event, id));
206 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) => (dispatch: Dispatch, getState: () => RootState) => {
207 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
210 openContextMenu(event, {
212 ownerUuid: res.ownerUuid,
213 kind: ResourceKind.PROCESS,
215 description: res.description,
216 outputUuid: res.outputUuid || "",
217 workflowUuid: res.properties.template_uuid || "",
218 menuKind: ContextMenuKind.PROCESS_RESOURCE,
224 export const openRunningProcessContextMenu =
225 (event: React.MouseEvent<HTMLElement>, process: Process) => (dispatch: Dispatch, getState: () => RootState) => {
226 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
229 openContextMenu(event, {
231 ownerUuid: res.ownerUuid,
232 kind: ResourceKind.PROCESS,
234 description: res.description,
235 outputUuid: res.outputUuid || "",
236 workflowUuid: res.properties.template_uuid || "",
237 menuKind: ContextMenuKind.RUNNING_PROCESS_RESOURCE,
243 export const openPermissionEditContextMenu =
244 (event: React.MouseEvent<HTMLElement>, link: LinkResource) => (dispatch: Dispatch, getState: () => RootState) => {
247 openContextMenu(event, {
251 menuKind: ContextMenuKind.PERMISSION_EDIT,
252 ownerUuid: link.ownerUuid,
258 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) => (dispatch: Dispatch, getState: () => RootState) => {
260 openContextMenu(event, {
263 ownerUuid: user.ownerUuid,
265 menuKind: ContextMenuKind.USER,
270 export const resourceUuidToContextMenuKind =
271 (uuid: string, readonly = false) =>
272 (dispatch: Dispatch, getState: () => RootState) => {
273 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
274 const kind = extractUuidKind(uuid);
275 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
276 const isFrozen = resourceIsFrozen(resource, getState().resources);
277 const isEditable = (isAdminUser || (resource || ({} as EditableResource)).isEditable) && !readonly && !isFrozen;
280 case ResourceKind.PROJECT:
282 return isAdminUser ? ContextMenuKind.FROZEN_PROJECT_ADMIN : ContextMenuKind.FROZEN_PROJECT;
285 return isAdminUser && !readonly
286 ? resource && resource.groupClass !== GroupClass.FILTER
287 ? ContextMenuKind.PROJECT_ADMIN
288 : ContextMenuKind.FILTER_GROUP_ADMIN
290 ? resource && resource.groupClass !== GroupClass.FILTER
291 ? ContextMenuKind.PROJECT
292 : ContextMenuKind.FILTER_GROUP
293 : ContextMenuKind.READONLY_PROJECT;
294 case ResourceKind.COLLECTION:
295 const c = getResource<CollectionResource>(uuid)(getState().resources);
296 if (c === undefined) {
299 const isOldVersion = c.uuid !== c.currentVersionUuid;
300 const isTrashed = c.isTrashed;
302 ? ContextMenuKind.OLD_VERSION_COLLECTION
303 : isTrashed && isEditable
304 ? ContextMenuKind.TRASHED_COLLECTION
305 : isAdminUser && isEditable
306 ? ContextMenuKind.COLLECTION_ADMIN
308 ? ContextMenuKind.COLLECTION
309 : ContextMenuKind.READONLY_COLLECTION;
310 case ResourceKind.PROCESS:
311 return isAdminUser && isEditable
312 ? resource && isProcessCancelable(getProcess(resource.uuid)(getState().resources) as Process)
313 ? ContextMenuKind.RUNNING_PROCESS_ADMIN
314 : ContextMenuKind.PROCESS_ADMIN
316 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
317 : resource && isProcessCancelable(getProcess(resource.uuid)(getState().resources) as Process)
318 ? ContextMenuKind.RUNNING_PROCESS_RESOURCE
319 : ContextMenuKind.PROCESS_RESOURCE;
320 case ResourceKind.USER:
321 return ContextMenuKind.ROOT_PROJECT;
322 case ResourceKind.LINK:
323 return ContextMenuKind.LINK;
324 case ResourceKind.WORKFLOW:
325 return isEditable ? ContextMenuKind.WORKFLOW : ContextMenuKind.READONLY_WORKFLOW;
331 export const openSearchResultsContextMenu =
332 (event: React.MouseEvent<HTMLElement>, uuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
333 const res = getResource<Resource>(uuid)(getState().resources);
336 openContextMenu(event, {
341 menuKind: ContextMenuKind.SEARCH_RESULTS,