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 } 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 { filterCollectionFilesBySelection } from "store/collection-panel/collection-panel-files/collection-panel-files-state";
28 export const contextMenuActions = unionize({
29 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition; resource: ContextMenuResource }>(),
30 CLOSE_CONTEXT_MENU: ofType<{}>(),
33 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
35 export type ContextMenuResource = {
41 menuKind: ContextMenuKind | string;
45 workflowUuid?: string;
48 storageClassesDesired?: string[];
49 properties?: { [key: string]: string | string[] };
51 fromContextMenu?: boolean;
54 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
56 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) => (dispatch: Dispatch) => {
57 event.preventDefault();
58 const { left, top } = event.currentTarget.getBoundingClientRect();
60 contextMenuActions.OPEN_CONTEXT_MENU({
62 x: event.clientX || left,
63 y: event.clientY || top,
70 export const openCollectionFilesContextMenu =
71 (event: React.MouseEvent<HTMLElement>, isWritable: boolean) => (dispatch: Dispatch, getState: () => RootState) => {
72 const selectedCount = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).length;
73 const multiple = selectedCount > 1;
75 openContextMenu(event, {
80 kind: ResourceKind.COLLECTION,
85 ? ContextMenuKind.COLLECTION_FILES_MULTIPLE
86 : ContextMenuKind.COLLECTION_FILES
88 ? ContextMenuKind.READONLY_COLLECTION_FILES_MULTIPLE
89 : ContextMenuKind.READONLY_COLLECTION_FILES
90 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED,
95 export const openRepositoryContextMenu =
96 (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) => (dispatch: Dispatch, getState: () => RootState) => {
98 openContextMenu(event, {
100 uuid: repository.uuid,
101 ownerUuid: repository.ownerUuid,
102 kind: ResourceKind.REPOSITORY,
103 menuKind: ContextMenuKind.REPOSITORY,
108 export const openVirtualMachinesContextMenu =
109 (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) => (dispatch: Dispatch, getState: () => RootState) => {
111 openContextMenu(event, {
113 uuid: repository.uuid,
114 ownerUuid: repository.ownerUuid,
115 kind: ResourceKind.VIRTUAL_MACHINE,
116 menuKind: ContextMenuKind.VIRTUAL_MACHINE,
121 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) => (dispatch: Dispatch) => {
123 openContextMenu(event, {
126 ownerUuid: sshKey.ownerUuid,
127 kind: ResourceKind.SSH_KEY,
128 menuKind: ContextMenuKind.SSH_KEY,
133 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) => (dispatch: Dispatch) => {
135 openContextMenu(event, {
137 uuid: keepService.uuid,
138 ownerUuid: keepService.ownerUuid,
139 kind: ResourceKind.KEEP_SERVICE,
140 menuKind: ContextMenuKind.KEEP_SERVICE,
145 export const openApiClientAuthorizationContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => (dispatch: Dispatch) => {
147 openContextMenu(event, {
151 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
152 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION,
157 export const openRootProjectContextMenu =
158 (event: React.MouseEvent<HTMLElement>, projectUuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
159 const res = getResource<UserResource>(projectUuid)(getState().resources);
162 openContextMenu(event, {
167 menuKind: ContextMenuKind.ROOT_PROJECT,
174 export const openProjectContextMenu =
175 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
176 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
177 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
178 if (res && menuKind) {
180 openContextMenu(event, {
185 description: res.description,
186 ownerUuid: res.ownerUuid,
187 isTrashed: "isTrashed" in res ? res.isTrashed : false,
188 isFrozen: !!(res as ProjectResource).frozenByUuid,
194 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) => (dispatch: Dispatch, getState: () => RootState) => {
195 if (!isSidePanelTreeCategory(id)) {
196 const kind = extractUuidKind(id);
197 if (kind === ResourceKind.USER) {
198 dispatch<any>(openRootProjectContextMenu(event, id));
199 } else if (kind === ResourceKind.PROJECT) {
200 dispatch<any>(openProjectContextMenu(event, id));
205 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) => (dispatch: Dispatch, getState: () => RootState) => {
206 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
209 openContextMenu(event, {
211 ownerUuid: res.ownerUuid,
212 kind: ResourceKind.PROCESS,
214 description: res.description,
215 outputUuid: res.outputUuid || "",
216 workflowUuid: res.properties.template_uuid || "",
217 menuKind: ContextMenuKind.PROCESS_RESOURCE,
223 export const openPermissionEditContextMenu =
224 (event: React.MouseEvent<HTMLElement>, link: LinkResource) => (dispatch: Dispatch, getState: () => RootState) => {
227 openContextMenu(event, {
231 menuKind: ContextMenuKind.PERMISSION_EDIT,
232 ownerUuid: link.ownerUuid,
238 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) => (dispatch: Dispatch, getState: () => RootState) => {
240 openContextMenu(event, {
243 ownerUuid: user.ownerUuid,
245 menuKind: ContextMenuKind.USER,
250 export const resourceUuidToContextMenuKind =
251 (uuid: string, readonly = false) =>
252 (dispatch: Dispatch, getState: () => RootState) => {
253 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
254 const kind = extractUuidKind(uuid);
255 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
256 const isFrozen = resourceIsFrozen(resource, getState().resources);
257 const isEditable = (isAdminUser || (resource || ({} as EditableResource)).isEditable) && !readonly && !isFrozen;
260 case ResourceKind.PROJECT:
262 return isAdminUser ? ContextMenuKind.FROZEN_PROJECT_ADMIN : ContextMenuKind.FROZEN_PROJECT;
265 return isAdminUser && !readonly
266 ? resource && resource.groupClass !== GroupClass.FILTER
267 ? ContextMenuKind.PROJECT_ADMIN
268 : ContextMenuKind.FILTER_GROUP_ADMIN
270 ? resource && resource.groupClass !== GroupClass.FILTER
271 ? ContextMenuKind.PROJECT
272 : ContextMenuKind.FILTER_GROUP
273 : ContextMenuKind.READONLY_PROJECT;
274 case ResourceKind.COLLECTION:
275 const c = getResource<CollectionResource>(uuid)(getState().resources);
276 if (c === undefined) {
279 const isOldVersion = c.uuid !== c.currentVersionUuid;
280 const isTrashed = c.isTrashed;
282 ? ContextMenuKind.OLD_VERSION_COLLECTION
283 : isTrashed && isEditable
284 ? ContextMenuKind.TRASHED_COLLECTION
285 : isAdminUser && isEditable
286 ? ContextMenuKind.COLLECTION_ADMIN
288 ? ContextMenuKind.COLLECTION
289 : ContextMenuKind.READONLY_COLLECTION;
290 case ResourceKind.PROCESS:
291 return isAdminUser && isEditable
292 ? ContextMenuKind.PROCESS_ADMIN
294 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
295 : ContextMenuKind.PROCESS_RESOURCE;
296 case ResourceKind.USER:
297 return ContextMenuKind.ROOT_PROJECT;
298 case ResourceKind.LINK:
299 return ContextMenuKind.LINK;
300 case ResourceKind.WORKFLOW:
301 return isEditable ? ContextMenuKind.WORKFLOW : ContextMenuKind.READONLY_WORKFLOW;
307 export const openSearchResultsContextMenu =
308 (event: React.MouseEvent<HTMLElement>, uuid: string) => (dispatch: Dispatch, getState: () => RootState) => {
309 const res = getResource<Resource>(uuid)(getState().resources);
312 openContextMenu(event, {
317 menuKind: ContextMenuKind.SEARCH_RESULTS,