1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { IconButton, Tabs, Tab, Typography, Grid, Tooltip } from '@material-ui/core';
7 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
8 import { Transition } from 'react-transition-group';
9 import { ArvadosTheme } from 'common/custom-theme';
10 import classnames from "classnames";
11 import { connect } from 'react-redux';
12 import { RootState } from 'store/store';
13 import { CloseIcon } from 'components/icon/icon';
14 import { EmptyResource } from 'models/empty';
15 import { Dispatch } from "redux";
16 import { ResourceKind } from "models/resource";
17 import { ProjectDetails } from "./project-details";
18 import { RootProjectDetails } from './root-project-details';
19 import { CollectionDetails } from "./collection-details";
20 import { ProcessDetails } from "./process-details";
21 import { EmptyDetails } from "./empty-details";
22 import { WorkflowDetails } from "./workflow-details";
23 import { DetailsData } from "./details-data";
24 import { DetailsResource } from "models/details";
25 import { Config } from 'common/config';
26 import { isInlineFileUrlSafe } from "../context-menu/actions/helpers";
27 import { getResource } from 'store/resources/resources';
28 import { toggleDetailsPanel, SLIDE_TIMEOUT, openDetailsPanel } from 'store/details-panel/details-panel-action';
29 import { FileDetails } from 'views-components/details-panel/file-details';
30 import { getNode } from 'models/tree';
31 import { resourceIsFrozen } from 'common/frozen-resources';
32 import { CLOSE_DRAWER } from 'store/details-panel/details-panel-action';
34 type CssRules = 'root' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
36 const DRAWER_WIDTH = 320;
37 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
39 background: theme.palette.background.paper,
40 borderLeft: `1px solid ${theme.palette.divider}`,
43 transition: `width ${SLIDE_TIMEOUT}ms ease`,
54 color: theme.palette.grey["600"],
55 margin: `${theme.spacing.unit}px 0`,
63 padding: theme.spacing.unit * 1,
67 const EMPTY_RESOURCE: EmptyResource = { kind: undefined, name: 'Projects' };
69 const getItem = (res: DetailsResource, pathName: string): DetailsData => {
72 case ResourceKind.PROJECT:
73 return new ProjectDetails(res);
74 case ResourceKind.COLLECTION:
75 return new CollectionDetails(res);
76 case ResourceKind.PROCESS:
77 return new ProcessDetails(res);
78 case ResourceKind.WORKFLOW:
79 return new WorkflowDetails(res);
80 case ResourceKind.USER:
81 if(pathName.includes('projects')) {
82 return new RootProjectDetails(res);
84 return new EmptyDetails(EMPTY_RESOURCE);
86 return new EmptyDetails(res as EmptyResource);
89 return new FileDetails(res);
93 const mapStateToProps = ({ auth, detailsPanel, resources, collectionPanelFiles, selectedResourceUuid, properties, router }: RootState) => {
94 const resource = getResource(selectedResourceUuid ?? properties.currentRouteUuid)(resources) as DetailsResource | undefined;
97 : getNode(detailsPanel.resourceUuid)(collectionPanelFiles);
101 isFrozen = resourceIsFrozen(resource, resources);
106 authConfig: auth.config,
107 isOpened: detailsPanel.isOpened,
108 tabNr: detailsPanel.tabNr,
109 res: resource || (file && file.value) || EMPTY_RESOURCE,
110 pathname: router.location ? router.location.pathname : "",
114 const mapDispatchToProps = (dispatch: Dispatch) => ({
115 onCloseDrawer: (currentItemId) => {
116 dispatch<any>(toggleDetailsPanel(currentItemId));
118 setActiveTab: (tabNr: number) => {
119 dispatch<any>(openDetailsPanel(undefined, tabNr));
123 export interface DetailsPanelDataProps {
124 onCloseDrawer: (currentItemId) => void;
125 setActiveTab: (tabNr: number) => void;
129 res: DetailsResource;
134 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
136 export const DetailsPanel = withStyles(styles)(
137 connect(mapStateToProps, mapDispatchToProps)(
138 class extends React.Component<DetailsPanelProps> {
139 shouldComponentUpdate(nextProps: DetailsPanelProps) {
140 if ('etag' in nextProps.res && 'etag' in this.props.res &&
141 nextProps.res.etag === this.props.res.etag &&
142 nextProps.isOpened === this.props.isOpened &&
143 nextProps.tabNr === this.props.tabNr) {
149 handleChange = (event: any, value: number) => {
150 this.props.setActiveTab(value);
154 const { classes, isOpened } = this.props;
159 className={classnames([classes.root, { [classes.opened]: isOpened }])}>
162 timeout={SLIDE_TIMEOUT}
164 {isOpened ? this.renderContent() : <div />}
171 const { classes, onCloseDrawer, res, tabNr, authConfig, pathname } = this.props;
172 let shouldShowInlinePreview = false;
173 if (!('kind' in res)) {
174 shouldShowInlinePreview = isInlineFileUrlSafe(
176 authConfig.keepWebServiceUrl,
177 authConfig.keepWebInlineServiceUrl
178 ) || authConfig.clusterConfig.Collections.TrustAllContent;
181 const item = getItem(res, pathname);
183 data-cy='details-panel'
188 className={classes.container} >
191 className={classes.headerContainer}
194 justify='space-around'
197 {item.getIcon(classes.headerIcon)}
200 <Tooltip title={item.getTitle()}>
201 <Typography variant='h6' noWrap>
207 <IconButton data-cy="close-details-btn" color="inherit" onClick={()=>onCloseDrawer(CLOSE_DRAWER)}>
213 <Tabs onChange={this.handleChange}
214 value={(item.getTabLabels().length >= tabNr + 1) ? tabNr : 0}>
215 {item.getTabLabels().map((tabLabel, idx) =>
216 <Tab key={`tab-label-${idx}`} disableRipple label={tabLabel} />)
220 <Grid item xs className={this.props.classes.tabContainer} >
221 {item.getDetails({ tabNr, showPreview: shouldShowInlinePreview })}