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, extractUuidObjectType, ResourceObjectType } from 'models/resource';
17 import { CategoriesListReducer } from 'common/plugintypes';
18 import { pluginConfig } from 'plugins';
19 import { LinkClass, LinkResource } from 'models/link';
20 import { verifyAndUpdateLinks } from 'common/link-update-name';
21 import { ProcessIcon, ProjectIcon, FilterGroupIcon, FavoriteIcon, ProjectsIcon, ShareMeIcon, TrashIcon, PublicFavoriteIcon, GroupsIcon, TerminalIcon, ResourceIcon } from 'components/icon/icon';
23 export enum SidePanelTreeCategory {
24 PROJECTS = 'Home Projects',
25 FAVORITES = 'My Favorites',
26 PUBLIC_FAVORITES = 'Public Favorites',
27 SHARED_WITH_ME = 'Shared with me',
28 ALL_PROCESSES = 'All Processes',
29 INSTANCE_TYPES = 'Instance Types',
30 SHELL_ACCESS = 'Shell Access',
35 export const SIDE_PANEL_TREE = 'sidePanelTree';
36 const SIDEPANEL_TREE_NODE_LIMIT = 50
38 export const getSidePanelTree = (treePicker: TreePicker) =>
39 getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
41 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
42 const tree = getSidePanelTree(treePicker);
44 const ancestors = getNodeAncestors(uuid)(tree);
45 const node = getNode(uuid)(tree);
47 return [...ancestors, node];
53 let SIDE_PANEL_CATEGORIES: string[] = [
54 SidePanelTreeCategory.PROJECTS,
55 SidePanelTreeCategory.FAVORITES,
56 SidePanelTreeCategory.PUBLIC_FAVORITES,
57 SidePanelTreeCategory.SHARED_WITH_ME,
58 SidePanelTreeCategory.ALL_PROCESSES,
59 SidePanelTreeCategory.INSTANCE_TYPES,
60 SidePanelTreeCategory.SHELL_ACCESS,
61 SidePanelTreeCategory.GROUPS,
62 SidePanelTreeCategory.TRASH
65 const reduceCatsFn: (a: string[],
66 b: CategoriesListReducer) => string[] = (a, b) => b(a);
68 SIDE_PANEL_CATEGORIES = pluginConfig.sidePanelCategories.reduce(reduceCatsFn, SIDE_PANEL_CATEGORIES);
70 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
73 export const initSidePanelTree = () =>
74 (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
75 const rootProjectUuid = getUserUuid(getState());
76 if (!rootProjectUuid) { return; }
77 const nodes = SIDE_PANEL_CATEGORIES.map(id => {
78 if (id === SidePanelTreeCategory.PROJECTS) {
79 return initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
81 return initTreeNode({ id, value: id });
84 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
86 pickerId: SIDE_PANEL_TREE,
89 SIDE_PANEL_CATEGORIES.forEach(category => {
90 if (category !== SidePanelTreeCategory.PROJECTS && category !== SidePanelTreeCategory.FAVORITES && category !== SidePanelTreeCategory.PUBLIC_FAVORITES ) {
91 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
93 pickerId: SIDE_PANEL_TREE,
100 export const loadSidePanelTreeProjects = (projectUuid: string) =>
101 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
102 const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
103 const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
104 if (projectUuid === SidePanelTreeCategory.PUBLIC_FAVORITES) {
105 const unverifiedPubFaves = await dispatch<any>(loadPublicFavoritesTree());
106 verifyAndUpdateLinkNames(projectUuid, unverifiedPubFaves, dispatch, getState, services);
107 } else if (projectUuid === SidePanelTreeCategory.FAVORITES) {
108 const unverifiedFaves = await dispatch<any>(loadFavoritesTree());
109 await setFaves(unverifiedFaves, dispatch, getState, services);
110 verifyAndUpdateLinkNames(projectUuid, unverifiedFaves, dispatch, getState, services);
111 } else if (node || projectUuid !== '') {
112 await dispatch<any>(loadProject(projectUuid));
116 const loadProject = (projectUuid: string) =>
117 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
119 const objectType = extractUuidObjectType(projectUuid);
120 if (objectType !== ResourceObjectType.USER && objectType !== ResourceObjectType.GROUP) {
124 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
126 filters: new FilterBuilder()
127 .addEqual('owner_uuid', projectUuid)
129 order: new OrderBuilder<ProjectResource>()
130 .addDesc('createdAt')
132 limit: SIDEPANEL_TREE_NODE_LIMIT,
135 const { items } = await services.projectService.list(params);
137 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
139 pickerId: SIDE_PANEL_TREE,
140 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
142 dispatch(resourcesActions.SET_RESOURCES(items));
145 export const loadFavoritesTree = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
146 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.FAVORITES, pickerId: SIDE_PANEL_TREE }));
149 filters: new FilterBuilder()
150 .addEqual('link_class', LinkClass.STAR)
151 .addEqual('tail_uuid', getUserUuid(getState()))
152 .addEqual('tail_kind', ResourceKind.USER)
154 order: new OrderBuilder<ProjectResource>().addDesc('createdAt').getOrder(),
155 limit: SIDEPANEL_TREE_NODE_LIMIT,
158 const { items } = await services.linkService.list(params);
161 treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
162 id: SidePanelTreeCategory.FAVORITES,
163 pickerId: SIDE_PANEL_TREE,
164 nodes: items.map(item => initTreeNode({ id: item.headUuid, value: item.name })),
171 const setFaves = async(links: LinkResource[], dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
173 const uuids = links.map(it => it.headUuid);
174 const groupItems: Promise<any> = services.groupsService.list({
175 filters: new FilterBuilder()
176 .addIn("uuid", uuids)
179 const collectionItems: Promise<any> = services.collectionService.list({
180 filters: new FilterBuilder()
181 .addIn("uuid", uuids)
184 const processItems: Promise<any> = services.containerRequestService.list({
185 filters: new FilterBuilder()
186 .addIn("uuid", uuids)
190 const resolvedItems = await Promise.all([groupItems, collectionItems, processItems]);
192 const responseItems = resolvedItems.reduce((acc, response) => acc.concat(response.items), []);
194 //setting resources here so they won't be re-fetched in validation step
195 dispatch(resourcesActions.SET_RESOURCES(responseItems));
198 const verifyAndUpdateLinkNames = async (category: SidePanelTreeCategory, links: LinkResource[], dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
199 const verfifiedLinks = await verifyAndUpdateLinks(links, dispatch, getState, services);
202 treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
204 pickerId: SIDE_PANEL_TREE,
205 nodes: verfifiedLinks.map(item => initTreeNode({ id: item.headUuid, value: item })),
210 export const loadPublicFavoritesTree = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
211 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.PUBLIC_FAVORITES, pickerId: SIDE_PANEL_TREE }));
213 const uuidPrefix = getState().auth.config.uuidPrefix;
214 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
215 const typeFilters = [ResourceKind.COLLECTION, ResourceKind.CONTAINER_REQUEST, ResourceKind.GROUP, ResourceKind.WORKFLOW];
218 filters: new FilterBuilder()
219 .addEqual('link_class', LinkClass.STAR)
220 .addEqual('owner_uuid', publicProjectUuid)
221 .addIsA('head_uuid', typeFilters)
223 order: new OrderBuilder<ProjectResource>().addDesc('createdAt').getOrder(),
224 limit: SIDEPANEL_TREE_NODE_LIMIT,
227 const { items } = await services.linkService.list(params);
229 const uuids = items.map(it => it.headUuid);
230 const groupItems: Promise<any> = services.groupsService.list({
231 filters: new FilterBuilder()
232 .addIn("uuid", uuids)
233 .addIsA("uuid", typeFilters)
236 const collectionItems: Promise<any> = services.collectionService.list({
237 filters: new FilterBuilder()
238 .addIn("uuid", uuids)
239 .addIsA("uuid", typeFilters)
242 const processItems: Promise<any> = services.containerRequestService.list({
243 filters: new FilterBuilder()
244 .addIn("uuid", uuids)
245 .addIsA("uuid", typeFilters)
249 const resolvedItems = await Promise.all([groupItems, collectionItems, processItems]);
251 const responseItems = resolvedItems.reduce((acc, response) => acc.concat(response.items), []);
253 const filteredItems = items.filter(item => responseItems.some(responseItem => responseItem.uuid === item.headUuid));
256 treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
257 id: SidePanelTreeCategory.PUBLIC_FAVORITES,
258 pickerId: SIDE_PANEL_TREE,
259 nodes: filteredItems.map(item => initTreeNode({ id: item.headUuid, value: item })),
263 //setting resources here so they won't be re-fetched in validation step
264 dispatch(resourcesActions.SET_RESOURCES(responseItems));
266 return filteredItems;
269 export const activateSidePanelTreeItem = (id: string) =>
270 async (dispatch: Dispatch, getState: () => RootState) => {
271 const node = getSidePanelTreeNode(id)(getState().treePicker);
272 if (node && !node.active) {
273 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
275 if (!isSidePanelTreeCategory(id)) {
276 await dispatch<any>(activateSidePanelTreeProject(id));
280 export const activateSidePanelTreeProject = (id: string) =>
281 async (dispatch: Dispatch, getState: () => RootState) => {
282 const { treePicker } = getState();
283 const node = getSidePanelTreeNode(id)(treePicker);
284 if (node && node.status !== TreeNodeStatus.LOADED) {
285 await dispatch<any>(loadSidePanelTreeProjects(id));
286 } else if (node === undefined) {
287 await dispatch<any>(activateSidePanelTreeBranch(id));
289 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
290 ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
291 pickerId: SIDE_PANEL_TREE
293 dispatch<any>(expandSidePanelTreeItem(id));
296 export const activateSidePanelTreeBranch = (id: string) =>
297 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
298 const userUuid = getUserUuid(getState());
299 if (!userUuid) { return; }
300 const ancestors = await services.ancestorsService.ancestors(id, userUuid);
301 for (const ancestor of ancestors) {
302 await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
304 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
305 ids: ancestors.map(ancestor => ancestor.uuid),
306 pickerId: SIDE_PANEL_TREE
308 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
311 export const toggleSidePanelTreeItemCollapse = (id: string) =>
312 async (dispatch: Dispatch, getState: () => RootState) => {
313 const node = getSidePanelTreeNode(id)(getState().treePicker);
314 if (node && node.status === TreeNodeStatus.INITIAL) {
315 await dispatch<any>(loadSidePanelTreeProjects(node.id));
317 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
320 export const expandSidePanelTreeItem = (id: string) =>
321 async (dispatch: Dispatch, getState: () => RootState) => {
322 const node = getSidePanelTreeNode(id)(getState().treePicker);
323 if (node && !node.expanded) {
324 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
328 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
329 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
331 ? getNode(id)(sidePanelTree)
335 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
336 const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
338 ? getNodeAncestorsIds(id)(sidePanelTree)
342 export const getSidePanelIcon = (category: string) => {
344 case SidePanelTreeCategory.FAVORITES:
346 case SidePanelTreeCategory.PROJECTS:
348 case SidePanelTreeCategory.SHARED_WITH_ME:
350 case SidePanelTreeCategory.TRASH:
352 case SidePanelTreeCategory.PUBLIC_FAVORITES:
353 return PublicFavoriteIcon;
354 case SidePanelTreeCategory.ALL_PROCESSES:
356 case SidePanelTreeCategory.INSTANCE_TYPES:
358 case SidePanelTreeCategory.GROUPS:
360 case SidePanelTreeCategory.SHELL_ACCESS: