Create loadVocabulary action
[arvados.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
19 export const contextMenuActions = unionize({
20     OPEN_CONTEXT_MENU: ofType<{ position: ContextMenuPosition, resource: ContextMenuResource }>(),
21     CLOSE_CONTEXT_MENU: ofType<{}>()
22 });
23
24 export type ContextMenuAction = UnionOf<typeof contextMenuActions>;
25
26 export type ContextMenuResource = {
27     name: string;
28     uuid: string;
29     ownerUuid: string;
30     description?: string;
31     kind: ResourceKind,
32     menuKind: ContextMenuKind;
33     isTrashed?: boolean;
34     index?: number
35 };
36 export const isKeyboardClick = (event: React.MouseEvent<HTMLElement>) =>
37     event.nativeEvent.detail === 0;
38 export const openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: ContextMenuResource) =>
39     (dispatch: Dispatch) => {
40         event.preventDefault();
41         const { left, top } = event.currentTarget.getBoundingClientRect();
42         dispatch(
43             contextMenuActions.OPEN_CONTEXT_MENU({
44                 position: {
45                     x: event.clientX || left,
46                     y: event.clientY || top,
47                 },
48                 resource
49             })
50         );
51     };
52
53 export const openCollectionFilesContextMenu = (event: React.MouseEvent<HTMLElement>) =>
54     (dispatch: Dispatch, getState: () => RootState) => {
55         const isCollectionFileSelected = JSON.stringify(getState().collectionPanelFiles).includes('"selected":true');
56         dispatch<any>(openContextMenu(event, {
57             name: '',
58             uuid: '',
59             ownerUuid: '',
60             kind: ResourceKind.COLLECTION,
61             menuKind: isCollectionFileSelected ? ContextMenuKind.COLLECTION_FILES : ContextMenuKind.COLLECTION_FILES_NOT_SELECTED
62         }));
63     };
64
65 export const openRepositoryContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, repository: RepositoryResource) =>
66     (dispatch: Dispatch, getState: () => RootState) => {
67             dispatch<any>(openContextMenu(event, {
68                 name: '',
69                 uuid: repository.uuid,
70                 ownerUuid: repository.ownerUuid,
71                 kind: ResourceKind.REPOSITORY,
72                 menuKind: ContextMenuKind.REPOSITORY,
73                 index
74             }));
75     };
76
77 export const openSshKeyContextMenu = (event: React.MouseEvent<HTMLElement>, index: number, sshKey: SshKeyResource) =>
78     (dispatch: Dispatch) => {
79         dispatch<any>(openContextMenu(event, {
80             name: '',
81             uuid: sshKey.uuid,
82             ownerUuid: sshKey.ownerUuid,
83             kind: ResourceKind.SSH_KEY,
84             menuKind: ContextMenuKind.SSH_KEY,
85             index
86         }));
87     };
88
89 export const openRootProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
90     (dispatch: Dispatch, getState: () => RootState) => {
91         const res = getResource<UserResource>(projectUuid)(getState().resources);
92         if (res) {
93             dispatch<any>(openContextMenu(event, {
94                 name: '',
95                 uuid: res.uuid,
96                 ownerUuid: res.uuid,
97                 kind: res.kind,
98                 menuKind: ContextMenuKind.ROOT_PROJECT,
99                 isTrashed: false
100             }));
101         }
102     };
103
104 export const openProjectContextMenu = (event: React.MouseEvent<HTMLElement>, projectUuid: string) =>
105     (dispatch: Dispatch, getState: () => RootState) => {
106         const res = getResource<ProjectResource>(projectUuid)(getState().resources);
107         if (res) {
108             dispatch<any>(openContextMenu(event, {
109                 name: res.name,
110                 uuid: res.uuid,
111                 kind: res.kind,
112                 menuKind: ContextMenuKind.PROJECT,
113                 ownerUuid: res.ownerUuid,
114                 isTrashed: res.isTrashed
115             }));
116         }
117     };
118
119 export const openSidePanelContextMenu = (event: React.MouseEvent<HTMLElement>, id: string) =>
120     (dispatch: Dispatch, getState: () => RootState) => {
121         if (!isSidePanelTreeCategory(id)) {
122             const kind = extractUuidKind(id);
123             if (kind === ResourceKind.USER) {
124                 dispatch<any>(openRootProjectContextMenu(event, id));
125             } else if (kind === ResourceKind.PROJECT) {
126                 dispatch<any>(openProjectContextMenu(event, id));
127             }
128         }
129     };
130
131 export const openProcessContextMenu = (event: React.MouseEvent<HTMLElement>, process: Process) =>
132     (dispatch: Dispatch, getState: () => RootState) => {
133         const resource = {
134             uuid: process.containerRequest.uuid,
135             ownerUuid: process.containerRequest.ownerUuid,
136             kind: ResourceKind.PROCESS,
137             name: process.containerRequest.name,
138             description: process.containerRequest.description,
139             menuKind: ContextMenuKind.PROCESS
140         };
141         dispatch<any>(openContextMenu(event, resource));
142     };
143
144 export const resourceKindToContextMenuKind = (uuid: string) => {
145     const kind = extractUuidKind(uuid);
146     switch (kind) {
147         case ResourceKind.PROJECT:
148             return ContextMenuKind.PROJECT;
149         case ResourceKind.COLLECTION:
150             return ContextMenuKind.COLLECTION_RESOURCE;
151         case ResourceKind.PROCESS:
152             return ContextMenuKind.PROCESS_RESOURCE;
153         case ResourceKind.USER:
154             return ContextMenuKind.ROOT_PROJECT;
155         default:
156             return;
157     }
158 };