1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
16 ResourceLastModifiedDate,
18 ResourceOwnerWithName,
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';
33 export enum SearchResultsPanelColumnNames {
39 FILE_SIZE = "File size",
40 LAST_MODIFIED = "Last modified"
43 export type CssRules = 'siteManagerLink' | 'searchResults';
45 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
50 marginRight: theme.spacing.unit * 2,
55 export interface WorkflowPanelFilter extends DataTableFilterItem {
56 type: ResourceKind | ContainerRequestState;
59 export const searchResultsPanelColumns: DataColumns<string> = [
61 name: SearchResultsPanelColumnNames.CLUSTER,
64 filters: createTree(),
65 render: (uuid: string) => <ResourceCluster uuid={uuid} />
68 name: SearchResultsPanelColumnNames.NAME,
71 sortDirection: SortDirection.NONE,
72 filters: createTree(),
73 render: (uuid: string) => <ResourceName uuid={uuid} />
76 name: SearchResultsPanelColumnNames.STATUS,
79 filters: createTree(),
80 render: uuid => <ResourceStatus uuid={uuid} />
83 name: SearchResultsPanelColumnNames.TYPE,
86 filters: getInitialResourceTypeFilters(),
87 render: (uuid: string) => <ResourceType uuid={uuid} />,
90 name: SearchResultsPanelColumnNames.OWNER,
93 filters: createTree(),
94 render: uuid => <ResourceOwnerWithName uuid={uuid} />
97 name: SearchResultsPanelColumnNames.FILE_SIZE,
100 filters: createTree(),
101 render: uuid => <ResourceFileSize uuid={uuid} />
104 name: SearchResultsPanelColumnNames.LAST_MODIFIED,
107 sortDirection: SortDirection.DESC,
108 filters: createTree(),
109 render: uuid => <ResourceLastModifiedDate uuid={uuid} />
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[]>([]);
121 let tmpPath: string[] = [];
124 if (selectedItem !== '') {
125 let searchUuid = selectedItem;
126 let itemKind = extractUuidKind(searchUuid);
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);
136 if (sessions.length > 0) {
137 const session = sessions[0];
138 const { name, ownerUuid } = await (service as any).get(searchUuid, false, undefined, session);
140 searchUuid = ownerUuid;
141 itemKind = extractUuidKind(searchUuid);
147 tmpPath.push(props.user.uuid === searchUuid ? 'Projects' : 'Shared with me');
148 setItemPath(tmpPath);
152 // eslint-disable-next-line react-hooks/exhaustive-deps
155 const onItemClick = useCallback((uuid) => {
156 setSelectedItem(uuid);
157 props.onItemClick(uuid);
158 // eslint-disable-next-line react-hooks/exhaustive-deps
159 },[props.onItemClick]);
161 return <span data-cy='search-results' className={props.classes.searchResults}>
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(' / ')}`}
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>
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>}