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';
25 export const contextMenuActions = unionize({
26 OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
27 CLOSE_CONTEXT_MENU: ofType<{}>()
30 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
32 export type ContextMenuResource = {
38 menuKind: ContextMenuKind | string;
42 workflowUuid?: string;
43 storageClassesDesired?: string[];
44 properties?: { [key: string]: string | string[] };
47 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
49 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
50 (dispatch: Dispatch) => {
51 event.preventDefault();
52 const { left, top } = event.currentTarget.getBoundingClientRect();
54 contextMenuActions.OPEN_CONTEXT_MENU({
56 x: event.clientX || left,
57 y: event.clientY || top,
64 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>, isWritable: boolean) =>
65 (dispatch: Dispatch, getState: () => RootState) => {
66 const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
67 dispatch<any>(openContextMenu(event, {
72 kind: ResourceKind.COLLECTION,
73 menuKind: isCollectionFileSelected
75 ? ContextMenuKind.COLLECTION_FILES
76 : ContextMenuKind.READONLY_COLLECTION_FILES
77 : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
81 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
82 (dispatch: Dispatch, getState: () => RootState) => {
83 dispatch<any>(openContextMenu(event, {
85 uuid: repository.uuid,
86 ownerUuid: repository.ownerUuid,
87 kind: ResourceKind.REPOSITORY,
88 menuKind: ContextMenuKind.REPOSITORY
92 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
93 (dispatch: Dispatch, getState: () => RootState) => {
94 dispatch<any>(openContextMenu(event, {
96 uuid: repository.uuid,
97 ownerUuid: repository.ownerUuid,
98 kind: ResourceKind.VIRTUAL_MACHINE,
99 menuKind: ContextMenuKind.VIRTUAL_MACHINE
103 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
104 (dispatch: Dispatch) => {
105 dispatch<any>(openContextMenu(event, {
108 ownerUuid: sshKey.ownerUuid,
109 kind: ResourceKind.SSH_KEY,
110 menuKind: ContextMenuKind.SSH_KEY
114 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
115 (dispatch: Dispatch) => {
116 dispatch<any>(openContextMenu(event, {
118 uuid: keepService.uuid,
119 ownerUuid: keepService.ownerUuid,
120 kind: ResourceKind.KEEP_SERVICE,
121 menuKind: ContextMenuKind.KEEP_SERVICE
125 export const openApiClientAuthorizationContextMenu =
126 (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
127 (dispatch: Dispatch) => {
128 dispatch<any>(openContextMenu(event, {
132 kind: ResourceKind.API_CLIENT_AUTHORIZATION,
133 menuKind: ContextMenuKind.API_CLIENT_AUTHORIZATION
137 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
138 (dispatch: Dispatch, getState: () => RootState) => {
139 const res = getResource<UserResource>(projectUuid)(getState().resources);
141 dispatch<any>(openContextMenu(event, {
146 menuKind: ContextMenuKind.ROOT_PROJECT,
152 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, resourceUuid: string) =>
153 (dispatch: Dispatch, getState: () => RootState) => {
154 const res = getResource<GroupContentsResource>(resourceUuid)(getState().resources);
155 const menuKind = dispatch<any>(resourceUuidToContextMenuKind(resourceUuid));
156 if (res && menuKind) {
157 dispatch<any>(openContextMenu(event, {
162 description: res.description,
163 ownerUuid: res.ownerUuid,
164 isTrashed: ('isTrashed' in res) ? res.isTrashed : false,
169 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
170 (dispatch: Dispatch, getState: () => RootState) => {
171 if (!isSidePanelTreeCategory(id)) {
172 const kind = extractUuidKind(id);
173 if (kind === ResourceKind.USER) {
174 dispatch<any>(openRootProjectContextMenu(event, id));
175 } else if (kind === ResourceKind.PROJECT) {
176 dispatch<any>(openProjectContextMenu(event, id));
181 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
182 (dispatch: Dispatch, getState: () => RootState) => {
183 const res = getResource<ProcessResource>(process.containerRequest.uuid)(getState().resources);
185 dispatch<any>(openContextMenu(event, {
187 ownerUuid: res.ownerUuid,
188 kind: ResourceKind.PROCESS,
190 description: res.description,
191 outputUuid: res.outputUuid || '',
192 workflowUuid: res.properties.template_uuid || '',
193 menuKind: ContextMenuKind.PROCESS_RESOURCE
198 export const openPermissionEditContextMenu = (event: React.MouseEvent<HTMLElement>, link: LinkResource) =>
199 (dispatch: Dispatch, getState: () => RootState) => {
201 dispatch<any>(openContextMenu(event, {
205 menuKind: ContextMenuKind.PERMISSION_EDIT,
206 ownerUuid: link.ownerUuid,
211 export const openUserContextMenu = (event: React.MouseEvent<HTMLElement>, user: UserResource) =>
212 (dispatch: Dispatch, getState: () => RootState) => {
213 dispatch<any>(openContextMenu(event, {
216 ownerUuid: user.ownerUuid,
218 menuKind: ContextMenuKind.USER
222 export const resourceUuidToContextMenuKind = (uuid: string, readonly = false) =>
223 (dispatch: Dispatch, getState: () => RootState) => {
224 const { isAdmin: isAdminUser, uuid: userUuid } = getState().auth.user!;
225 const kind = extractUuidKind(uuid);
226 const resource = getResourceWithEditableStatus<GroupResource & EditableResource>(uuid, userUuid)(getState().resources);
228 const isEditable = (isAdminUser || (resource || {} as EditableResource).isEditable) && !readonly;
230 case ResourceKind.PROJECT:
231 return (isAdminUser && !readonly)
232 ? (resource && resource.groupClass !== GroupClass.FILTER)
233 ? ContextMenuKind.PROJECT_ADMIN
234 : ContextMenuKind.FILTER_GROUP_ADMIN
236 ? (resource && resource.groupClass !== GroupClass.FILTER)
237 ? ContextMenuKind.PROJECT
238 : ContextMenuKind.FILTER_GROUP
239 : ContextMenuKind.READONLY_PROJECT;
240 case ResourceKind.COLLECTION:
241 const c = getResource<CollectionResource>(uuid)(getState().resources);
242 if (c === undefined) { return; }
243 const isOldVersion = c.uuid !== c.currentVersionUuid;
244 const isTrashed = c.isTrashed;
246 ? ContextMenuKind.OLD_VERSION_COLLECTION
247 : (isTrashed && isEditable)
248 ? ContextMenuKind.TRASHED_COLLECTION
249 : (isAdminUser && !readonly)
250 ? ContextMenuKind.COLLECTION_ADMIN
252 ? ContextMenuKind.COLLECTION
253 : ContextMenuKind.READONLY_COLLECTION;
254 case ResourceKind.PROCESS:
255 return (isAdminUser && !readonly)
256 ? ContextMenuKind.PROCESS_ADMIN
258 ? ContextMenuKind.READONLY_PROCESS_RESOURCE
259 : ContextMenuKind.PROCESS_RESOURCE;
260 case ResourceKind.USER:
261 return ContextMenuKind.ROOT_PROJECT;
262 case ResourceKind.LINK:
263 return ContextMenuKind.LINK;
264 case ResourceKind.WORKFLOW:
265 return ContextMenuKind.WORKFLOW;
271 export const openSearchResultsContextMenu = (event: React.MouseEvent<HTMLElement>, uuid: string) =>
272 (dispatch: Dispatch, getState: () => RootState) => {
273 const res = getResource<Resource>(uuid)(getState().resources);
275 dispatch<any>(openContextMenu(event, {
280 menuKind: ContextMenuKind.SEARCH_RESULTS,