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 } from 'models/tree';
7 import { createCollectionFilesTree } 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 'views-components/projects-tree-picker/generic-projects-tree-picker';
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";
26 export const treePickerActions = unionize({
27 LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
28 LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
29 APPEND_TREE_PICKER_NODE_SUBTREE: ofType<{ id: string, subtree: Tree<any>, pickerId: string }>(),
30 TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
31 ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string, relatedTreePickers?: string[] }>(),
32 DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
33 TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
34 SELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
35 DESELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
36 EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
37 RESET_TREE_PICKER: ofType<{ pickerId: string }>()
40 export type TreePickerAction = UnionOf<typeof treePickerActions>;
42 export const getProjectsTreePickerIds = (pickerId: string) => ({
43 home: `${pickerId}_home`,
44 shared: `${pickerId}_shared`,
45 favorites: `${pickerId}_favorites`,
46 publicFavorites: `${pickerId}_publicFavorites`
49 export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
51 () => values(getProjectsTreePickerIds(pickerId)),
54 .map(id => getTreePicker<Value>(id)(state)),
57 .map(getNodeDescendants(''))
58 .reduce((allNodes, nodes) => allNodes.concat(nodes), []),
61 .reduce((map, node) =>
63 ? map.set(node.id, node)
64 : map, new Map<string, TreeNode<Value>>())
67 uniqueNodes => Array.from(uniqueNodes),
69 export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) =>
70 getAllNodes<Value>(pickerId, node => node.selected)(state);
72 export const initProjectsTreePicker = (pickerId: string) =>
73 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
74 const { home, shared, favorites, publicFavorites } = getProjectsTreePickerIds(pickerId);
75 dispatch<any>(initUserProject(home));
76 dispatch<any>(initSharedProject(shared));
77 dispatch<any>(initFavoritesProject(favorites));
78 dispatch<any>(initPublicFavoritesProject(publicFavorites));
81 interface ReceiveTreePickerDataParams<T> {
83 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
88 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
89 (dispatch: Dispatch) => {
90 const { data, extractNodeData, id, pickerId, } = params;
91 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
93 nodes: data.map(item => initTreeNode(extractNodeData(item))),
96 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
99 interface LoadProjectParams {
102 includeCollections?: boolean;
103 includeFiles?: boolean;
104 includeFilterGroups?: boolean;
105 loadShared?: boolean;
107 export const loadProject = (params: LoadProjectParams) =>
108 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
109 const { id, pickerId, includeCollections = false, includeFiles = false, includeFilterGroups = false, loadShared = false } = params;
111 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
113 const filters = pipe(
114 (fb: FilterBuilder) => includeCollections
115 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
116 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
117 fb => fb.getFilters(),
118 )(new FilterBuilder());
120 const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
122 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
125 data: items.filter((item) => {
126 if (!includeFilterGroups && (item as GroupResource).groupClass && (item as GroupResource).groupClass === GroupClass.FILTER) {
131 extractNodeData: item => ({
134 status: item.kind === ResourceKind.PROJECT
135 ? TreeNodeStatus.INITIAL
137 ? TreeNodeStatus.INITIAL
138 : TreeNodeStatus.LOADED
143 export const loadCollection = (id: string, pickerId: string) =>
144 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
145 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
147 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
150 const node = getNode(id)(picker);
151 if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
152 const files = await services.collectionService.files(node.value.portableDataHash);
153 const tree = createCollectionFilesTree(files);
154 const sorted = sortFilesTree(tree);
155 const filesTree = mapTreeValues(services.collectionService.extendFileURL)(sorted);
158 treePickerActions.APPEND_TREE_PICKER_NODE_SUBTREE({
161 subtree: mapTree(node => ({ ...node, status: TreeNodeStatus.LOADED }))(filesTree)
164 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
170 export const initUserProject = (pickerId: string) =>
171 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
172 const uuid = getUserUuid(getState());
174 dispatch(receiveTreePickerData({
177 data: [{ uuid, name: 'Projects' }],
178 extractNodeData: value => ({
180 status: TreeNodeStatus.INITIAL,
186 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
187 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
188 const uuid = getUserUuid(getState());
190 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
194 export const SHARED_PROJECT_ID = 'Shared with me';
195 export const initSharedProject = (pickerId: string) =>
196 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
197 dispatch(receiveTreePickerData({
200 data: [{ uuid: SHARED_PROJECT_ID, name: SHARED_PROJECT_ID }],
201 extractNodeData: value => ({
203 status: TreeNodeStatus.INITIAL,
209 export const FAVORITES_PROJECT_ID = 'Favorites';
210 export const initFavoritesProject = (pickerId: string) =>
211 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
212 dispatch(receiveTreePickerData({
215 data: [{ uuid: FAVORITES_PROJECT_ID, name: FAVORITES_PROJECT_ID }],
216 extractNodeData: value => ({
218 status: TreeNodeStatus.INITIAL,
224 export const PUBLIC_FAVORITES_PROJECT_ID = 'Public Favorites';
225 export const initPublicFavoritesProject = (pickerId: string) =>
226 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
227 dispatch(receiveTreePickerData({
230 data: [{ uuid: PUBLIC_FAVORITES_PROJECT_ID, name: PUBLIC_FAVORITES_PROJECT_ID }],
231 extractNodeData: value => ({
233 status: TreeNodeStatus.INITIAL,
239 interface LoadFavoritesProjectParams {
241 includeCollections?: boolean;
242 includeFiles?: boolean;
245 export const loadFavoritesProject = (params: LoadFavoritesProjectParams,
246 options: { showOnlyOwned: boolean, showOnlyWritable: boolean } = { showOnlyOwned: true, showOnlyWritable: false }) =>
247 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
248 const { pickerId, includeCollections = false, includeFiles = false } = params;
249 const uuid = getUserUuid(getState());
251 const filters = pipe(
252 (fb: FilterBuilder) => includeCollections
253 ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
254 : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
255 fb => fb.getFilters(),
256 )(new FilterBuilder());
258 const { items } = await services.favoriteService.list(uuid, { filters }, options.showOnlyOwned);
260 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
263 data: items.filter((item) => {
264 if (options.showOnlyWritable && (item as GroupResource).writableBy && (item as GroupResource).writableBy.indexOf(uuid) === -1) {
270 extractNodeData: item => ({
273 status: item.kind === ResourceKind.PROJECT
274 ? TreeNodeStatus.INITIAL
276 ? TreeNodeStatus.INITIAL
277 : TreeNodeStatus.LOADED
283 export const loadPublicFavoritesProject = (params: LoadFavoritesProjectParams) =>
284 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
285 const { pickerId, includeCollections = false, includeFiles = false } = params;
286 const uuidPrefix = getState().auth.config.uuidPrefix;
287 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
289 const filters = pipe(
290 (fb: FilterBuilder) => includeCollections
291 ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
292 : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
294 .addEqual('link_class', LinkClass.STAR)
295 .addEqual('owner_uuid', publicProjectUuid)
297 )(new FilterBuilder());
299 const { items } = await services.linkService.list({ filters });
301 dispatch<any>(receiveTreePickerData<LinkResource>({
302 id: 'Public Favorites',
305 extractNodeData: item => ({
308 status: item.headKind === ResourceKind.PROJECT
309 ? TreeNodeStatus.INITIAL
311 ? TreeNodeStatus.INITIAL
312 : TreeNodeStatus.LOADED
317 export const receiveTreePickerProjectsData = (id: string, projects: ProjectResource[], pickerId: string) =>
318 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
319 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
321 nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
325 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
328 export const loadProjectTreePickerProjects = (id: string) =>
329 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
330 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
333 const ownerUuid = id.length === 0 ? getUserUuid(getState()) || '' : id;
334 const { items } = await services.projectService.list(buildParams(ownerUuid));
336 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PROJECTS));
339 export const loadFavoriteTreePickerProjects = (id: string) =>
340 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
341 const parentId = getUserUuid(getState()) || '';
344 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
345 const { items } = await services.favoriteService.list(parentId);
346 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
348 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
349 const { items } = await services.projectService.list(buildParams(id));
350 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.FAVORITES));
355 export const loadPublicFavoriteTreePickerProjects = (id: string) =>
356 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
357 const parentId = getUserUuid(getState()) || '';
360 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.PUBLIC_FAVORITES }));
361 const { items } = await services.favoriteService.list(parentId);
362 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.PUBLIC_FAVORITES));
364 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PUBLIC_FAVORITES }));
365 const { items } = await services.projectService.list(buildParams(id));
366 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PUBLIC_FAVORITES));
371 const buildParams = (ownerUuid: string) => {
373 filters: new FilterBuilder()
374 .addEqual('owner_uuid', ownerUuid)
376 order: new OrderBuilder<ProjectResource>()