1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
6 DataExplorerMiddlewareService,
7 listResultsToDataExplorerItemsMeta,
8 dataExplorerToListParams,
9 getDataExplorerColumnFilters,
10 } from "../data-explorer/data-explorer-middleware-service";
11 import { ServiceRepository } from "services/services";
12 import { MiddlewareAPI, Dispatch } from "redux";
13 import { RootState } from 'store/store';
14 import { getDataExplorer, DataExplorer } from 'store/data-explorer/data-explorer-reducer';
15 import { updateFavorites } from 'store/favorites/favorites-actions';
16 import { updateResources } from 'store/resources/resources-actions';
17 import { loadMissingProcessesInformation } from 'store/project-panel/project-panel-run-middleware-service';
18 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
19 import { sharedWithMePanelActions } from './shared-with-me-panel-actions';
20 import { ListResults } from 'services/common-service/common-service';
21 import { ContentsArguments, GroupContentsResource, GroupContentsResourcePrefix } from 'services/groups-service/groups-service';
22 import { SortDirection } from 'components/data-table/data-column';
23 import { OrderBuilder, OrderDirection } from 'services/api/order-builder';
24 import { ProjectResource } from 'models/project';
25 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
26 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
27 import { FilterBuilder, joinFilters } from 'services/api/filter-builder';
28 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
29 import { AuthState } from 'store/auth/auth-reducer';
30 import { SharedWithMePanelColumnNames } from 'views/shared-with-me-panel/shared-with-me-panel';
31 import { buildProcessStatusFilters, serializeResourceTypeFilters } from 'store/resource-type-filters/resource-type-filters';
32 import { DataColumns } from 'components/data-table/data-table';
33 import { couldNotFetchItemsAvailable } from 'store/data-explorer/data-explorer-action';
35 export class SharedWithMeMiddlewareService extends DataExplorerMiddlewareService {
36 constructor(private services: ServiceRepository, id: string) {
40 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
41 const state = api.getState();
42 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
44 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
45 const response = await this.services.groupsService
46 .contents('', getParams(dataExplorer, state.auth));
47 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
48 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
49 api.dispatch(updateResources(response.items));
50 api.dispatch(updateResources(response.included));
51 api.dispatch(setItems(response));
53 api.dispatch(couldNotFetchSharedItems());
55 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
59 async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean) {
60 const state = api.getState();
61 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
63 if (criteriaChanged) {
65 return this.services.groupsService.contents('', getCountParams(dataExplorer, state.auth))
66 .then((results: ListResults<GroupContentsResource>) => {
67 if (results.itemsAvailable !== undefined) {
68 api.dispatch<any>(sharedWithMePanelActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
70 couldNotFetchItemsAvailable();
77 export const getParams = (dataExplorer: DataExplorer, authState: AuthState): ContentsArguments => ({
78 ...dataExplorerToListParams(dataExplorer),
79 order: getOrder(dataExplorer),
80 filters: getFilters(dataExplorer, authState),
81 excludeHomeProject: true,
83 include: ["owner_uuid", "container_uuid"]
86 const getCountParams = (dataExplorer: DataExplorer, authState: AuthState): ContentsArguments => ({
89 filters: getFilters(dataExplorer, authState),
90 excludeHomeProject: true,
93 const getOrder = (dataExplorer: DataExplorer) => {
94 const sortColumn = getSortColumn<ProjectResource>(dataExplorer);
95 const order = new OrderBuilder<ProjectResource>();
96 if (sortColumn && sortColumn.sort) {
97 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
99 : OrderDirection.DESC;
101 // Use createdAt as a secondary sort column so we break ties consistently.
103 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.COLLECTION)
104 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROCESS)
105 .addOrder(sortDirection, sortColumn.sort.field, GroupContentsResourcePrefix.PROJECT)
106 .addOrder(OrderDirection.DESC, "createdAt", GroupContentsResourcePrefix.PROCESS)
109 return order.getOrder();
113 const getFilters = (dataExplorer: DataExplorer, authState: AuthState) => {
114 const columns = dataExplorer.columns as DataColumns<string, ProjectResource>;
115 const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, SharedWithMePanelColumnNames.TYPE));
116 const statusColumnFilters = getDataExplorerColumnFilters(columns, "Status");
117 const activeStatusFilter = Object.keys(statusColumnFilters).find(filterName => statusColumnFilters[filterName].selected);
119 // TODO: Extract group contents name filter
120 const nameFilters = new FilterBuilder()
121 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
122 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
123 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
126 // Filter by container status
127 const statusFilters = buildProcessStatusFilters(new FilterBuilder(), activeStatusFilter || "", GroupContentsResourcePrefix.PROCESS).getFilters();
129 // Filter public favorites
130 const favoritesFilter = new FilterBuilder().addDistinct('uuid', `${authState.config.uuidPrefix}-j7d0g-publicfavorites`).getFilters()
132 return joinFilters(statusFilters, typeFilters, nameFilters, favoritesFilter);
136 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
137 sharedWithMePanelActions.SET_ITEMS({
138 ...listResultsToDataExplorerItemsMeta(listResults),
139 items: listResults.items.map(resource => resource.uuid),
142 const couldNotFetchSharedItems = () =>
143 snackbarActions.OPEN_SNACKBAR({
144 message: 'Could not fetch shared items.',
145 kind: SnackbarKind.ERROR