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 { getResource } from 'store/resources/resources';
24 import { toggleDetailsPanel, SLIDE_TIMEOUT, openDetailsPanel } from 'store/details-panel/details-panel-action';
25 import { FileDetails } from 'views-components/details-panel/file-details';
26 import { getNode } from 'models/tree';
28 type CssRules = 'root' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
30 const DRAWER_WIDTH = 320;
31 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
33 background: theme.palette.background.paper,
34 borderLeft: `1px solid ${theme.palette.divider}`,
37 transition: `width ${SLIDE_TIMEOUT}ms ease`,
48 color: theme.palette.grey["600"],
49 margin: `${theme.spacing.unit}px 0`,
57 padding: theme.spacing.unit * 1,
61 const EMPTY_RESOURCE: EmptyResource = { kind: undefined, name: 'Projects' };
63 const getItem = (res: DetailsResource): DetailsData => {
66 case ResourceKind.PROJECT:
67 return new ProjectDetails(res);
68 case ResourceKind.COLLECTION:
69 return new CollectionDetails(res);
70 case ResourceKind.PROCESS:
71 return new ProcessDetails(res);
73 return new EmptyDetails(res);
76 return new FileDetails(res);
80 const mapStateToProps = ({ detailsPanel, resources, collectionPanelFiles }: RootState) => {
81 const resource = getResource(detailsPanel.resourceUuid)(resources) as DetailsResource | undefined;
84 : getNode(detailsPanel.resourceUuid)(collectionPanelFiles);
86 isOpened: detailsPanel.isOpened,
87 tabNr: detailsPanel.tabNr,
88 res: resource || (file && file.value) || EMPTY_RESOURCE,
92 const mapDispatchToProps = (dispatch: Dispatch) => ({
93 onCloseDrawer: () => {
94 dispatch<any>(toggleDetailsPanel());
96 setActiveTab: (tabNr: number) => {
97 dispatch<any>(openDetailsPanel(undefined, tabNr));
101 export interface DetailsPanelDataProps {
102 onCloseDrawer: () => void;
103 setActiveTab: (tabNr: number) => void;
106 res: DetailsResource;
109 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
111 export const DetailsPanel = withStyles(styles)(
112 connect(mapStateToProps, mapDispatchToProps)(
113 class extends React.Component<DetailsPanelProps> {
114 shouldComponentUpdate(nextProps: DetailsPanelProps) {
115 if ('etag' in nextProps.res && 'etag' in this.props.res &&
116 nextProps.res.etag === this.props.res.etag &&
117 nextProps.isOpened === this.props.isOpened &&
118 nextProps.tabNr === this.props.tabNr) {
124 handleChange = (event: any, value: number) => {
125 this.props.setActiveTab(value);
129 const { classes, isOpened } = this.props;
134 className={classnames([classes.root, { [classes.opened]: isOpened }])}>
137 timeout={SLIDE_TIMEOUT}
139 {isOpened ? this.renderContent() : <div />}
146 const { classes, onCloseDrawer, res, tabNr } = this.props;
147 const item = getItem(res);
153 className={classes.container} >
156 className={classes.headerContainer}
159 justify='space-around'
162 {item.getIcon(classes.headerIcon)}
165 <Tooltip title={item.getTitle()}>
166 <Typography variant='h6' noWrap>
172 <IconButton color="inherit" onClick={onCloseDrawer}>
178 <Tabs onChange={this.handleChange}
179 value={(item.getTabLabels().length >= tabNr+1) ? tabNr : 0}>
180 { item.getTabLabels().map((tabLabel, idx) =>
181 <Tab key={`tab-label-${idx}`} disableRipple label={tabLabel} />)
185 <Grid item xs className={this.props.classes.tabContainer} >
186 {item.getDetails(tabNr)}