1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
7 import { RootState } from 'store/store';
8 import { getUserUuid } from "common/getuser";
9 import { ServiceRepository } from 'services/services';
10 import { FilterBuilder } from 'services/api/filter-builder';
11 import { resourcesActions } from 'store/resources/resources-actions';
12 import { getTreePicker, TreePicker } from 'store/tree-picker/tree-picker';
13 import { getNodeAncestors, getNodeAncestorsIds, getNode, TreeNode, initTreeNode, TreeNodeStatus } from 'models/tree';
14 import { ProjectResource } from 'models/project';
15 import { OrderBuilder } from 'services/api/order-builder';
16 import { ResourceKind } from 'models/resource';
17 import { CategoriesListReducer } from 'common/plugintypes';
18 import { pluginConfig } from 'plugins';
19 import { LinkClass } from 'models/link';
21 export enum SidePanelTreeCategory {
22 PROJECTS = 'Home Projects',
23 FAVORITES = 'My Favorites',
24 PUBLIC_FAVORITES = 'Public Favorites',
25 SHARED_WITH_ME = 'Shared with me',
26 ALL_PROCESSES = 'All Processes',
27 INSTANCE_TYPES = 'Instance Types',
28 SHELL_ACCESS = 'Shell Access',
33 export const SIDE_PANEL_TREE = 'sidePanelTree';
34 const SIDEPANEL_TREE_NODE_LIMIT = 50
36 export const getSidePanelTree = (treePicker: TreePicker) =>
37 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
39 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
40 const tree = getSidePanelTree(treePicker);
42 const ancestors = getNodeAncestors(uuid)(tree);
43 const node = getNode(uuid)(tree);
45 return [...ancestors, node];
51 let SIDE_PANEL_CATEGORIES: string[] = [
52 SidePanelTreeCategory.PROJECTS,
53 SidePanelTreeCategory.FAVORITES,
54 SidePanelTreeCategory.PUBLIC_FAVORITES,
55 SidePanelTreeCategory.SHARED_WITH_ME,
56 SidePanelTreeCategory.ALL_PROCESSES,
57 SidePanelTreeCategory.INSTANCE_TYPES,
58 SidePanelTreeCategory.SHELL_ACCESS,
59 SidePanelTreeCategory.GROUPS,
60 SidePanelTreeCategory.TRASH
63 const reduceCatsFn: (a: string[],
64 b: CategoriesListReducer) => string[] = (a, b) => b(a);
66 SIDE_PANEL_CATEGORIES = pluginConfig.sidePanelCategories.reduce(reduceCatsFn, SIDE_PANEL_CATEGORIES);
68 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
71 export const initSidePanelTree = () =>
72 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
73 const rootProjectUuid = getUserUuid(getState());
74 if (!rootProjectUuid) { return; }
75 const nodes = SIDE_PANEL_CATEGORIES.map(id => {
76 if (id === SidePanelTreeCategory.PROJECTS) {
77 return initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
79 return initTreeNode({ id, value: id });
82 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
84 pickerId: SIDE_PANEL_TREE,
87 SIDE_PANEL_CATEGORIES.forEach(category => {
88 if (category !== SidePanelTreeCategory.PROJECTS && category !== SidePanelTreeCategory.FAVORITES && category !== SidePanelTreeCategory.PUBLIC_FAVORITES ) {
89 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
91 pickerId: SIDE_PANEL_TREE,
98 export const loadSidePanelTreeProjects = (projectUuid: string) =>
99 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
101 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
102 if (projectUuid === SidePanelTreeCategory.PUBLIC_FAVORITES) {
103 await dispatch<any>(loadPublicFavoritesTree());
104 } else if (projectUuid === SidePanelTreeCategory.FAVORITES) {
105 await dispatch<any>(loadFavoritesTree());
106 } else if (node || projectUuid !== '') {
107 await dispatch<any>(loadProject(projectUuid));
111 const loadProject = (projectUuid: string) =>
112 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
113 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
115 filters: new FilterBuilder()
116 .addEqual('owner_uuid', projectUuid)
118 order: new OrderBuilder<ProjectResource>()
119 .addDesc('createdAt')
121 limit: SIDEPANEL_TREE_NODE_LIMIT,
124 const { items } = await services.projectService.list(params);
126 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
128 pickerId: SIDE_PANEL_TREE,
129 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
131 dispatch(resourcesActions.SET_RESOURCES(items));
134 export const loadFavoritesTree = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
135 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.FAVORITES, pickerId: SIDE_PANEL_TREE }));
138 filters: new FilterBuilder()
139 .addEqual('link_class', LinkClass.STAR)
140 .addEqual('tail_uuid', getUserUuid(getState()))
141 .addEqual('tail_kind', ResourceKind.USER)
143 order: new OrderBuilder<ProjectResource>().addDesc('createdAt').getOrder(),
144 limit: SIDEPANEL_TREE_NODE_LIMIT,
147 const { items } = await services.linkService.list(params);
150 treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
151 id: SidePanelTreeCategory.FAVORITES,
152 pickerId: SIDE_PANEL_TREE,
153 nodes: items.map(item => initTreeNode({ id: item.headUuid, value: item })),
157 dispatch(resourcesActions.SET_RESOURCES(items));
160 export const loadPublicFavoritesTree = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
161 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.PUBLIC_FAVORITES, pickerId: SIDE_PANEL_TREE }));
163 const uuidPrefix = getState().auth.config.uuidPrefix;
164 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
165 const typeFilters = [ResourceKind.COLLECTION, ResourceKind.CONTAINER_REQUEST, ResourceKind.GROUP, ResourceKind.WORKFLOW];
168 filters: new FilterBuilder()
169 .addEqual('link_class', LinkClass.STAR)
170 .addEqual('owner_uuid', publicProjectUuid)
171 .addIsA('head_uuid', typeFilters)
173 order: new OrderBuilder<ProjectResource>().addDesc('createdAt').getOrder(),
174 limit: SIDEPANEL_TREE_NODE_LIMIT,
177 const { items } = await services.linkService.list(params);
180 treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
181 id: SidePanelTreeCategory.PUBLIC_FAVORITES,
182 pickerId: SIDE_PANEL_TREE,
183 nodes: items.map(item => initTreeNode({ id: item.headUuid, value: item })),
187 dispatch(resourcesActions.SET_RESOURCES(items));
190 export const activateSidePanelTreeItem = (id: string) =>
191 async (dispatch: Dispatch, getState: () => RootState) => {
192 const node = getSidePanelTreeNode(id)(getState().treePicker);
193 if (node && !node.active) {
194 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
196 if (!isSidePanelTreeCategory(id)) {
197 await dispatch<any>(activateSidePanelTreeProject(id));
201 export const activateSidePanelTreeProject = (id: string) =>
202 async (dispatch: Dispatch, getState: () => RootState) => {
203 const { treePicker } = getState();
204 const node = getSidePanelTreeNode(id)(treePicker);
205 if (node && node.status !== TreeNodeStatus.LOADED) {
206 await dispatch<any>(loadSidePanelTreeProjects(id));
207 } else if (node === undefined) {
208 await dispatch<any>(activateSidePanelTreeBranch(id));
210 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
211 ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
212 pickerId: SIDE_PANEL_TREE
214 dispatch<any>(expandSidePanelTreeItem(id));
217 export const activateSidePanelTreeBranch = (id: string) =>
218 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
219 const userUuid = getUserUuid(getState());
220 if (!userUuid) { return; }
221 const ancestors = await services.ancestorsService.ancestors(id, userUuid);
222 for (const ancestor of ancestors) {
223 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
225 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
226 ids: ancestors.map(ancestor => ancestor.uuid),
227 pickerId: SIDE_PANEL_TREE
229 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
232 export const toggleSidePanelTreeItemCollapse = (id: string) =>
233 async (dispatch: Dispatch, getState: () => RootState) => {
234 const node = getSidePanelTreeNode(id)(getState().treePicker);
235 if (node && node.status === TreeNodeStatus.INITIAL) {
236 await dispatch<any>(loadSidePanelTreeProjects(node.id));
238 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
241 export const expandSidePanelTreeItem = (id: string) =>
242 async (dispatch: Dispatch, getState: () => RootState) => {
243 const node = getSidePanelTreeNode(id)(getState().treePicker);
244 if (node && !node.expanded) {
245 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
249 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
250 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
252 ? getNode(id)(sidePanelTree)
256 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
257 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
259 ? getNodeAncestorsIds(id)(sidePanelTree)