1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { unionize, ofType, UnionOf } from "common/unionize";
6 import { TreeNode, initTreeNode, getNodeDescendants, TreeNodeStatus, getNode, TreePickerId, Tree, setNode, createTree } from 'models/tree';
7 import { CollectionFileType, createCollectionFilesTree, getCollectionResourceCollectionUuid } from "models/collection-file";
8 import { Dispatch } from 'redux';
9 import { RootState } from 'store/store';
10 import { getUserUuid } from "common/getuser";
11 import { ServiceRepository } from 'services/services';
12 import { FilterBuilder } from 'services/api/filter-builder';
13 import { pipe, values } from 'lodash/fp';
14 import { ResourceKind } from 'models/resource';
15 import { GroupContentsResource } from 'services/groups-service/groups-service';
16 import { getTreePicker, TreePicker } from './tree-picker';
17 import { ProjectsTreePickerItem } from './tree-picker-middleware';
18 import { OrderBuilder } from 'services/api/order-builder';
19 import { ProjectResource } from 'models/project';
20 import { mapTree } from '../../models/tree';
21 import { LinkResource, LinkClass } from "models/link";
22 import { mapTreeValues } from "models/tree";
23 import { sortFilesTree } from "services/collection-service/collection-service-files-response";
24 import { GroupClass, GroupResource } from "models/group";
25 import { CollectionResource } from "models/collection";
26 import { getResource } from "store/resources/resources";
27 import { updateResources } from "store/resources/resources-actions";
28 import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
30 export const treePickerActions = unionize({
31 LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
32 LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
33 APPEND_TREE_PICKER_NODE_SUBTREE: ofType<{ id: string, subtree: Tree<any>, pickerId: string }>(),
34 TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
35 EXPAND_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
36 EXPAND_TREE_PICKER_NODE_ANCESTORS: ofType<{ id: string, pickerId: string }>(),
37 ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string, relatedTreePickers?: string[] }>(),
38 DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
39 TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string, cascade: boolean }>(),
40 SELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string, cascade: boolean }>(),
41 DESELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string, cascade: boolean }>(),
42 EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
43 RESET_TREE_PICKER: ofType<{ pickerId: string }>()
46 export type TreePickerAction = UnionOf<typeof treePickerActions>;
48 export interface LoadProjectParams {
49 includeCollections?: boolean;
50 includeDirectories?: boolean;
51 includeFiles?: boolean;
52 includeFilterGroups?: boolean;
53 options?: { showOnlyOwned: boolean; showOnlyWritable: boolean; };
56 export const treePickerSearchActions = unionize({
57 SET_TREE_PICKER_PROJECT_SEARCH: ofType<{ pickerId: string, projectSearchValue: string }>(),
58 SET_TREE_PICKER_COLLECTION_FILTER: ofType<{ pickerId: string, collectionFilterValue: string }>(),
59 SET_TREE_PICKER_LOAD_PARAMS: ofType<{ pickerId: string, params: LoadProjectParams }>(),
60 REFRESH_TREE_PICKER: ofType<{ pickerId: string }>(),
63 export type TreePickerSearchAction = UnionOf<typeof treePickerSearchActions>;
65 export const getProjectsTreePickerIds = (pickerId: string) => ({
66 home: `${pickerId}_home`,
67 shared: `${pickerId}_shared`,
68 favorites: `${pickerId}_favorites`,
69 publicFavorites: `${pickerId}_publicFavorites`,
70 search: `${pickerId}_search`,
73 export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
75 () => values(getProjectsTreePickerIds(pickerId)),
78 .map(id => getTreePicker<Value>(id)(state)),
81 .map(getNodeDescendants(''))
82 .reduce((allNodes, nodes) => allNodes.concat(nodes), []),
85 .reduce((map, node) =>
87 ? map.set(node.id, node)
88 : map, new Map<string, TreeNode<Value>>())
91 uniqueNodes => Array.from(uniqueNodes),
93 export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) =>
94 getAllNodes<Value>(pickerId, node => node.selected)(state);
96 interface TreePickerPreloadParams {
97 selectedItemUuids: string[];
98 includeDirectories: boolean;
99 includeFiles: boolean;
103 export const initProjectsTreePicker = (pickerId: string, preloadParams?: TreePickerPreloadParams) =>
104 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
105 const { home, shared, favorites, publicFavorites, search } = getProjectsTreePickerIds(pickerId);
106 dispatch<any>(initUserProject(home));
107 dispatch<any>(initSharedProject(shared));
108 dispatch<any>(initFavoritesProject(favorites));
109 dispatch<any>(initPublicFavoritesProject(publicFavorites));
110 dispatch<any>(initSearchProject(search));
112 if (preloadParams && preloadParams.selectedItemUuids.length) {
113 await dispatch<any>(loadInitialValue(
114 preloadParams.selectedItemUuids,
116 preloadParams.includeDirectories,
117 preloadParams.includeFiles,
123 interface ReceiveTreePickerDataParams<T> {
125 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
130 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
131 (dispatch: Dispatch) => {
132 const { data, extractNodeData, id, pickerId, } = params;
133 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
135 nodes: data.map(item => initTreeNode(extractNodeData(item))),
138 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODE({ id, pickerId }));
141 interface LoadProjectParamsWithId extends LoadProjectParams {
144 loadShared?: boolean;
145 searchProjects?: boolean;
149 * loadProject is used to load or refresh a project node in a tree picker
150 * Errors are caught and a toast is shown if the project fails to load
152 export const loadProject = (params: LoadProjectParamsWithId) =>
153 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
157 includeCollections = false,
158 includeDirectories = false,
159 includeFiles = false,
160 includeFilterGroups = false,
163 searchProjects = false
166 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
168 let filterB = new FilterBuilder();
170 filterB = (includeCollections && !searchProjects)
171 ? filterB.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
172 : filterB.addIsA('uuid', [ResourceKind.PROJECT]);
174 const state = getState();
176 if (state.treePickerSearch.collectionFilterValues[pickerId]) {
177 filterB = filterB.addFullTextSearch(state.treePickerSearch.collectionFilterValues[pickerId], 'collections');
179 filterB = filterB.addNotIn("collections.properties.type", ["intermediate", "log"]);
182 if (searchProjects && state.treePickerSearch.projectSearchValues[pickerId]) {
183 filterB = filterB.addFullTextSearch(state.treePickerSearch.projectSearchValues[pickerId], 'groups');
186 const filters = filterB.getFilters();
188 const itemLimit = 200;
191 const { items, itemsAvailable } = await services.groupsService.contents((loadShared || searchProjects) ? '' : id, { filters, excludeHomeProject: loadShared || undefined, limit: itemLimit });
192 dispatch<any>(updateResources(items));
194 if (itemsAvailable > itemLimit) {
196 uuid: "more-items-available",
197 kind: ResourceKind.WORKFLOW,
198 name: `*** Not all items listed (${items.length} out of ${itemsAvailable}), reduce item count with search or filter ***`,
203 modifiedByClientUuid: "",
204 modifiedByUserUuid: "",
211 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
214 data: items.filter((item) => {
215 if (!includeFilterGroups && (item as GroupResource).groupClass && (item as GroupResource).groupClass === GroupClass.FILTER) {
219 if (options && options.showOnlyWritable && item.hasOwnProperty('frozenByUuid') && (item as ProjectResource).frozenByUuid) {
225 extractNodeData: item => (
226 item.uuid === "more-items-available" ?
230 status: TreeNodeStatus.LOADED
235 status: item.kind === ResourceKind.PROJECT
236 ? TreeNodeStatus.INITIAL
237 : includeDirectories || includeFiles
238 ? TreeNodeStatus.INITIAL
239 : TreeNodeStatus.LOADED
243 console.error("Failed to load project into tree picker:", e);;
244 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Failed to load project`, kind: SnackbarKind.ERROR }));
248 export const loadCollection = (id: string, pickerId: string, includeDirectories?: boolean, includeFiles?: boolean) =>
249 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
250 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
252 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
255 const node = getNode(id)(picker);
256 if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
257 const files = (await services.collectionService.files(node.value.uuid))
260 (includeDirectories && file.type === CollectionFileType.DIRECTORY)
262 const tree = createCollectionFilesTree(files);
263 const sorted = sortFilesTree(tree);
264 const filesTree = mapTreeValues(services.collectionService.extendFileURL)(sorted);
266 // await tree modifications so that consumers can guarantee node presence
268 treePickerActions.APPEND_TREE_PICKER_NODE_SUBTREE({
271 subtree: mapTree(node => ({ ...node, status: TreeNodeStatus.LOADED }))(filesTree)
274 // Expand collection root node
275 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
280 export const HOME_PROJECT_ID = 'Home Projects';
281 export const initUserProject = (pickerId: string) =>
282 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
283 const uuid = getUserUuid(getState());
285 dispatch(receiveTreePickerData({
288 data: [{ uuid, name: HOME_PROJECT_ID }],
289 extractNodeData: value => ({
291 status: TreeNodeStatus.INITIAL,
297 export const loadUserProject = (pickerId: string, includeCollections = false, includeDirectories = false, includeFiles = false, options?: { showOnlyOwned: boolean, showOnlyWritable: boolean }) =>
298 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
299 const uuid = getUserUuid(getState());
301 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeDirectories, includeFiles, options }));
305 export const SHARED_PROJECT_ID = 'Shared with me';
306 export const initSharedProject = (pickerId: string) =>
307 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
308 dispatch(receiveTreePickerData({
311 data: [{ uuid: SHARED_PROJECT_ID, name: SHARED_PROJECT_ID }],
312 extractNodeData: value => ({
314 status: TreeNodeStatus.INITIAL,
320 type PickerItemPreloadData = {
322 mainItemUuid: string;
323 ancestors: (GroupResource | CollectionResource)[];
324 isHomeProjectItem: boolean;
327 type PickerTreePreloadData = {
328 tree: Tree<GroupResource | CollectionResource>;
329 pickerTreeId: string;
330 pickerTreeRootUuid: string;
333 export const loadInitialValue = (pickerItemIds: string[], pickerId: string, includeDirectories: boolean, includeFiles: boolean, multi: boolean,) =>
334 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
335 const homeUuid = getUserUuid(getState());
337 // Request ancestor trees in paralell and save home project status
338 const pickerItemsData: PickerItemPreloadData[] = await Promise.allSettled(pickerItemIds.map(async itemId => {
339 const mainItemUuid = itemId.includes('/') ? itemId.split('/')[0] : itemId;
341 const ancestors = (await services.ancestorsService.ancestors(mainItemUuid, ''))
343 item.kind === ResourceKind.GROUP ||
344 item.kind === ResourceKind.COLLECTION
345 ) as (GroupResource | CollectionResource)[];
347 if (ancestors.length === 0) {
348 return Promise.reject({item: itemId});
351 const isHomeProjectItem = !!(homeUuid && ancestors.some(item => item.ownerUuid === homeUuid));
360 // Show toast if any selections failed to restore
361 const rejectedPromises = res.filter((promiseResult): promiseResult is PromiseRejectedResult => (promiseResult.status === 'rejected'));
362 if (rejectedPromises.length) {
363 rejectedPromises.forEach(item => {
364 console.error("The following item failed to load into the tree picker", item.reason);
366 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `Some selections failed to load and were removed. See console for details.`, kind: SnackbarKind.ERROR }));
368 // Filter out any failed promises and map to resulting preload data with ancestors
369 return res.filter((promiseResult): promiseResult is PromiseFulfilledResult<PickerItemPreloadData> => (
370 promiseResult.status === 'fulfilled'
371 )).map(res => res.value)
374 // Group items to preload / ancestor data by home/shared picker and create initial Trees to preload
375 const initialTreePreloadData: PickerTreePreloadData[] = [
376 pickerItemsData.filter((item) => item.isHomeProjectItem),
377 pickerItemsData.filter((item) => !item.isHomeProjectItem),
379 .filter((items) => items.length > 0)
382 (preloadTree, itemData) => ({
383 tree: createInitialPickerTree(
385 itemData.mainItemUuid,
388 pickerTreeId: getPickerItemTreeId(itemData, homeUuid, pickerId),
389 pickerTreeRootUuid: getPickerItemRootUuid(itemData, homeUuid),
392 tree: createTree<GroupResource | CollectionResource>(),
394 pickerTreeRootUuid: '',
395 } as PickerTreePreloadData
399 // Load initial trees into corresponding picker store
400 await Promise.all(initialTreePreloadData.map(preloadTree => (
402 treePickerActions.APPEND_TREE_PICKER_NODE_SUBTREE({
403 id: preloadTree.pickerTreeRootUuid,
404 pickerId: preloadTree.pickerTreeId,
405 subtree: preloadTree.tree,
410 // Await loading collection before attempting to select items
411 await Promise.all(pickerItemsData.map(async itemData => {
412 const pickerTreeId = getPickerItemTreeId(itemData, homeUuid, pickerId);
414 // Selected item resides in collection subpath
415 if (itemData.itemId.includes('/')) {
416 // Load collection into tree
417 // loadCollection includes more than dispatched actions and must be awaited
418 await dispatch(loadCollection(itemData.mainItemUuid, pickerTreeId, includeDirectories, includeFiles));
420 // Expand nodes down to destination
421 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODE_ANCESTORS({ id: itemData.itemId, pickerId: pickerTreeId }));
424 // Select or activate nodes
425 pickerItemsData.forEach(itemData => {
426 const pickerTreeId = getPickerItemTreeId(itemData, homeUuid, pickerId);
429 dispatch(treePickerActions.SELECT_TREE_PICKER_NODE({ id: itemData.itemId, pickerId: pickerTreeId, cascade: false}));
431 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: itemData.itemId, pickerId: pickerTreeId }));
435 // Refresh triggers loading in all adjacent items that were not included in the ancestor tree
436 await initialTreePreloadData.map(preloadTree => dispatch(treePickerSearchActions.REFRESH_TREE_PICKER({ pickerId: preloadTree.pickerTreeId })));
439 const getPickerItemTreeId = (itemData: PickerItemPreloadData, homeUuid: string | undefined, pickerId: string) => {
440 const { home, shared } = getProjectsTreePickerIds(pickerId);
441 return ((itemData.isHomeProjectItem && homeUuid) ? home : shared);
444 const getPickerItemRootUuid = (itemData: PickerItemPreloadData, homeUuid: string | undefined) => {
445 return (itemData.isHomeProjectItem && homeUuid) ? homeUuid : SHARED_PROJECT_ID;
448 export const FAVORITES_PROJECT_ID = 'Favorites';
449 export const initFavoritesProject = (pickerId: string) =>
450 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
451 dispatch(receiveTreePickerData({
454 data: [{ uuid: FAVORITES_PROJECT_ID, name: FAVORITES_PROJECT_ID }],
455 extractNodeData: value => ({
457 status: TreeNodeStatus.INITIAL,
463 export const PUBLIC_FAVORITES_PROJECT_ID = 'Public Favorites';
464 export const initPublicFavoritesProject = (pickerId: string) =>
465 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
466 dispatch(receiveTreePickerData({
469 data: [{ uuid: PUBLIC_FAVORITES_PROJECT_ID, name: PUBLIC_FAVORITES_PROJECT_ID }],
470 extractNodeData: value => ({
472 status: TreeNodeStatus.INITIAL,
478 export const SEARCH_PROJECT_ID = 'Search all Projects';
479 export const initSearchProject = (pickerId: string) =>
480 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
481 dispatch(receiveTreePickerData({
484 data: [{ uuid: SEARCH_PROJECT_ID, name: SEARCH_PROJECT_ID }],
485 extractNodeData: value => ({
487 status: TreeNodeStatus.INITIAL,
494 interface LoadFavoritesProjectParams {
496 includeCollections?: boolean;
497 includeDirectories?: boolean;
498 includeFiles?: boolean;
499 options?: { showOnlyOwned: boolean, showOnlyWritable: boolean };
502 export const loadFavoritesProject = (params: LoadFavoritesProjectParams,
503 options: { showOnlyOwned: boolean, showOnlyWritable: boolean } = { showOnlyOwned: true, showOnlyWritable: false }) =>
504 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
505 const { pickerId, includeCollections = false, includeDirectories = false, includeFiles = false } = params;
506 const uuid = getUserUuid(getState());
508 const filters = pipe(
509 (fb: FilterBuilder) => includeCollections
510 ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
511 : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
512 fb => fb.getFilters(),
513 )(new FilterBuilder());
515 const { items } = await services.favoriteService.list(uuid, { filters }, options.showOnlyOwned);
517 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
520 data: items.filter((item) => {
521 if (options.showOnlyWritable && !(item as GroupResource).canWrite) {
525 if (options.showOnlyWritable && item.hasOwnProperty('frozenByUuid') && (item as ProjectResource).frozenByUuid) {
531 extractNodeData: item => ({
534 status: item.kind === ResourceKind.PROJECT
535 ? TreeNodeStatus.INITIAL
536 : includeDirectories || includeFiles
537 ? TreeNodeStatus.INITIAL
538 : TreeNodeStatus.LOADED
544 export const loadPublicFavoritesProject = (params: LoadFavoritesProjectParams) =>
545 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
546 const { pickerId, includeCollections = false, includeDirectories = false, includeFiles = false } = params;
547 const uuidPrefix = getState().auth.config.uuidPrefix;
548 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
550 const filters = pipe(
551 (fb: FilterBuilder) => includeCollections
552 ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
553 : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
555 .addEqual('link_class', LinkClass.STAR)
556 .addEqual('owner_uuid', publicProjectUuid)
558 )(new FilterBuilder());
560 const { items } = await services.linkService.list({ filters });
562 dispatch<any>(receiveTreePickerData<LinkResource>({
563 id: 'Public Favorites',
565 data: items.filter(item => {
566 if (params.options && params.options.showOnlyWritable && item.hasOwnProperty('frozenByUuid') && (item as any).frozenByUuid) {
572 extractNodeData: item => ({
575 status: item.headKind === ResourceKind.PROJECT
576 ? TreeNodeStatus.INITIAL
577 : includeDirectories || includeFiles
578 ? TreeNodeStatus.INITIAL
579 : TreeNodeStatus.LOADED
584 export const receiveTreePickerProjectsData = (id: string, projects: ProjectResource[], pickerId: string) =>
585 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
586 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
588 nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
592 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
595 export const loadProjectTreePickerProjects = (id: string) =>
596 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
597 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
600 const ownerUuid = id.length === 0 ? getUserUuid(getState()) || '' : id;
601 const { items } = await services.projectService.list(buildParams(ownerUuid));
603 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PROJECTS));
606 export const loadFavoriteTreePickerProjects = (id: string) =>
607 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
608 const parentId = getUserUuid(getState()) || '';
611 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
612 const { items } = await services.favoriteService.list(parentId);
613 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
615 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
616 const { items } = await services.projectService.list(buildParams(id));
617 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.FAVORITES));
622 export const loadPublicFavoriteTreePickerProjects = (id: string) =>
623 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
624 const parentId = getUserUuid(getState()) || '';
627 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.PUBLIC_FAVORITES }));
628 const { items } = await services.favoriteService.list(parentId);
629 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.PUBLIC_FAVORITES));
631 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PUBLIC_FAVORITES }));
632 const { items } = await services.projectService.list(buildParams(id));
633 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PUBLIC_FAVORITES));
638 const buildParams = (ownerUuid: string) => {
640 filters: new FilterBuilder()
641 .addEqual('owner_uuid', ownerUuid)
643 order: new OrderBuilder<ProjectResource>()
650 * Given a tree picker item, return collection uuid and path
651 * if the item represents a valid target/destination location
653 export type FileOperationLocation = {
659 export const getFileOperationLocation = (item: ProjectsTreePickerItem) =>
660 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<FileOperationLocation | undefined> => {
661 if ('kind' in item && item.kind === ResourceKind.COLLECTION) {
665 pdh: item.portableDataHash,
668 } else if ('type' in item && item.type === CollectionFileType.DIRECTORY) {
669 const uuid = getCollectionResourceCollectionUuid(item.id);
671 const collection = getResource<CollectionResource>(uuid)(getState().resources);
673 const itemPath = [item.path, item.name].join('/');
678 pdh: collection.portableDataHash,
688 * Create an expanded tree picker subtree from array of nested projects/collection
689 * First item is assumed to be root and gets empty parent id
690 * Nodes must be sorted from top down to prevent orphaned nodes
692 export const createInitialPickerTree = (sortedAncestors: Array<GroupResource | CollectionResource>, tailUuid: string, initialTree: Tree<GroupResource | CollectionResource>) => {
693 return sortedAncestors
694 .reduce((tree, item, index) => {
695 if (getNode(item.uuid)(tree)) {
701 parent: index === 0 ? '' : item.ownerUuid,
706 status: item.uuid !== tailUuid ? TreeNodeStatus.LOADED : TreeNodeStatus.INITIAL,
712 export const fileOperationLocationToPickerId = (location: FileOperationLocation): string => {
713 let id = location.uuid;
714 if (location.subpath.length && location.subpath !== '/') {
715 id = id + location.subpath;