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 { 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 { getTreePicker, TreePicker } from './tree-picker';
15 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
16 import { OrderBuilder } from '~/services/api/order-builder';
17 import { ProjectResource } from '~/models/project';
18 import { mapTree } from '../../models/tree';
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 APPEND_TREE_PICKER_NODE_SUBTREE: ofType<{ id: string, subtree: Tree<any>, pickerId: string }>(),
24 TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
25 ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string, relatedTreePickers?: string[] }>(),
26 DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
27 TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
28 SELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
29 DESELECT_TREE_PICKER_NODE: ofType<{ id: string | string[], pickerId: string }>(),
30 EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
31 RESET_TREE_PICKER: ofType<{ pickerId: string }>()
34 export type TreePickerAction = UnionOf<typeof treePickerActions>;
36 export const getProjectsTreePickerIds = (pickerId: string) => ({
37 home: `${pickerId}_home`,
38 shared: `${pickerId}_shared`,
39 favorites: `${pickerId}_favorites`,
42 export const getAllNodes = <Value>(pickerId: string, filter = (node: TreeNode<Value>) => true) => (state: TreePicker) =>
44 () => values(getProjectsTreePickerIds(pickerId)),
47 .map(id => getTreePicker<Value>(id)(state)),
50 .map(getNodeDescendants(''))
51 .reduce((allNodes, nodes) => allNodes.concat(nodes), []),
54 .reduce((map, node) =>
56 ? map.set(node.id, node)
57 : map, new Map<string, TreeNode<Value>>())
60 uniqueNodes => Array.from(uniqueNodes),
62 export const getSelectedNodes = <Value>(pickerId: string) => (state: TreePicker) =>
63 getAllNodes<Value>(pickerId, node => node.selected)(state);
65 export const initProjectsTreePicker = (pickerId: string) =>
66 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
67 const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
68 dispatch<any>(initUserProject(home));
69 dispatch<any>(initSharedProject(shared));
70 dispatch<any>(initFavoritesProject(favorites));
73 interface ReceiveTreePickerDataParams<T> {
75 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
80 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
81 (dispatch: Dispatch) => {
82 const { data, extractNodeData, id, pickerId, } = params;
83 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
85 nodes: data.map(item => initTreeNode(extractNodeData(item))),
88 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
91 interface LoadProjectParams {
94 includeCollections?: boolean;
95 includeFiles?: boolean;
98 export const loadProject = (params: LoadProjectParams) =>
99 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
100 const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
102 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
104 const filters = pipe(
105 (fb: FilterBuilder) => includeCollections
106 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
107 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
108 fb => fb.getFilters(),
109 )(new FilterBuilder());
111 const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
113 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
117 extractNodeData: item => ({
120 status: item.kind === ResourceKind.PROJECT
121 ? TreeNodeStatus.INITIAL
123 ? TreeNodeStatus.INITIAL
124 : TreeNodeStatus.LOADED
129 export const loadCollection = (id: string, pickerId: string) =>
130 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
131 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
133 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
136 const node = getNode(id)(picker);
137 if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
139 const filesTree = await services.collectionService.files(node.value.portableDataHash);
142 treePickerActions.APPEND_TREE_PICKER_NODE_SUBTREE({
145 subtree: mapTree(node => ({ ...node, status: TreeNodeStatus.LOADED }))(filesTree)
148 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
154 export const initUserProject = (pickerId: string) =>
155 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
156 const uuid = services.authService.getUuid();
158 dispatch(receiveTreePickerData({
161 data: [{ uuid, name: 'Projects' }],
162 extractNodeData: value => ({
164 status: TreeNodeStatus.INITIAL,
170 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
171 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
172 const uuid = services.authService.getUuid();
174 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
178 export const SHARED_PROJECT_ID = 'Shared with me';
179 export const initSharedProject = (pickerId: string) =>
180 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
181 dispatch(receiveTreePickerData({
184 data: [{ uuid: SHARED_PROJECT_ID, name: SHARED_PROJECT_ID }],
185 extractNodeData: value => ({
187 status: TreeNodeStatus.INITIAL,
193 export const FAVORITES_PROJECT_ID = 'Favorites';
194 export const initFavoritesProject = (pickerId: string) =>
195 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
196 dispatch(receiveTreePickerData({
199 data: [{ uuid: FAVORITES_PROJECT_ID, name: FAVORITES_PROJECT_ID }],
200 extractNodeData: value => ({
202 status: TreeNodeStatus.INITIAL,
208 interface LoadFavoritesProjectParams {
210 includeCollections?: boolean;
211 includeFiles?: boolean;
214 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
215 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
216 const { pickerId, includeCollections = false, includeFiles = false } = params;
217 const uuid = services.authService.getUuid();
220 const filters = pipe(
221 (fb: FilterBuilder) => includeCollections
222 ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
223 : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
224 fb => fb.getFilters(),
225 )(new FilterBuilder());
227 const { items } = await services.favoriteService.list(uuid, { filters });
229 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
233 extractNodeData: item => ({
236 status: item.kind === ResourceKind.PROJECT
237 ? TreeNodeStatus.INITIAL
239 ? TreeNodeStatus.INITIAL
240 : TreeNodeStatus.LOADED
246 export const receiveTreePickerProjectsData = (id: string, projects: ProjectResource[], pickerId: string) =>
247 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
248 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
250 nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
254 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
257 export const loadProjectTreePickerProjects = (id: string) =>
258 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
259 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
261 const ownerUuid = id.length === 0 ? services.authService.getUuid() || '' : id;
262 const { items } = await services.projectService.list(buildParams(ownerUuid));
264 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PROJECTS));
267 export const loadFavoriteTreePickerProjects = (id: string) =>
268 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
269 const parentId = services.authService.getUuid() || '';
272 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
273 const { items } = await services.favoriteService.list(parentId);
274 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
276 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
277 const { items } = await services.projectService.list(buildParams(id));
278 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.FAVORITES));
283 const buildParams = (ownerUuid: string) => {
285 filters: new FilterBuilder()
286 .addEqual('ownerUuid', ownerUuid)
288 order: new OrderBuilder<ProjectResource>()