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';
27 import { getUserDisplayName } from '~/models/user';
29 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
30 constructor(private services: ServiceRepository, id: string) {
34 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
35 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
37 api.dispatch(collectionPanelDataExplorerIsNotSet());
39 const sortColumn = getSortColumn(dataExplorer);
41 const contentOrder = new OrderBuilder<GroupContentsResource>();
43 if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
44 const direction = sortColumn.sortDirection === SortDirection.ASC
46 : OrderDirection.DESC;
49 .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
52 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
53 const userUuid = getUserUuid(api.getState());
54 const pathname = api.getState().router.location!.pathname;
55 const contentAddress = pathname.split('/')[2];
56 const response = await this.services.collectionService.list({
57 limit: dataExplorer.rowsPerPage,
58 offset: dataExplorer.page * dataExplorer.rowsPerPage,
59 filters: new FilterBuilder()
60 .addEqual('portable_data_hash', contentAddress)
61 .addILike("name", dataExplorer.searchValue)
63 includeOldVersions: true
65 const userUuids = response.items.map(it => {
66 if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
73 const groupUuids = response.items.map(it => {
74 if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
80 const responseUsers = await this.services.userService.list({
81 filters: new FilterBuilder()
82 .addIn('uuid', userUuids)
86 const responseGroups = await this.services.groupsService.list({
87 filters: new FilterBuilder()
88 .addIn('uuid', groupUuids)
92 responseUsers.items.map(it => {
93 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
94 name: it.uuid === userUuid
96 : `User: ${getUserDisplayName(it)}`,
100 responseGroups.items.map(it => {
101 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
103 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
104 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
105 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
106 if (response.itemsAvailable === 1) {
107 api.dispatch<any>(navigateTo(response.items[0].uuid));
108 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
111 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
112 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
113 items: response.items.map((resource: any) => resource.uuid),
114 itemsAvailable: response.itemsAvailable,
115 page: Math.floor(response.offset / response.limit),
116 rowsPerPage: response.limit
120 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
121 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
125 rowsPerPage: dataExplorer.rowsPerPage
127 api.dispatch(couldNotFetchCollections());
133 const collectionPanelDataExplorerIsNotSet = () =>
134 snackbarActions.OPEN_SNACKBAR({
135 message: 'Collection panel is not ready.',
136 kind: SnackbarKind.ERROR
139 const couldNotFetchCollections = () =>
140 snackbarActions.OPEN_SNACKBAR({
141 message: 'Could not fetch collection with this content address.',
142 kind: SnackbarKind.ERROR