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 { ServiceRepository } from '~/services/services';
9 import { FilterBuilder } from '~/services/api/filter-builder';
10 import { resourcesActions } from '~/store/resources/resources-actions';
11 import { getTreePicker, TreePicker } from '~/store/tree-picker/tree-picker';
12 import { getNodeAncestors, getNodeAncestorsIds, getNode, TreeNode, initTreeNode, TreeNodeStatus } from '~/models/tree';
13 import { ProjectResource } from '~/models/project';
14 import { OrderBuilder } from '~/services/api/order-builder';
15 import { ResourceKind } from '~/models/resource';
16 import { GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
17 import { GroupClass } from '~/models/group';
19 export enum SidePanelTreeCategory {
20 PROJECTS = 'Projects',
21 SHARED_WITH_ME = 'Shared with me',
22 PUBLIC_FAVORITES = 'Public Favorites',
23 WORKFLOWS = 'Workflows',
24 FAVORITES = 'Favorites',
28 export const SIDE_PANEL_TREE = 'sidePanelTree';
30 export const getSidePanelTree = (treePicker: TreePicker) =>
31 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
33 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
34 const tree = getSidePanelTree(treePicker);
36 const ancestors = getNodeAncestors(uuid)(tree);
37 const node = getNode(uuid)(tree);
39 return [...ancestors, node];
45 const SIDE_PANEL_CATEGORIES = [
46 SidePanelTreeCategory.PUBLIC_FAVORITES,
47 SidePanelTreeCategory.WORKFLOWS,
48 SidePanelTreeCategory.FAVORITES,
49 SidePanelTreeCategory.TRASH,
52 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
54 export const initSidePanelTree = () =>
55 (dispatch: Dispatch, _: () => RootState, { authService }: ServiceRepository) => {
56 const rootProjectUuid = authService.getUuid() || '';
57 const nodes = SIDE_PANEL_CATEGORIES.map(id => initTreeNode({ id, value: id }));
58 const projectsNode = initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
59 const sharedNode = initTreeNode({ id: SidePanelTreeCategory.SHARED_WITH_ME, value: SidePanelTreeCategory.SHARED_WITH_ME });
60 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
62 pickerId: SIDE_PANEL_TREE,
63 nodes: [projectsNode, sharedNode, ...nodes]
65 SIDE_PANEL_CATEGORIES.forEach(category => {
66 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
68 pickerId: SIDE_PANEL_TREE,
74 export const loadSidePanelTreeProjects = (projectUuid: string) =>
75 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
77 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
78 if (projectUuid === SidePanelTreeCategory.SHARED_WITH_ME) {
79 await dispatch<any>(loadSharedRoot);
80 } else if (node || projectUuid === '') {
81 await dispatch<any>(loadProject(projectUuid));
85 const loadProject = (projectUuid: string) =>
86 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
87 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
89 filters: new FilterBuilder()
90 .addEqual('ownerUuid', projectUuid)
92 order: new OrderBuilder<ProjectResource>()
96 const { items } = await services.projectService.list(params);
97 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
99 pickerId: SIDE_PANEL_TREE,
100 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
102 dispatch(resourcesActions.SET_RESOURCES(items));
105 const loadSharedRoot = async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
106 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.SHARED_WITH_ME, pickerId: SIDE_PANEL_TREE }));
109 filters: `[${new FilterBuilder()
110 .addIsA('uuid', ResourceKind.PROJECT)
111 .addEqual('groupClass', GroupClass.PROJECT)
113 order: new OrderBuilder<ProjectResource>()
114 .addAsc('name', GroupContentsResourcePrefix.PROJECT)
119 const { items } = await services.groupsService.shared(params);
121 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
122 id: SidePanelTreeCategory.SHARED_WITH_ME,
123 pickerId: SIDE_PANEL_TREE,
124 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
127 dispatch(resourcesActions.SET_RESOURCES(items));
130 export const activateSidePanelTreeItem = (id: string) =>
131 async (dispatch: Dispatch, getState: () => RootState) => {
132 const node = getSidePanelTreeNode(id)(getState().treePicker);
133 if (node && !node.active) {
134 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
136 if (!isSidePanelTreeCategory(id)) {
137 await dispatch<any>(activateSidePanelTreeProject(id));
141 export const activateSidePanelTreeProject = (id: string) =>
142 async (dispatch: Dispatch, getState: () => RootState) => {
143 const { treePicker } = getState();
144 const node = getSidePanelTreeNode(id)(treePicker);
145 if (node && node.status !== TreeNodeStatus.LOADED) {
146 await dispatch<any>(loadSidePanelTreeProjects(id));
147 } else if (node === undefined) {
148 await dispatch<any>(activateSidePanelTreeBranch(id));
150 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
151 ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
152 pickerId: SIDE_PANEL_TREE
154 dispatch<any>(expandSidePanelTreeItem(id));
157 export const activateSidePanelTreeBranch = (id: string) =>
158 async (dispatch: Dispatch, _: void, services: ServiceRepository) => {
159 const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
160 const isShared = ancestors.every(({ uuid }) => uuid !== services.authService.getUuid());
162 await dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.SHARED_WITH_ME));
164 for (const ancestor of ancestors) {
165 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
167 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
169 ...(isShared ? [SidePanelTreeCategory.SHARED_WITH_ME] : []),
170 ...ancestors.map(ancestor => ancestor.uuid)
172 pickerId: SIDE_PANEL_TREE
174 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
177 export const toggleSidePanelTreeItemCollapse = (id: string) =>
178 async (dispatch: Dispatch, getState: () => RootState) => {
179 const node = getSidePanelTreeNode(id)(getState().treePicker);
180 if (node && node.status === TreeNodeStatus.INITIAL) {
181 await dispatch<any>(loadSidePanelTreeProjects(node.id));
183 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
186 export const expandSidePanelTreeItem = (id: string) =>
187 async (dispatch: Dispatch, getState: () => RootState) => {
188 const node = getSidePanelTreeNode(id)(getState().treePicker);
189 if (node && !node.expanded) {
190 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
194 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
195 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
197 ? getNode(id)(sidePanelTree)
201 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
202 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
204 ? getNodeAncestorsIds(id)(sidePanelTree)