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)
64 const userUuids = response.items.map(it => {
65 if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
72 const groupUuids = response.items.map(it => {
73 if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
79 const responseUsers = await this.services.userService.list({
80 filters: new FilterBuilder()
81 .addIn('uuid', userUuids)
85 const responseGroups = await this.services.groupsService.list({
86 filters: new FilterBuilder()
87 .addIn('uuid', groupUuids)
91 responseUsers.items.map(it => {
92 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
93 name: it.uuid === userUuid
95 : `User: ${getUserDisplayName(it)}`,
99 responseGroups.items.map(it => {
100 api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
102 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
103 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
104 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
105 if (response.itemsAvailable === 1) {
106 api.dispatch<any>(navigateTo(response.items[0].uuid));
107 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
109 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110 api.dispatch(resourcesActions.SET_RESOURCES(response.items));
111 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
112 items: response.items.map((resource: any) => resource.uuid),
113 itemsAvailable: response.itemsAvailable,
114 page: Math.floor(response.offset / response.limit),
115 rowsPerPage: response.limit
119 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
120 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
124 rowsPerPage: dataExplorer.rowsPerPage
126 api.dispatch(couldNotFetchCollections());
132 const collectionPanelDataExplorerIsNotSet = () =>
133 snackbarActions.OPEN_SNACKBAR({
134 message: 'Collection panel is not ready.',
135 kind: SnackbarKind.ERROR
138 const couldNotFetchCollections = () =>
139 snackbarActions.OPEN_SNACKBAR({
140 message: 'Could not fetch collection with this content address.',
141 kind: SnackbarKind.ERROR