1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ServiceRepository } from '~/services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService } from '~/store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from '~/store/store';
9 import { getUserUuid } from "~/common/getuser";
10 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
11 import { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
12 import { resourcesActions } from '~/store/resources/resources-actions';
13 import { FilterBuilder } from '~/services/api/filter-builder';
14 import { SortDirection } from '~/components/data-table/data-column';
15 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
16 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
17 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
18 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
19 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
20 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
21 import { navigateTo } from '~/store/navigation/navigation-action';
22 import { updateFavorites } from '~/store/favorites/favorites-actions';
23 import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
24 import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
25 import { ResourceKind, extractUuidKind } from '~/models/resource';
26 import { ownerNameActions } from '~/store/owner-name/owner-name-actions';
28 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
29 constructor(private services: ServiceRepository, id: string) {
33 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
34 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
36 api.dispatch(collectionPanelDataExplorerIsNotSet());
38 const sortColumn = getSortColumn(dataExplorer);
40 const contentOrder = new OrderBuilder<GroupContentsResource>();
42 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
43 const direction = sortColumn.sortDirection === SortDirection.ASC
45 : OrderDirection.DESC;
48 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
51 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
52 const userUuid = getUserUuid(api.getState());
53 const pathname = api.getState().router.location!.pathname;
54 const contentAddress = pathname.split('/')[2];
55 const response = await this.services.collectionService.list({
56 limit: dataExplorer.rowsPerPage,
57 offset: dataExplorer.page * dataExplorer.rowsPerPage,
58 filters: new FilterBuilder()
59 .addEqual('portable_data_hash', contentAddress)
60 .addILike("name", dataExplorer.searchValue)
63 const userUuids = response.items.map(it => {
64 if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
71 const groupUuids = response.items.map(it => {
72 if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
78 const responseUsers = await this.services.userService.list({
79 limit: dataExplorer.rowsPerPage,
80 offset: dataExplorer.page * dataExplorer.rowsPerPage,
81 filters: new FilterBuilder()
82 .addIn('uuid', userUuids)
85 const responseGroups = await this.services.groupsService.list({
86 limit: dataExplorer.rowsPerPage,
87 offset: dataExplorer.page * dataExplorer.rowsPerPage,
88 filters: new FilterBuilder()
89 .addIn('uuid', groupUuids)
92 responseUsers.items.map(it => {
93 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: it.uuid === userUuid ? 'User: Me' : `User: ${it.firstName} ${it.lastName}`, uuid: it.uuid }));
95 responseGroups.items.map(it => {
96 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
98 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
99 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
100 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
101 if (response.itemsAvailable === 1) {
102 api.dispatch<any>(navigateTo(response.items[0].uuid));
103 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
105 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
106 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
107 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
108 items: response.items.map((resource: any) => resource.uuid),
109 itemsAvailable: response.itemsAvailable,
110 page: Math.floor(response.offset / response.limit),
111 rowsPerPage: response.limit
115 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
116 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
120 rowsPerPage: dataExplorer.rowsPerPage
122 api.dispatch(couldNotFetchCollections());
128 const collectionPanelDataExplorerIsNotSet = () =>
129 snackbarActions.OPEN_SNACKBAR({
130 message: 'Collection panel is not ready.',
131 kind: SnackbarKind.ERROR
134 const couldNotFetchCollections = () =>
135 snackbarActions.OPEN_SNACKBAR({
136 message: 'Could not fetch collection with this content address.',
137 kind: SnackbarKind.ERROR