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 { 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 loadShared?: boolean;
106 export const loadProject = (params: LoadProjectParams) =>
107 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
108 const { id, pickerId, includeCollections = false, includeFiles = false, loadShared = false } = params;
110 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
112 const filters = pipe(
113 (fb: FilterBuilder) => includeCollections
114 ? fb.addIsA('uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
115 : fb.addIsA('uuid', [ResourceKind.PROJECT]),
116 fb => fb.getFilters(),
117 )(new FilterBuilder());
119 const { items } = await services.groupsService.contents(loadShared ? '' : id, { filters, excludeHomeProject: loadShared || undefined });
121 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
125 extractNodeData: item => ({
128 status: item.kind === ResourceKind.PROJECT
129 ? TreeNodeStatus.INITIAL
131 ? TreeNodeStatus.INITIAL
132 : TreeNodeStatus.LOADED
137 export const loadCollection = (id: string, pickerId: string) =>
138 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
139 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId }));
141 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
144 const node = getNode(id)(picker);
145 if (node && 'kind' in node.value && node.value.kind === ResourceKind.COLLECTION) {
147 const files = await services.collectionService.files(node.value.portableDataHash);
148 const tree = createCollectionFilesTree(files);
149 const sorted = sortFilesTree(tree);
150 const filesTree = mapTreeValues(services.collectionService.extendFileURL)(sorted);
153 treePickerActions.APPEND_TREE_PICKER_NODE_SUBTREE({
156 subtree: mapTree(node => ({ ...node, status: TreeNodeStatus.LOADED }))(filesTree)
159 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
165 export const initUserProject = (pickerId: string) =>
166 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
167 const uuid = getUserUuid(getState());
169 dispatch(receiveTreePickerData({
172 data: [{ uuid, name: 'Projects' }],
173 extractNodeData: value => ({
175 status: TreeNodeStatus.INITIAL,
181 export const loadUserProject = (pickerId: string, includeCollections = false, includeFiles = false) =>
182 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
183 const uuid = getUserUuid(getState());
185 dispatch(loadProject({ id: uuid, pickerId, includeCollections, includeFiles }));
189 export const SHARED_PROJECT_ID = 'Shared with me';
190 export const initSharedProject = (pickerId: string) =>
191 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
192 dispatch(receiveTreePickerData({
195 data: [{ uuid: SHARED_PROJECT_ID, name: SHARED_PROJECT_ID }],
196 extractNodeData: value => ({
198 status: TreeNodeStatus.INITIAL,
204 export const FAVORITES_PROJECT_ID = 'Favorites';
205 export const initFavoritesProject = (pickerId: string) =>
206 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
207 dispatch(receiveTreePickerData({
210 data: [{ uuid: FAVORITES_PROJECT_ID, name: FAVORITES_PROJECT_ID }],
211 extractNodeData: value => ({
213 status: TreeNodeStatus.INITIAL,
219 export const PUBLIC_FAVORITES_PROJECT_ID = 'Public Favorites';
220 export const initPublicFavoritesProject = (pickerId: string) =>
221 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
222 dispatch(receiveTreePickerData({
225 data: [{ uuid: PUBLIC_FAVORITES_PROJECT_ID, name: PUBLIC_FAVORITES_PROJECT_ID }],
226 extractNodeData: value => ({
228 status: TreeNodeStatus.INITIAL,
234 interface LoadFavoritesProjectParams {
236 includeCollections?: boolean;
237 includeFiles?: boolean;
240 export const loadFavoritesProject = (params: LoadFavoritesProjectParams) =>
241 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
242 const { pickerId, includeCollections = false, includeFiles = false } = params;
243 const uuid = getUserUuid(getState());
245 const filters = pipe(
246 (fb: FilterBuilder) => includeCollections
247 ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
248 : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
249 fb => fb.getFilters(),
250 )(new FilterBuilder());
252 const { items } = await services.favoriteService.list(uuid, { filters }, false);
254 dispatch<any>(receiveTreePickerData<GroupContentsResource>({
257 data: items.filter((item) => {
258 if ((item as GroupResource).writableBy && (item as GroupResource).writableBy.indexOf(uuid) === -1) {
264 extractNodeData: item => ({
267 status: item.kind === ResourceKind.PROJECT
268 ? TreeNodeStatus.INITIAL
270 ? TreeNodeStatus.INITIAL
271 : TreeNodeStatus.LOADED
277 export const loadPublicFavoritesProject = (params: LoadFavoritesProjectParams) =>
278 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
279 const { pickerId, includeCollections = false, includeFiles = false } = params;
280 const uuidPrefix = getState().auth.config.uuidPrefix;
281 const publicProjectUuid = `${uuidPrefix}-j7d0g-publicfavorites`;
283 const filters = pipe(
284 (fb: FilterBuilder) => includeCollections
285 ? fb.addIsA('head_uuid', [ResourceKind.PROJECT, ResourceKind.COLLECTION])
286 : fb.addIsA('head_uuid', [ResourceKind.PROJECT]),
288 .addEqual('link_class', LinkClass.STAR)
289 .addEqual('owner_uuid', publicProjectUuid)
291 )(new FilterBuilder());
293 const { items } = await services.linkService.list({ filters });
295 dispatch<any>(receiveTreePickerData<LinkResource>({
296 id: 'Public Favorites',
299 extractNodeData: item => ({
302 status: item.headKind === ResourceKind.PROJECT
303 ? TreeNodeStatus.INITIAL
305 ? TreeNodeStatus.INITIAL
306 : TreeNodeStatus.LOADED
311 export const receiveTreePickerProjectsData = (id: string, projects: ProjectResource[], pickerId: string) =>
312 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
313 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
315 nodes: projects.map(project => initTreeNode({ id: project.uuid, value: project })),
319 dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId }));
322 export const loadProjectTreePickerProjects = (id: string) =>
323 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
324 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PROJECTS }));
327 const ownerUuid = id.length === 0 ? getUserUuid(getState()) || '' : id;
328 const { items } = await services.projectService.list(buildParams(ownerUuid));
330 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PROJECTS));
333 export const loadFavoriteTreePickerProjects = (id: string) =>
334 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
335 const parentId = getUserUuid(getState()) || '';
338 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.FAVORITES }));
339 const { items } = await services.favoriteService.list(parentId);
340 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.FAVORITES));
342 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.FAVORITES }));
343 const { items } = await services.projectService.list(buildParams(id));
344 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.FAVORITES));
349 export const loadPublicFavoriteTreePickerProjects = (id: string) =>
350 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
351 const parentId = getUserUuid(getState()) || '';
354 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: parentId, pickerId: TreePickerId.PUBLIC_FAVORITES }));
355 const { items } = await services.favoriteService.list(parentId);
356 dispatch<any>(receiveTreePickerProjectsData(parentId, items as ProjectResource[], TreePickerId.PUBLIC_FAVORITES));
358 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id, pickerId: TreePickerId.PUBLIC_FAVORITES }));
359 const { items } = await services.projectService.list(buildParams(id));
360 dispatch<any>(receiveTreePickerProjectsData(id, items, TreePickerId.PUBLIC_FAVORITES));
365 const buildParams = (ownerUuid: string) => {
367 filters: new FilterBuilder()
368 .addEqual('owner_uuid', ownerUuid)
370 order: new OrderBuilder<ProjectResource>()