Merge branch '14563-image-files-thumbnails'
[arvados-workbench2.git] / src / store / context-menu / context-menu-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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 } from '../resources/resources';
11 import { ProjectResource } from '~/models/project';
12 import { UserResource } from '~/models/user';
13 import { isSidePanelTreeCategory } from '~/store/side-panel-tree/side-panel-tree-actions';
14 import { extractUuidKind, ResourceKind } from '~/models/resource';
15 import { Process } from '~/store/processes/process';
16 import { RepositoryResource } from '~/models/repositories';
17 import { SshKeyResource } from '~/models/ssh-key';
18 import { VirtualMachinesResource } from '~/models/virtual-machines';
19 import { KeepServiceResource } from '~/models/keep-services';
20
21 export const contextMenuActions = unionize({
22     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
23     CLOSE_CONTEXT_MENU: ofType<{}>()
24 });
25
26 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
27
28 export type ContextMenuResource = {
29     name: string;
30     uuid: string;
31     ownerUuid: string;
32     description?: string;
33     kind: ResourceKind,
34     menuKind: ContextMenuKind;
35     isTrashed?: boolean;
36     index?: number
37 };
38
39 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) => event.nativeEvent.detail === 0;
40
41 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
42     (dispatch: Dispatch) => {
43         event.preventDefault();
44         const { left, top } = event.currentTarget.getBoundingClientRect();
45         dispatch(
46             contextMenuActions.OPEN_CONTEXT_MENU({
47                 position: {
48                     x: event.clientX || left,
49                     y: event.clientY || top,
50                 },
51                 resource
52             })
53         );
54     };
55
56 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
57     (dispatch: Dispatch, getState: () => RootState) => {
58         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
59         dispatch<any>(openContextMenu(event, {
60             name: '',
61             uuid: '',
62             ownerUuid: '',
63             kind: ResourceKind.COLLECTION,
64             menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
65         }));
66     };
67
68 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, repository: RepositoryResource) =>
69     (dispatch: Dispatch, getState: () => RootState) => {
70         dispatch<any>(openContextMenu(event, {
71             name: '',
72             uuid: repository.uuid,
73             ownerUuid: repository.ownerUuid,
74             kind: ResourceKind.REPOSITORY,
75             menuKind: ContextMenuKind.REPOSITORY
76         }));
77     };
78
79 export const openVirtualMachinesContextMenu = (event: React.MouseEvent<HTMLElement>, repository: VirtualMachinesResource) =>
80     (dispatch: Dispatch, getState: () => RootState) => {
81         dispatch<any>(openContextMenu(event, {
82             name: '',
83             uuid: repository.uuid,
84             ownerUuid: repository.ownerUuid,
85             kind: ResourceKind.VIRTUAL_MACHINE,
86             menuKind: ContextMenuKind.VIRTUAL_MACHINE
87         }));
88     };
89
90 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, sshKey: SshKeyResource) =>
91     (dispatch: Dispatch) => {
92         dispatch<any>(openContextMenu(event, {
93             name: '',
94             uuid: sshKey.uuid,
95             ownerUuid: sshKey.ownerUuid,
96             kind: ResourceKind.SSH_KEY,
97             menuKind: ContextMenuKind.SSH_KEY
98         }));
99     };
100
101 export const openKeepServiceContextMenu = (event: React.MouseEvent<HTMLElement>, keepService: KeepServiceResource) =>
102     (dispatch: Dispatch) => {
103         dispatch<any>(openContextMenu(event, {
104             name: '',
105             uuid: keepService.uuid,
106             ownerUuid: keepService.ownerUuid,
107             kind: ResourceKind.KEEP_SERVICE,
108             menuKind: ContextMenuKind.KEEP_SERVICE
109         }));
110     };
111
112 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
113     (dispatch: Dispatch, getState: () => RootState) => {
114         const res = getResource<UserResource>(projectUuid)(getState().resources);
115         if (res) {
116             dispatch<any>(openContextMenu(event, {
117                 name: '',
118                 uuid: res.uuid,
119                 ownerUuid: res.uuid,
120                 kind: res.kind,
121                 menuKind: ContextMenuKind.ROOT_PROJECT,
122                 isTrashed: false
123             }));
124         }
125     };
126
127 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
128     (dispatch: Dispatch, getState: () => RootState) => {
129         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
130         if (res) {
131             dispatch<any>(openContextMenu(event, {
132                 name: res.name,
133                 uuid: res.uuid,
134                 kind: res.kind,
135                 menuKind: ContextMenuKind.PROJECT,
136                 ownerUuid: res.ownerUuid,
137                 isTrashed: res.isTrashed
138             }));
139         }
140     };
141
142 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
143     (dispatch: Dispatch, getState: () => RootState) => {
144         if (!isSidePanelTreeCategory(id)) {
145             const kind = extractUuidKind(id);
146             if (kind === ResourceKind.USER) {
147                 dispatch<any>(openRootProjectContextMenu(event, id));
148             } else if (kind === ResourceKind.PROJECT) {
149                 dispatch<any>(openProjectContextMenu(event, id));
150             }
151         }
152     };
153
154 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
155     (dispatch: Dispatch, getState: () => RootState) => {
156         const resource = {
157             uuid: process.containerRequest.uuid,
158             ownerUuid: process.containerRequest.ownerUuid,
159             kind: ResourceKind.PROCESS,
160             name: process.containerRequest.name,
161             description: process.containerRequest.description,
162             menuKind: ContextMenuKind.PROCESS
163         };
164         dispatch<any>(openContextMenu(event, resource));
165     };
166
167 export const resourceKindToContextMenuKind = (uuid: string) => {
168     const kind = extractUuidKind(uuid);
169     switch (kind) {
170         case ResourceKind.PROJECT:
171             return ContextMenuKind.PROJECT;
172         case ResourceKind.COLLECTION:
173             return ContextMenuKind.COLLECTION_RESOURCE;
174         case ResourceKind.PROCESS:
175             return ContextMenuKind.PROCESS_RESOURCE;
176         case ResourceKind.USER:
177             return ContextMenuKind.ROOT_PROJECT;
178         default:
179             return;
180     }
181 };