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 { GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
18 import { GroupClass } from '~/models/group';
20 export enum SidePanelTreeCategory {
21 PROJECTS = 'Projects',
22 SHARED_WITH_ME = 'Shared with me',
23 PUBLIC_FAVORITES = 'Public Favorites',
24 WORKFLOWS = 'Workflows',
25 FAVORITES = 'My Favorites',
27 ALL_PROCESSES = 'All Processes'
30 export const SIDE_PANEL_TREE = 'sidePanelTree';
32 export const getSidePanelTree = (treePicker: TreePicker) =>
33 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
35 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
36 const tree = getSidePanelTree(treePicker);
38 const ancestors = getNodeAncestors(uuid)(tree);
39 const node = getNode(uuid)(tree);
41 return [...ancestors, node];
47 const SIDE_PANEL_CATEGORIES = [
48 SidePanelTreeCategory.PUBLIC_FAVORITES,
49 SidePanelTreeCategory.FAVORITES,
50 SidePanelTreeCategory.WORKFLOWS,
51 SidePanelTreeCategory.ALL_PROCESSES,
52 SidePanelTreeCategory.TRASH,
55 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
57 export const initSidePanelTree = () =>
58 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
59 const rootProjectUuid = getUserUuid(getState());
60 if (!rootProjectUuid) { return; }
61 const nodes = SIDE_PANEL_CATEGORIES.map(id => initTreeNode({ id, value: id }));
62 const projectsNode = initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
63 const sharedNode = initTreeNode({ id: SidePanelTreeCategory.SHARED_WITH_ME, value: SidePanelTreeCategory.SHARED_WITH_ME });
64 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
66 pickerId: SIDE_PANEL_TREE,
67 nodes: [projectsNode, sharedNode, ...nodes]
69 SIDE_PANEL_CATEGORIES.forEach(category => {
70 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
72 pickerId: SIDE_PANEL_TREE,
78 export const loadSidePanelTreeProjects = (projectUuid: string) =>
79 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
80 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
81 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
82 if (projectUuid === SidePanelTreeCategory.SHARED_WITH_ME) {
83 await dispatch<any>(loadSharedRoot);
84 } else if (node || projectUuid === '') {
85 await dispatch<any>(loadProject(projectUuid));
89 const loadProject = (projectUuid: string) =>
90 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
91 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
93 filters: new FilterBuilder()
94 .addEqual('owner_uuid', projectUuid)
96 order: new OrderBuilder<ProjectResource>()
100 const { items } = await services.projectService.list(params);
101 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
103 pickerId: SIDE_PANEL_TREE,
104 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
106 dispatch(resourcesActions.SET_RESOURCES(items));
109 const loadSharedRoot = async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
110 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.SHARED_WITH_ME, pickerId: SIDE_PANEL_TREE }));
113 filters: `[${new FilterBuilder()
114 .addIsA('uuid', ResourceKind.PROJECT)
115 .addEqual('group_class', GroupClass.PROJECT)
116 .addDistinct('uuid', getState().auth.config.uuidPrefix + '-j7d0g-publicfavorites')
118 order: new OrderBuilder<ProjectResource>()
119 .addAsc('name', GroupContentsResourcePrefix.PROJECT)
124 const { items } = await services.groupsService.shared(params);
126 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
127 id: SidePanelTreeCategory.SHARED_WITH_ME,
128 pickerId: SIDE_PANEL_TREE,
129 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
132 dispatch(resourcesActions.SET_RESOURCES(items));
135 export const activateSidePanelTreeItem = (id: string) =>
136 async (dispatch: Dispatch, getState: () => RootState) => {
137 const node = getSidePanelTreeNode(id)(getState().treePicker);
138 if (node && !node.active) {
139 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
141 if (!isSidePanelTreeCategory(id)) {
142 await dispatch<any>(activateSidePanelTreeProject(id));
146 export const activateSidePanelTreeProject = (id: string) =>
147 async (dispatch: Dispatch, getState: () => RootState) => {
148 const { treePicker } = getState();
149 const node = getSidePanelTreeNode(id)(treePicker);
150 if (node && node.status !== TreeNodeStatus.LOADED) {
151 await dispatch<any>(loadSidePanelTreeProjects(id));
152 } else if (node === undefined) {
153 await dispatch<any>(activateSidePanelTreeBranch(id));
155 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
156 ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
157 pickerId: SIDE_PANEL_TREE
159 dispatch<any>(expandSidePanelTreeItem(id));
162 export const activateSidePanelTreeBranch = (id: string) =>
163 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
164 const userUuid = getUserUuid(getState());
165 if (!userUuid) { return; }
166 const ancestors = await services.ancestorsService.ancestors(id, userUuid);
167 const isShared = ancestors.every(({ uuid }) => uuid !== userUuid);
169 await dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.SHARED_WITH_ME));
171 for (const ancestor of ancestors) {
172 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
174 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
176 ...(isShared ? [SidePanelTreeCategory.SHARED_WITH_ME] : []),
177 ...ancestors.map(ancestor => ancestor.uuid)
179 pickerId: SIDE_PANEL_TREE
181 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
184 export const toggleSidePanelTreeItemCollapse = (id: string) =>
185 async (dispatch: Dispatch, getState: () => RootState) => {
186 const node = getSidePanelTreeNode(id)(getState().treePicker);
187 if (node && node.status === TreeNodeStatus.INITIAL) {
188 await dispatch<any>(loadSidePanelTreeProjects(node.id));
190 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
193 export const expandSidePanelTreeItem = (id: string) =>
194 async (dispatch: Dispatch, getState: () => RootState) => {
195 const node = getSidePanelTreeNode(id)(getState().treePicker);
196 if (node && !node.expanded) {
197 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
201 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
202 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
204 ? getNode(id)(sidePanelTree)
208 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
209 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
211 ? getNodeAncestorsIds(id)(sidePanelTree)