Merge branch '21893-input-collections-virtual-list' into main. Closes #21893
[arvados.git] / services / workbench2 / src / store / collections-content-address-panel / collections-content-address-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, dataExplorerToListParams, getOrder } 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 { DataExplorer, 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 { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
15 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
16 import { updateFavorites } from 'store/favorites/favorites-actions';
17 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
18 import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
19 import { ResourceKind, extractUuidKind } from 'models/resource';
20 import { ownerNameActions } from 'store/owner-name/owner-name-actions';
21 import { getUserDisplayName } from 'models/user';
22 import { CollectionResource } from 'models/collection';
23 import { replace } from "react-router-redux";
24 import { getNavUrl } from 'routes/routes';
25 import { ListArguments, ListResults } from 'services/common-service/common-service';
26 import { couldNotFetchItemsAvailable } from 'store/data-explorer/data-explorer-action';
27
28 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
29     constructor(private services: ServiceRepository, id: string) {
30         super(id);
31     }
32
33     async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
34         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
35         if (!dataExplorer) {
36             api.dispatch(collectionPanelDataExplorerIsNotSet());
37         } else {
38             try {
39                 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
40
41                 const state = api.getState();
42                 const userUuid = getUserUuid(state);
43                 const pathname = state.router.location!.pathname;
44                 const contentAddress = pathname.split('/')[2];
45
46                 // Get items
47                 const response = await this.services.collectionService.list(getParams(dataExplorer, contentAddress));
48                 const userUuids = response.items.map(it => {
49                     if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
50                         return it.ownerUuid;
51                     } else {
52                         return '';
53                     }
54                 }
55                 );
56                 const groupUuids = response.items.map(it => {
57                     if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
58                         return it.ownerUuid;
59                     } else {
60                         return '';
61                     }
62                 });
63                 const responseUsers = await this.services.userService.list({
64                     filters: new FilterBuilder()
65                         .addIn('uuid', userUuids)
66                         .getFilters(),
67                     count: "none"
68                 });
69                 const responseGroups = await this.services.groupsService.list({
70                     filters: new FilterBuilder()
71                         .addIn('uuid', groupUuids)
72                         .getFilters(),
73                     count: "none"
74                 });
75                 responseUsers.items.forEach(it => {
76                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
77                         name: it.uuid === userUuid
78                             ? 'User: Me'
79                             : `User: ${getUserDisplayName(it)}`,
80                         uuid: it.uuid
81                     }));
82                 });
83                 responseGroups.items.forEach(it => {
84                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
85                 });
86                 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
87                 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
88                 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
89                 if (response.itemsAvailable === 1) {
90                     api.dispatch<any>(replace(getNavUrl(response.items[0].uuid, api.getState().auth)));
91                 } else {
92                     api.dispatch(resourcesActions.SET_RESOURCES(response.items));
93                     api.dispatch(collectionsContentAddressActions.SET_ITEMS({
94                         items: response.items.map((resource: any) => resource.uuid),
95                         itemsAvailable: response.itemsAvailable,
96                         page: Math.floor(response.offset / response.limit),
97                         rowsPerPage: response.limit
98                     }));
99                 }
100             } catch (e) {
101                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
102                     items: [],
103                     itemsAvailable: 0,
104                     page: 0,
105                     rowsPerPage: dataExplorer.rowsPerPage
106                 }));
107                 api.dispatch(couldNotFetchCollections());
108             } finally {
109                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
110             }
111         }
112     }
113
114     async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
115         const state = api.getState();
116         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
117         const pathname = state.router.location!.pathname;
118         const contentAddress = pathname.split('/')[2];
119
120         if (criteriaChanged) {
121             // Get itemsAvailable
122             return this.services.collectionService.list(getCountParams(dataExplorer, contentAddress))
123                 .then((results: ListResults<CollectionResource>) => {
124                     if (results.itemsAvailable !== undefined) {
125                         api.dispatch<any>(collectionsContentAddressActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
126                     } else {
127                         couldNotFetchItemsAvailable();
128                     }
129                 });
130         }
131     }
132 }
133
134 const getFilters = (dataExplorer: DataExplorer, contentAddress: string) => (
135     new FilterBuilder()
136         .addEqual('portable_data_hash', contentAddress)
137         .addILike("name", dataExplorer.searchValue)
138         .getFilters()
139 );
140
141 const getParams = (dataExplorer: DataExplorer, contentAddress: string): ListArguments => ({
142     ...dataExplorerToListParams(dataExplorer),
143     filters: getFilters(dataExplorer, contentAddress),
144     order: getOrder<CollectionResource>(dataExplorer),
145     includeOldVersions: true,
146     count: 'none',
147 });
148
149 const getCountParams = (dataExplorer: DataExplorer, contentAddress: string): ListArguments => ({
150     limit: 0,
151     count: 'exact',
152     filters: getFilters(dataExplorer, contentAddress),
153     includeOldVersions: true,
154 });
155
156 const collectionPanelDataExplorerIsNotSet = () =>
157     snackbarActions.OPEN_SNACKBAR({
158         message: 'Collection panel is not ready.',
159         kind: SnackbarKind.ERROR
160     });
161
162 const couldNotFetchCollections = () =>
163     snackbarActions.OPEN_SNACKBAR({
164         message: 'Could not fetch collection with this content address.',
165         kind: SnackbarKind.ERROR
166     });