19079: Add search results context menu with clipboard and new tab options
[arvados-workbench2.git] / src / store / search-bar / search-bar-tree-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { getTreePicker, TreePicker } from "store/tree-picker/tree-picker";
6 import { getNode, getNodeAncestorsIds, initTreeNode, TreeNodeStatus } from "models/tree";
7 import { Dispatch } from "redux";
8 import { RootState } from "store/store";
9 import { getUserUuid } from "common/getuser";
10 import { ServiceRepository } from "services/services";
11 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
12 import { FilterBuilder } from "services/api/filter-builder";
13 import { OrderBuilder } from "services/api/order-builder";
14 import { ProjectResource } from "models/project";
15 import { resourcesActions } from "store/resources/resources-actions";
16 import { SEARCH_BAR_ADVANCED_FORM_PICKER_ID } from "store/search-bar/search-bar-actions";
17
18 const getSearchBarTreeNode = (id: string) => (treePicker: TreePicker) => {
19     const searchTree = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(treePicker);
20     return searchTree
21         ? getNode(id)(searchTree)
22         : undefined;
23 };
24
25 export const loadSearchBarTreeProjects = (projectUuid: string) =>
26     async (dispatch: Dispatch, getState: () => RootState) => {
27         const treePicker = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(getState().treePicker);
28         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
29         if (node || projectUuid === '') {
30             await dispatch<any>(loadSearchBarProject(projectUuid));
31         }
32     };
33
34 export const getSearchBarTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
35     const searchTree = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(treePicker);
36     return searchTree
37         ? getNodeAncestorsIds(id)(searchTree)
38         : [];
39 };
40
41 export const activateSearchBarTreeBranch = (id: string) =>
42     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43         const userUuid = getUserUuid(getState());
44         if (!userUuid) { return; }
45         const ancestors = await services.ancestorsService.ancestors(id, userUuid);
46
47         for (const ancestor of ancestors) {
48             await dispatch<any>(loadSearchBarTreeProjects(ancestor.uuid));
49         }
50         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
51             ids: [
52                 ...[],
53                 ...ancestors.map(ancestor => ancestor.uuid)
54             ],
55             pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
56         }));
57         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
58     };
59
60 export const expandSearchBarTreeItem = (id: string) =>
61     async (dispatch: Dispatch, getState: () => RootState) => {
62         const node = getSearchBarTreeNode(id)(getState().treePicker);
63         if (node && !node.expanded) {
64             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
65         }
66     };
67
68 export const activateSearchBarProject = (id: string) =>
69     async (dispatch: Dispatch, getState: () => RootState) => {
70         const { treePicker } = getState();
71         const node = getSearchBarTreeNode(id)(treePicker);
72         if (node && node.status !== TreeNodeStatus.LOADED) {
73             await dispatch<any>(loadSearchBarTreeProjects(id));
74         } else if (node === undefined) {
75             await dispatch<any>(activateSearchBarTreeBranch(id));
76         }
77         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
78             ids: getSearchBarTreeNodeAncestorsIds(id)(treePicker),
79             pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
80         }));
81         dispatch<any>(expandSearchBarTreeItem(id));
82     };
83
84
85 const loadSearchBarProject = (projectUuid: string) =>
86     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
87         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
88         const params = {
89             filters: new FilterBuilder()
90                 .addEqual('owner_uuid', projectUuid)
91                 .getFilters(),
92             order: new OrderBuilder<ProjectResource>()
93                 .addAsc('name')
94                 .getOrder()
95         };
96         const { items } = await services.projectService.list(params);
97         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
98             id: projectUuid,
99             pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID,
100             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
101         }));
102         dispatch(resourcesActions.SET_RESOURCES(items));
103     };