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);
85 return new EmptyDetails(res as EmptyResource);
88 return new FileDetails(res);
92 const mapStateToProps = ({ auth, detailsPanel, resources, collectionPanelFiles, selectedResourceUuid, properties, router }: RootState) => {
93 const resource = getResource(selectedResourceUuid ?? properties.currentRouteUuid)(resources) as DetailsResource | undefined;
96 : getNode(detailsPanel.resourceUuid)(collectionPanelFiles);
100 isFrozen = resourceIsFrozen(resource, resources);
105 authConfig: auth.config,
106 isOpened: detailsPanel.isOpened,
107 tabNr: detailsPanel.tabNr,
108 res: resource || (file && file.value) || EMPTY_RESOURCE,
109 pathname: router.location ? router.location.pathname : "",
113 const mapDispatchToProps = (dispatch: Dispatch) => ({
114 onCloseDrawer: (currentItemId) => {
115 dispatch<any>(toggleDetailsPanel(currentItemId));
117 setActiveTab: (tabNr: number) => {
118 dispatch<any>(openDetailsPanel(undefined, tabNr));
122 export interface DetailsPanelDataProps {
123 onCloseDrawer: (currentItemId) => void;
124 setActiveTab: (tabNr: number) => void;
128 res: DetailsResource;
133 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
135 export const DetailsPanel = withStyles(styles)(
136 connect(mapStateToProps, mapDispatchToProps)(
137 class extends React.Component<DetailsPanelProps> {
138 shouldComponentUpdate(nextProps: DetailsPanelProps) {
139 if ('etag' in nextProps.res && 'etag' in this.props.res &&
140 nextProps.res.etag === this.props.res.etag &&
141 nextProps.isOpened === this.props.isOpened &&
142 nextProps.tabNr === this.props.tabNr) {
148 handleChange = (event: any, value: number) => {
149 this.props.setActiveTab(value);
153 const { classes, isOpened } = this.props;
158 className={classnames([classes.root, { [classes.opened]: isOpened }])}>
161 timeout={SLIDE_TIMEOUT}
163 {isOpened ? this.renderContent() : <div />}
170 const { classes, onCloseDrawer, res, tabNr, authConfig, pathname } = this.props;
171 let shouldShowInlinePreview = false;
172 if (!('kind' in res)) {
173 shouldShowInlinePreview = isInlineFileUrlSafe(
175 authConfig.keepWebServiceUrl,
176 authConfig.keepWebInlineServiceUrl
177 ) || authConfig.clusterConfig.Collections.TrustAllContent;
180 const item = getItem(res, pathname);
182 data-cy='details-panel'
187 className={classes.container} >
190 className={classes.headerContainer}
193 justify='space-around'
196 {item.getIcon(classes.headerIcon)}
199 <Tooltip title={item.getTitle()}>
200 <Typography variant='h6' noWrap>
206 <IconButton color="inherit" onClick={()=>onCloseDrawer(CLOSE_DRAWER)}>
212 <Tabs onChange={this.handleChange}
213 value={(item.getTabLabels().length >= tabNr + 1) ? tabNr : 0}>
214 {item.getTabLabels().map((tabLabel, idx) =>
215 <Tab key={`tab-label-${idx}`} disableRipple label={tabLabel} />)
219 <Grid item xs className={this.props.classes.tabContainer} >
220 {item.getDetails({ tabNr, showPreview: shouldShowInlinePreview })}