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: isProcessCancelable(process) ? ContextMenuKind.RUNNING_PROCESS_RESOURCE : ContextMenuKind.PROCESS_RESOURCE
224 export const openPermissionEditContextMenu =
225 (event: React.MouseEvent<HTMLElement>, link: LinkResource) => (dispatch: Dispatch, getState: () => RootState) => {
228 openContextMenu(event, {
232 menuKind: ContextMenuKind.PERMISSION_EDIT,
233 ownerUuid: link.ownerUuid,
239 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) => (dispatch: Dispatch, getState: () => RootState) => {
241 openContextMenu(event, {
244 ownerUuid: user.ownerUuid,
246 menuKind: ContextMenuKind.USER,
251 export const resourceUuidToContextMenuKind =
252 (uuid: string, readonly = false) =>
253 (dispatch: Dispatch, getState: () => RootState) => {
254 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
255 const kind = extractUuidKind(uuid);
256 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
257 const isFrozen = resourceIsFrozen(resource, getState().resources);
258 const isEditable = (isAdminUser || (resource || ({} as EditableResource)).isEditable) && !readonly && !isFrozen;
261 case ResourceKind.PROJECT:
263 return isAdminUser ? ContextMenuKind.FROZEN_PROJECT_ADMIN : ContextMenuKind.FROZEN_PROJECT;
266 return isAdminUser && !readonly
267 ? resource && resource.groupClass !== GroupClass.FILTER
268 ? ContextMenuKind.PROJECT_ADMIN
269 : ContextMenuKind.FILTER_GROUP_ADMIN
271 ? resource && resource.groupClass !== GroupClass.FILTER
272 ? ContextMenuKind.PROJECT
273 : ContextMenuKind.FILTER_GROUP
274 : ContextMenuKind.READONLY_PROJECT;
275 case ResourceKind.COLLECTION:
276 const c = getResource<CollectionResource>(uuid)(getState().resources);
277 if (c === undefined) {
280 const isOldVersion = c.uuid !== c.currentVersionUuid;
281 const isTrashed = c.isTrashed;
283 ? ContextMenuKind.OLD_VERSION_COLLECTION
284 : isTrashed && isEditable
285 ? ContextMenuKind.TRASHED_COLLECTION
286 : isAdminUser && isEditable
287 ? ContextMenuKind.COLLECTION_ADMIN
289 ? ContextMenuKind.COLLECTION
290 : ContextMenuKind.READONLY_COLLECTION;
291 case ResourceKind.PROCESS:
292 return isAdminUser && isEditable
293 ? resource && isProcessCancelable(getProcess(resource.uuid)(getState().resources) as Process)
294 ? ContextMenuKind.RUNNING_PROCESS_ADMIN
295 : ContextMenuKind.PROCESS_ADMIN
297 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
298 : resource && isProcessCancelable(getProcess(resource.uuid)(getState().resources) as Process)
299 ? ContextMenuKind.RUNNING_PROCESS_RESOURCE
300 : ContextMenuKind.PROCESS_RESOURCE;
301 case ResourceKind.USER:
302 return ContextMenuKind.ROOT_PROJECT;
303 case ResourceKind.LINK:
304 return ContextMenuKind.LINK;
305 case ResourceKind.WORKFLOW:
306 return isEditable ? ContextMenuKind.WORKFLOW : ContextMenuKind.READONLY_WORKFLOW;
312 export const openSearchResultsContextMenu =
313 (event: React.MouseEvent<HTMLElement>, uuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
314 const res = getResource<Resource>(uuid)(getState().resources);
317 openContextMenu(event, {
322 menuKind: ContextMenuKind.SEARCH_RESULTS,