Merge remote-tracking branch 'origin/main' into 18692-frozen-projects-workbench-support
[arvados.git] / src / views / search-results-panel / search-results-panel-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { useEffect, useCallback, useState } from 'react';
6 import { SortDirection } from 'components/data-table/data-column';
7 import { DataColumns } from 'components/data-table/data-table';
8 import { DataTableFilterItem } from 'components/data-table-filters/data-table-filters';
9 import { extractUuidKind, ResourceKind } from 'models/resource';
10 import { ContainerRequestState } from 'models/container-request';
11 import { SEARCH_RESULTS_PANEL_ID } from 'store/search-results-panel/search-results-panel-actions';
12 import { DataExplorer } from 'views-components/data-explorer/data-explorer';
13 import {
14     ResourceCluster,
15     ResourceFileSize,
16     ResourceLastModifiedDate,
17     ResourceName,
18     ResourceOwnerWithName,
19     ResourceStatus,
20     ResourceType
21 } from 'views-components/data-explorer/renderers';
22 import servicesProvider from 'common/service-provider';
23 import { createTree } from 'models/tree';
24 import { getInitialResourceTypeFilters } from 'store/resource-type-filters/resource-type-filters';
25 import { SearchResultsPanelProps } from "./search-results-panel";
26 import { Routes } from 'routes/routes';
27 import { Link } from 'react-router-dom';
28 import { StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core';
29 import { ArvadosTheme } from 'common/custom-theme';
30 import { getSearchSessions } from 'store/search-bar/search-bar-actions';
31 import { camelCase } from 'lodash';
32
33 export enum SearchResultsPanelColumnNames {
34     CLUSTER = "Cluster",
35     NAME = "Name",
36     STATUS = "Status",
37     TYPE = 'Type',
38     OWNER = "Owner",
39     FILE_SIZE = "File size",
40     LAST_MODIFIED = "Last modified"
41 }
42
43 export type CssRules = 'siteManagerLink' | 'searchResults';
44
45 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
46     searchResults: {
47         width: '100%'
48     },
49     siteManagerLink: {
50         marginRight: theme.spacing.unit * 2,
51         float: 'right'
52     }
53 });
54
55 export interface WorkflowPanelFilter extends DataTableFilterItem {
56     type: ResourceKind | ContainerRequestState;
57 }
58
59 export const searchResultsPanelColumns: DataColumns<string> = [
60     {
61         name: SearchResultsPanelColumnNames.CLUSTER,
62         selected: true,
63         configurable: true,
64         filters: createTree(),
65         render: (uuid: string) => <ResourceCluster uuid={uuid} />
66     },
67     {
68         name: SearchResultsPanelColumnNames.NAME,
69         selected: true,
70         configurable: true,
71         sortDirection: SortDirection.NONE,
72         filters: createTree(),
73         render: (uuid: string) => <ResourceName uuid={uuid} />
74     },
75     {
76         name: SearchResultsPanelColumnNames.STATUS,
77         selected: true,
78         configurable: true,
79         filters: createTree(),
80         render: uuid => <ResourceStatus uuid={uuid} />
81     },
82     {
83         name: SearchResultsPanelColumnNames.TYPE,
84         selected: true,
85         configurable: true,
86         filters: getInitialResourceTypeFilters(),
87         render: (uuid: string) => <ResourceType uuid={uuid} />,
88     },
89     {
90         name: SearchResultsPanelColumnNames.OWNER,
91         selected: true,
92         configurable: true,
93         filters: createTree(),
94         render: uuid => <ResourceOwnerWithName uuid={uuid} />
95     },
96     {
97         name: SearchResultsPanelColumnNames.FILE_SIZE,
98         selected: true,
99         configurable: true,
100         filters: createTree(),
101         render: uuid => <ResourceFileSize uuid={uuid} />
102     },
103     {
104         name: SearchResultsPanelColumnNames.LAST_MODIFIED,
105         selected: true,
106         configurable: true,
107         sortDirection: SortDirection.DESC,
108         filters: createTree(),
109         render: uuid => <ResourceLastModifiedDate uuid={uuid} />
110     }
111 ];
112
113 export const SearchResultsPanelView = withStyles(styles, { withTheme: true })(
114     (props: SearchResultsPanelProps & WithStyles<CssRules, true>) => {
115         const homeCluster = props.user.uuid.substring(0, 5);
116         const loggedIn = props.sessions.filter((ss) => ss.loggedIn && ss.userIsActive);
117         const [selectedItem, setSelectedItem] = useState('');
118         const [itemPath, setItemPath] = useState<string[]>([]);
119
120         useEffect(() => {
121             let tmpPath: string[] = [];
122
123             (async () => {
124                 if (selectedItem !== '') {
125                     let searchUuid = selectedItem;
126                     let itemKind = extractUuidKind(searchUuid);
127
128                     while (itemKind !== ResourceKind.USER) {
129                         const clusterId = searchUuid.split('-')[0];
130                         const serviceType = camelCase(itemKind?.replace('arvados#', ''));
131                         const service = Object.values(servicesProvider.getServices())
132                             .filter(({resourceType}) => !!resourceType)
133                             .find(({resourceType}) => camelCase(resourceType).indexOf(serviceType) > -1);
134                         const sessions = getSearchSessions(clusterId, props.sessions);
135
136                         if (sessions.length > 0) {
137                             const session = sessions[0];
138                             const { name, ownerUuid } = await (service as any).get(searchUuid, false, undefined, session);
139                             tmpPath.push(name);
140                             searchUuid = ownerUuid;
141                             itemKind = extractUuidKind(searchUuid);
142                         } else {
143                             break;
144                         }
145                     }
146
147                     tmpPath.push(props.user.uuid === searchUuid ? 'Projects' : 'Shared with me');
148                     setItemPath(tmpPath);
149                 }
150             })();
151
152         // eslint-disable-next-line react-hooks/exhaustive-deps
153         }, [selectedItem]);
154
155         const onItemClick = useCallback((uuid) => {
156             setSelectedItem(uuid);
157             props.onItemClick(uuid);
158         // eslint-disable-next-line react-hooks/exhaustive-deps
159         },[props.onItemClick]);
160
161         return <span data-cy='search-results' className={props.classes.searchResults}>
162             <DataExplorer
163             id={SEARCH_RESULTS_PANEL_ID}
164             onRowClick={onItemClick}
165             onRowDoubleClick={props.onItemDoubleClick}
166             onContextMenu={props.onContextMenu}
167             contextMenuColumn={false}
168             elementPath={`/ ${itemPath.reverse().join(' / ')}`}
169             hideSearchInput
170             title={
171                 <div>
172                     {loggedIn.length === 1 ?
173                         <span>Searching local cluster <ResourceCluster uuid={props.localCluster} /></span>
174                         : <span>Searching clusters: {loggedIn.map((ss) => <span key={ss.clusterId}>
175                             <a href={props.remoteHostsConfig[ss.clusterId] && props.remoteHostsConfig[ss.clusterId].workbench2Url} style={{ textDecoration: 'none' }}> <ResourceCluster uuid={ss.clusterId} /></a>
176                         </span>)}</span>}
177                     {loggedIn.length === 1 && props.localCluster !== homeCluster ?
178                         <span>To search multiple clusters, <a href={props.remoteHostsConfig[homeCluster] && props.remoteHostsConfig[homeCluster].workbench2Url}> start from your home Workbench.</a></span>
179                         : <span style={{ marginLeft: "2em" }}>Use <Link to={Routes.SITE_MANAGER} >Site Manager</Link> to manage which clusters will be searched.</span>}
180                 </div >
181             }
182         /></span>;
183     });