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 } from '~/models/tree';
7 import { Dispatch } from 'redux';
8 import { RootState } from '~/store/store';
9 import { ServiceRepository } from '~/services/services';
10 import { FilterBuilder } from '~/services/api/filter-builder';
11 import { pipe, values } from 'lodash/fp';
12 import { ResourceKind } from '~/models/resource';
13 import { GroupContentsResource } from '~/services/groups-service/groups-service';
14 import { CollectionDirectory, CollectionFile } from '~/models/collection-file';
15 import { getTreePicker, TreePicker } from './tree-picker';
16 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
17 import { OrderBuilder } from '~/services/api/order-builder';
18 import { ProjectResource } from '~/models/project';
20 export const treePickerActions = unionize({
21 LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
22 LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
23 TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
24 ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
25 DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
26 TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
27 SELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
28 DESELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
29 EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
30 RESET_TREE_PICKER: ofType<{ pickerId: string }>()
33 export type TreePickerAction = UnionOf<typeof treePickerActions>;
35 export const getProjectsTreePickerIds = (pickerId: string) => ({
36 home: `${pickerId}_home`,
37 shared: `${pickerId}_shared`,
38 favorites: `${pickerId}_favorites`,
41 export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
43 () => values(getProjectsTreePickerIds(pickerId)),
46 .map(id => getTreePicker<Value>(id)(state)),
49 .map(getNodeDescendants(''))
50 .reduce((allNodes, nodes) => allNodes.concat(nodes), []),
53 .reduce((map, node) =>
55 ? map.set(node.id, node)
56 : map, new Map<string, TreeNode<Value>>())
59 uniqueNodes => Array.from(uniqueNodes),
61 export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) =>
62 getAllNodes<Value>(pickerId, node => node.selected)(state);
64 export const initProjectsTreePicker = (pickerId: string) =>
65 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
66 const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
67 dispatch<any>(initUserProject(home));
68 dispatch<any>(initSharedProject(shared));
69 dispatch<any>(initFavoritesProject(favorites));
72 interface ReceiveTreePickerDataParams<T> {
74 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
79 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
80 (dispatch: Dispatch) => {
81 const { data, extractNodeData, id, pickerId, } = params;
82 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
84 nodes: data.map(item => initTreeNode(extractNodeData(item))),
87 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
90 interface LoadProjectParams {
93 includeCollections?: boolean;
94 includeFiles?: boolean;
97 export const loadProject = (params: LoadProjectParams) =>
98 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
99 const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
101 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
103 const filters = pipe(
104 (fb: FilterBuilder) => includeCollections
105 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
106 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
107 fb => fb.getFilters(),
108 )(new FilterBuilder());
110 const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
112 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
116 extractNodeData: item => ({
119 status: item.kind === ResourceKind.PROJECT
120 ? TreeNodeStatus.INITIAL
122 ? TreeNodeStatus.INITIAL
123 : TreeNodeStatus.LOADED
128 export const loadCollection = (id: string, pickerId: string) =>
129 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
130 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
132 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
135 const node = getNode(id)(picker);
136 if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
138 const files = await services.collectionService.files(node.value.portableDataHash);
139 const data = getNodeDescendants('')(files).map(node => node.value);
141 dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
145 extractNodeData: value => ({
147 status: TreeNodeStatus.LOADED,
156 export const initUserProject = (pickerId: string) =>
157 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
158 const uuid = services.authService.getUuid();
160 dispatch(receiveTreePickerData({
163 data: [{ uuid, name: 'Projects' }],
164 extractNodeData: value => ({
166 status: TreeNodeStatus.INITIAL,
172 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
173 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
174 const uuid = services.authService.getUuid();
176 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
181 export const initSharedProject = (pickerId: string) =>
182 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
183 dispatch(receiveTreePickerData({
186 data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
187 extractNodeData: value => ({
189 status: TreeNodeStatus.INITIAL,
195 export const initFavoritesProject = (pickerId: string) =>
196 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
197 dispatch(receiveTreePickerData({
200 data: [{ uuid: 'Favorites', name: 'Favorites' }],
201 extractNodeData: value => ({
203 status: TreeNodeStatus.INITIAL,
209 interface LoadFavoritesProjectParams {
211 includeCollections?: boolean;
212 includeFiles?: boolean;
215 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
216 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
217 const { pickerId, includeCollections = false, includeFiles = false } = params;
218 const uuid = services.authService.getUuid();
221 const filters = pipe(
222 (fb: FilterBuilder) => includeCollections
223 ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
224 : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
225 fb => fb.getFilters(),
226 )(new FilterBuilder());
228 const { items } = await services.favoriteService.list(uuid, { filters });
230 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
234 extractNodeData: item => ({
237 status: item.kind === ResourceKind.PROJECT
238 ? TreeNodeStatus.INITIAL
240 ? TreeNodeStatus.INITIAL
241 : TreeNodeStatus.LOADED
247 export const receiveTreePickerProjectsData = (id: string, projects: ProjectResource[], pickerId: string) =>
248 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
249 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
251 nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
255 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
258 export const loadProjectTreePickerProjects = (id: string) =>
259 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
260 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
262 const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
263 const { items } = await services.projectService.list(buildParams(ownerUuid));
265 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PROJECTS));
268 export const loadFavoriteTreePickerProjects = (id: string) =>
269 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
270 const parentId = services.authService.getUuid() || '';
273 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
274 const { items } = await services.favoriteService.list(parentId);
275 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
277 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
278 const { items } = await services.projectService.list(buildParams(id));
279 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.FAVORITES));
284 const buildParams = (ownerUuid: string) => {
286 filters: new FilterBuilder()
287 .addEqual('ownerUuid', ownerUuid)
289 order: new OrderBuilder<ProjectResource>()