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, getNodeDescendantsIds, getNodeValue, TreeNodeStatus, getNode } 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 } 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 } from './tree-picker';
16 import { ProjectsTreePickerItem } from '~/views-components/projects-tree-picker/generic-projects-tree-picker';
18 export const treePickerActions = unionize({
19 LOAD_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
20 LOAD_TREE_PICKER_NODE_SUCCESS: ofType<{ id: string, nodes: Array<TreeNode<any>>, pickerId: string }>(),
21 TOGGLE_TREE_PICKER_NODE_COLLAPSE: ofType<{ id: string, pickerId: string }>(),
22 ACTIVATE_TREE_PICKER_NODE: ofType<{ id: string, pickerId: string }>(),
23 DEACTIVATE_TREE_PICKER_NODE: ofType<{ pickerId: string }>(),
24 TOGGLE_TREE_PICKER_NODE_SELECTION: ofType<{ id: string, pickerId: string }>(),
25 EXPAND_TREE_PICKER_NODES: ofType<{ ids: string[], pickerId: string }>(),
26 RESET_TREE_PICKER: ofType<{ pickerId: string }>()
29 export type TreePickerAction = UnionOf<typeof treePickerActions>;
31 export const getProjectsTreePickerIds = (pickerId: string) => ({
32 home: `${pickerId}_home`,
33 shared: `${pickerId}_shared`,
34 favorites: `${pickerId}_favorites`,
36 export const initProjectsTreePicker = (pickerId: string) =>
37 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
38 const { home, shared, favorites } = getProjectsTreePickerIds(pickerId);
39 dispatch<any>(initUserProject(home));
40 dispatch<any>(initSharedProject(shared));
41 dispatch<any>(initFavoritesProject(favorites));
44 interface ReceiveTreePickerDataParams<T> {
46 extractNodeData: (value: T) => { id: string, value: T, status?: TreeNodeStatus };
50 export const receiveTreePickerData = <T>(params: ReceiveTreePickerDataParams<T>) =>
51 (dispatch: Dispatch) => {
52 const { data, extractNodeData, id, pickerId, } = params;
53 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
55 nodes: data.map(item => initTreeNode(extractNodeData(item))),
58 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
61 interface LoadProjectParams {
64 includeCollections?: boolean;
65 includeFiles?: boolean;
68 export const loadProject = (params: LoadProjectParams) =>
69 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
70 const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
72 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
75 (fb: FilterBuilder) => includeCollections
76 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
77 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
78 fb => fb.getFilters(),
79 )(new FilterBuilder());
81 const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
83 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
87 extractNodeData: item => ({
90 status: item.kind === ResourceKind.PROJECT
91 ? TreeNodeStatus.INITIAL
93 ? TreeNodeStatus.INITIAL
94 : TreeNodeStatus.LOADED
99 export const loadCollection = (id: string, pickerId: string) =>
100 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
101 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
103 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
106 const node = getNode(id)(picker);
107 if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
109 const files = await services.collectionService.files(node.value.portableDataHash);
110 const data = getNodeDescendants('')(files).map(node => node.value);
112 dispatch<any>(receiveTreePickerData<CollectionDirectory | CollectionFile>({
116 extractNodeData: value => ({
118 status: TreeNodeStatus.LOADED,
127 export const initUserProject = (pickerId: string) =>
128 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
129 const uuid = services.authService.getUuid();
131 dispatch(receiveTreePickerData({
134 data: [{ uuid, name: 'Projects' }],
135 extractNodeData: value => ({
137 status: TreeNodeStatus.INITIAL,
143 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
144 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
145 const uuid = services.authService.getUuid();
147 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
152 export const initSharedProject = (pickerId: string) =>
153 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
154 dispatch(receiveTreePickerData({
157 data: [{ uuid: 'Shared with me', name: 'Shared with me' }],
158 extractNodeData: value => ({
160 status: TreeNodeStatus.INITIAL,
166 export const initFavoritesProject = (pickerId: string) =>
167 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
168 dispatch(receiveTreePickerData({
171 data: [{ uuid: 'Favorites', name: 'Favorites' }],
172 extractNodeData: value => ({
174 status: TreeNodeStatus.INITIAL,
180 interface LoadFavoritesProjectParams {
182 includeCollections?: boolean;
183 includeFiles?: boolean;
185 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
186 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
187 const { pickerId, includeCollections = false, includeFiles = false } = params;
188 const uuid = services.authService.getUuid();
191 const filters = pipe(
192 (fb: FilterBuilder) => includeCollections
193 ? fb.addIsA('headUuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
194 : fb.addIsA('headUuid', [ResourceKind.PROJECT]),
195 fb => fb.getFilters(),
196 )(new FilterBuilder());
198 const { items } = await services.favoriteService.list(uuid, { filters });
200 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
204 extractNodeData: item => ({
207 status: item.kind === ResourceKind.PROJECT
208 ? TreeNodeStatus.INITIAL
210 ? TreeNodeStatus.INITIAL
211 : TreeNodeStatus.LOADED