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 = 'Favorites',
29 export const SIDE_PANEL_TREE = 'sidePanelTree';
31 export const getSidePanelTree = (treePicker: TreePicker) =>
32 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
34 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
35 const tree = getSidePanelTree(treePicker);
37 const ancestors = getNodeAncestors(uuid)(tree);
38 const node = getNode(uuid)(tree);
40 return [...ancestors, node];
46 const SIDE_PANEL_CATEGORIES = [
47 SidePanelTreeCategory.PUBLIC_FAVORITES,
48 SidePanelTreeCategory.WORKFLOWS,
49 SidePanelTreeCategory.FAVORITES,
50 SidePanelTreeCategory.TRASH,
53 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
55 export const initSidePanelTree = () =>
56 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
57 const rootProjectUuid = getUserUuid(getState());
58 if (!rootProjectUuid) { return; }
59 const nodes = SIDE_PANEL_CATEGORIES.map(id => initTreeNode({ id, value: id }));
60 const projectsNode = initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
61 const sharedNode = initTreeNode({ id: SidePanelTreeCategory.SHARED_WITH_ME, value: SidePanelTreeCategory.SHARED_WITH_ME });
62 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
64 pickerId: SIDE_PANEL_TREE,
65 nodes: [projectsNode, sharedNode, ...nodes]
67 SIDE_PANEL_CATEGORIES.forEach(category => {
68 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
70 pickerId: SIDE_PANEL_TREE,
76 export const loadSidePanelTreeProjects = (projectUuid: string) =>
77 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
79 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
80 if (projectUuid === SidePanelTreeCategory.SHARED_WITH_ME) {
81 await dispatch<any>(loadSharedRoot);
82 } else if (node || projectUuid === '') {
83 await dispatch<any>(loadProject(projectUuid));
87 const loadProject = (projectUuid: string) =>
88 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
89 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
91 filters: new FilterBuilder()
92 .addEqual('owner_uuid', projectUuid)
94 order: new OrderBuilder<ProjectResource>()
98 const { items } = await services.projectService.list(params);
99 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
101 pickerId: SIDE_PANEL_TREE,
102 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
104 dispatch(resourcesActions.SET_RESOURCES(items));
107 const loadSharedRoot = async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
108 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.SHARED_WITH_ME, pickerId: SIDE_PANEL_TREE }));
111 filters: `[${new FilterBuilder()
112 .addIsA('uuid', ResourceKind.PROJECT)
113 .addEqual('group_class', GroupClass.PROJECT)
115 order: new OrderBuilder<ProjectResource>()
116 .addAsc('name', GroupContentsResourcePrefix.PROJECT)
121 const { items } = await services.groupsService.shared(params);
123 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
124 id: SidePanelTreeCategory.SHARED_WITH_ME,
125 pickerId: SIDE_PANEL_TREE,
126 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
129 dispatch(resourcesActions.SET_RESOURCES(items));
132 export const activateSidePanelTreeItem = (id: string) =>
133 async (dispatch: Dispatch, getState: () => RootState) => {
134 const node = getSidePanelTreeNode(id)(getState().treePicker);
135 if (node && !node.active) {
136 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
138 if (!isSidePanelTreeCategory(id)) {
139 await dispatch<any>(activateSidePanelTreeProject(id));
143 export const activateSidePanelTreeProject = (id: string) =>
144 async (dispatch: Dispatch, getState: () => RootState) => {
145 const { treePicker } = getState();
146 const node = getSidePanelTreeNode(id)(treePicker);
147 if (node && node.status !== TreeNodeStatus.LOADED) {
148 await dispatch<any>(loadSidePanelTreeProjects(id));
149 } else if (node === undefined) {
150 await dispatch<any>(activateSidePanelTreeBranch(id));
152 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
153 ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
154 pickerId: SIDE_PANEL_TREE
156 dispatch<any>(expandSidePanelTreeItem(id));
159 export const activateSidePanelTreeBranch = (id: string) =>
160 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
161 const userUuid = getUserUuid(getState());
162 if (!userUuid) { return; }
163 const ancestors = await services.ancestorsService.ancestors(id, userUuid);
164 const isShared = ancestors.every(({ uuid }) => uuid !== userUuid);
166 await dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.SHARED_WITH_ME));
168 for (const ancestor of ancestors) {
169 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
171 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
173 ...(isShared ? [SidePanelTreeCategory.SHARED_WITH_ME] : []),
174 ...ancestors.map(ancestor => ancestor.uuid)
176 pickerId: SIDE_PANEL_TREE
178 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
181 export const toggleSidePanelTreeItemCollapse = (id: string) =>
182 async (dispatch: Dispatch, getState: () => RootState) => {
183 const node = getSidePanelTreeNode(id)(getState().treePicker);
184 if (node && node.status === TreeNodeStatus.INITIAL) {
185 await dispatch<any>(loadSidePanelTreeProjects(node.id));
187 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
190 export const expandSidePanelTreeItem = (id: string) =>
191 async (dispatch: Dispatch, getState: () => RootState) => {
192 const node = getSidePanelTreeNode(id)(getState().treePicker);
193 if (node && !node.expanded) {
194 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
198 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
199 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
201 ? getNode(id)(sidePanelTree)
205 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
206 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
208 ? getNodeAncestorsIds(id)(sidePanelTree)