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';
27 export const contextMenuActions = unionize({
28 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
29 CLOSE_CONTEXT_MENU: ofType<{}>()
32 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
34 export type ContextMenuResource = {
40 menuKind: ContextMenuKind | string;
44 workflowUuid?: string;
47 storageClassesDesired?: string[];
48 properties?: { [key: string]: string | string[] };
51 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
53 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
54 (dispatch: Dispatch) => {
55 event.preventDefault();
56 const { left, top } = event.currentTarget.getBoundingClientRect();
58 contextMenuActions.OPEN_CONTEXT_MENU({
60 x: event.clientX || left,
61 y: event.clientY || top,
68 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
69 (dispatch: Dispatch, getState: () => RootState) => {
70 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
71 dispatch<any>(openContextMenu(event, {
76 kind: ResourceKind.COLLECTION,
77 menuKind: isCollectionFileSelected
79 ? ContextMenuKind.COLLECTION_FILES
80 : ContextMenuKind.READONLY_COLLECTION_FILES
81 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
85 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
86 (dispatch: Dispatch, getState: () => RootState) => {
87 dispatch<any>(openContextMenu(event, {
89 uuid: repository.uuid,
90 ownerUuid: repository.ownerUuid,
91 kind: ResourceKind.REPOSITORY,
92 menuKind: ContextMenuKind.REPOSITORY
96 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
97 (dispatch: Dispatch, getState: () => RootState) => {
98 dispatch<any>(openContextMenu(event, {
100 uuid: repository.uuid,
101 ownerUuid: repository.ownerUuid,
102 kind: ResourceKind.VIRTUAL_MACHINE,
103 menuKind: ContextMenuKind.VIRTUAL_MACHINE
107 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
108 (dispatch: Dispatch) => {
109 dispatch<any>(openContextMenu(event, {
112 ownerUuid: sshKey.ownerUuid,
113 kind: ResourceKind.SSH_KEY,
114 menuKind: ContextMenuKind.SSH_KEY
118 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
119 (dispatch: Dispatch) => {
120 dispatch<any>(openContextMenu(event, {
122 uuid: keepService.uuid,
123 ownerUuid: keepService.ownerUuid,
124 kind: ResourceKind.KEEP_SERVICE,
125 menuKind: ContextMenuKind.KEEP_SERVICE
129 export const openApiClientAuthorizationContextMenu =
130 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
131 (dispatch: Dispatch) => {
132 dispatch<any>(openContextMenu(event, {
136 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
137 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
141 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
142 (dispatch: Dispatch, getState: () => RootState) => {
143 const res = getResource<UserResource>(projectUuid)(getState().resources);
145 dispatch<any>(openContextMenu(event, {
150 menuKind: ContextMenuKind.ROOT_PROJECT,
156 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
157 (dispatch: Dispatch, getState: () => RootState) => {
158 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
159 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
160 if (res && menuKind) {
161 dispatch<any>(openContextMenu(event, {
166 description: res.description,
167 ownerUuid: res.ownerUuid,
168 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
169 isFrozen: !!(res as ProjectResource).frozenByUuid,
174 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
175 (dispatch: Dispatch, getState: () => RootState) => {
176 if (!isSidePanelTreeCategory(id)) {
177 const kind = extractUuidKind(id);
178 if (kind === ResourceKind.USER) {
179 dispatch<any>(openRootProjectContextMenu(event, id));
180 } else if (kind === ResourceKind.PROJECT) {
181 dispatch<any>(openProjectContextMenu(event, id));
186 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
187 (dispatch: Dispatch, getState: () => RootState) => {
188 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
190 dispatch<any>(openContextMenu(event, {
192 ownerUuid: res.ownerUuid,
193 kind: ResourceKind.PROCESS,
195 description: res.description,
196 outputUuid: res.outputUuid || '',
197 workflowUuid: res.properties.template_uuid || '',
198 menuKind: ContextMenuKind.PROCESS_RESOURCE
203 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
204 (dispatch: Dispatch, getState: () => RootState) => {
206 dispatch<any>(openContextMenu(event, {
210 menuKind: ContextMenuKind.PERMISSION_EDIT,
211 ownerUuid: link.ownerUuid,
216 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) =>
217 (dispatch: Dispatch, getState: () => RootState) => {
218 dispatch<any>(openContextMenu(event, {
221 ownerUuid: user.ownerUuid,
223 menuKind: ContextMenuKind.USER
227 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
228 (dispatch: Dispatch, getState: () => RootState) => {
229 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
230 const kind = extractUuidKind(uuid);
231 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
232 const isFrozen = resourceIsFrozen(resource, getState().resources);
233 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly && !isFrozen;
236 case ResourceKind.PROJECT:
238 return isAdminUser ? ContextMenuKind.FROZEN_PROJECT_ADMIN : ContextMenuKind.FROZEN_PROJECT;
241 return (isAdminUser && !readonly)
242 ? (resource && resource.groupClass !== GroupClass.FILTER)
243 ? ContextMenuKind.PROJECT_ADMIN
244 : ContextMenuKind.FILTER_GROUP_ADMIN
246 ? (resource && resource.groupClass !== GroupClass.FILTER)
247 ? ContextMenuKind.PROJECT
248 : ContextMenuKind.FILTER_GROUP
249 : ContextMenuKind.READONLY_PROJECT;
250 case ResourceKind.COLLECTION:
251 const c = getResource<CollectionResource>(uuid)(getState().resources);
252 if (c === undefined) { return; }
253 const isOldVersion = c.uuid !== c.currentVersionUuid;
254 const isTrashed = c.isTrashed;
256 ? ContextMenuKind.OLD_VERSION_COLLECTION
257 : (isTrashed && isEditable)
258 ? ContextMenuKind.TRASHED_COLLECTION
259 : (isAdminUser && isEditable)
260 ? ContextMenuKind.COLLECTION_ADMIN
262 ? ContextMenuKind.COLLECTION
263 : ContextMenuKind.READONLY_COLLECTION;
264 case ResourceKind.PROCESS:
265 return (isAdminUser && isEditable)
266 ? ContextMenuKind.PROCESS_ADMIN
268 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
269 : ContextMenuKind.PROCESS_RESOURCE;
270 case ResourceKind.USER:
271 return ContextMenuKind.ROOT_PROJECT;
272 case ResourceKind.LINK:
273 return ContextMenuKind.LINK;
274 case ResourceKind.WORKFLOW:
275 return ContextMenuKind.WORKFLOW;
281 export const openSearchResultsContextMenu = (event: React.MouseEvent<HTMLElement>, uuid: string) =>
282 (dispatch: Dispatch, getState: () => RootState) => {
283 const res = getResource<Resource>(uuid)(getState().resources);
285 dispatch<any>(openContextMenu(event, {
290 menuKind: ContextMenuKind.SEARCH_RESULTS,