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 { CollectionDetails } from "./collection-details";
19 import { ProcessDetails } from "./process-details";
20 import { EmptyDetails } from "./empty-details";
21 import { DetailsData } from "./details-data";
22 import { DetailsResource } from "models/details";
23 import { Config } from 'common/config';
24 import { isInlineFileUrlSafe } from "../context-menu/actions/helpers";
25 import { getResource } from 'store/resources/resources';
26 import { toggleDetailsPanel, SLIDE_TIMEOUT, openDetailsPanel } from 'store/details-panel/details-panel-action';
27 import { FileDetails } from 'views-components/details-panel/file-details';
28 import { getNode } from 'models/tree';
30 type CssRules = 'root' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
32 const DRAWER_WIDTH = 320;
33 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
35 background: theme.palette.background.paper,
36 borderLeft: `1px solid ${theme.palette.divider}`,
39 transition: `width ${SLIDE_TIMEOUT}ms ease`,
50 color: theme.palette.grey["600"],
51 margin: `${theme.spacing.unit}px 0`,
59 padding: theme.spacing.unit * 1,
63 const EMPTY_RESOURCE: EmptyResource = { kind: undefined, name: 'Projects' };
65 const getItem = (res: DetailsResource): DetailsData => {
68 case ResourceKind.PROJECT:
69 return new ProjectDetails(res);
70 case ResourceKind.COLLECTION:
71 return new CollectionDetails(res);
72 case ResourceKind.PROCESS:
73 return new ProcessDetails(res);
75 return new EmptyDetails(res);
78 return new FileDetails(res);
82 const mapStateToProps = ({ auth, detailsPanel, resources, collectionPanelFiles }: RootState) => {
83 const resource = getResource(detailsPanel.resourceUuid)(resources) as DetailsResource | undefined;
86 : getNode(detailsPanel.resourceUuid)(collectionPanelFiles);
88 authConfig: auth.config,
89 isOpened: detailsPanel.isOpened,
90 tabNr: detailsPanel.tabNr,
91 res: resource || (file && file.value) || EMPTY_RESOURCE,
95 const mapDispatchToProps = (dispatch: Dispatch) => ({
96 onCloseDrawer: () => {
97 dispatch<any>(toggleDetailsPanel());
99 setActiveTab: (tabNr: number) => {
100 dispatch<any>(openDetailsPanel(undefined, tabNr));
104 export interface DetailsPanelDataProps {
105 onCloseDrawer: () => void;
106 setActiveTab: (tabNr: number) => void;
110 res: DetailsResource;
113 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
115 export const DetailsPanel = withStyles(styles)(
116 connect(mapStateToProps, mapDispatchToProps)(
117 class extends React.Component<DetailsPanelProps> {
118 shouldComponentUpdate(nextProps: DetailsPanelProps) {
119 if ('etag' in nextProps.res && 'etag' in this.props.res &&
120 nextProps.res.etag === this.props.res.etag &&
121 nextProps.isOpened === this.props.isOpened &&
122 nextProps.tabNr === this.props.tabNr) {
128 handleChange = (event: any, value: number) => {
129 this.props.setActiveTab(value);
133 const { classes, isOpened } = this.props;
138 className={classnames([classes.root, { [classes.opened]: isOpened }])}>
141 timeout={SLIDE_TIMEOUT}
143 {isOpened ? this.renderContent() : <div />}
150 const { classes, onCloseDrawer, res, tabNr, authConfig } = this.props;
152 let shouldShowInlinePreview = false;
153 if (!('kind' in res)) {
154 shouldShowInlinePreview = isInlineFileUrlSafe(
156 authConfig.keepWebServiceUrl,
157 authConfig.keepWebInlineServiceUrl
158 ) || authConfig.clusterConfig.Collections.TrustAllContent;
161 const item = getItem(res);
163 data-cy='details-panel'
168 className={classes.container} >
171 className={classes.headerContainer}
174 justify='space-around'
177 {item.getIcon(classes.headerIcon)}
180 <Tooltip title={item.getTitle()}>
181 <Typography variant='h6' noWrap>
187 <IconButton color="inherit" onClick={onCloseDrawer}>
193 <Tabs onChange={this.handleChange}
194 value={(item.getTabLabels().length >= tabNr+1) ? tabNr : 0}>
195 { item.getTabLabels().map((tabLabel, idx) =>
196 <Tab key={`tab-label-${idx}`} disableRipple label={tabLabel} />)
200 <Grid item xs className={this.props.classes.tabContainer} >
201 {item.getDetails({tabNr, showPreview: shouldShowInlinePreview})}