13494: Allows the user to open the versions tab by clicking on the version nr.
[arvados-workbench2.git] / src / views-components / details-panel / details-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as 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';
27
28 type CssRules = 'root' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
29
30 const DRAWER_WIDTH = 320;
31 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
32     root: {
33         background: theme.palette.background.paper,
34         borderLeft: `1px solid ${theme.palette.divider}`,
35         height: '100%',
36         overflow: 'hidden',
37         transition: `width ${SLIDE_TIMEOUT}ms ease`,
38         width: 0,
39     },
40     opened: {
41         width: DRAWER_WIDTH,
42     },
43     container: {
44         maxWidth: 'none',
45         width: DRAWER_WIDTH,
46     },
47     headerContainer: {
48         color: theme.palette.grey["600"],
49         margin: `${theme.spacing.unit}px 0`,
50         textAlign: 'center',
51     },
52     headerIcon: {
53         fontSize: '2.125rem',
54     },
55     tabContainer: {
56         overflow: 'auto',
57         padding: theme.spacing.unit * 1,
58     },
59 });
60
61 const EMPTY_RESOURCE: EmptyResource = { kind: undefined, name: 'Projects' };
62
63 const getItem = (res: DetailsResource): DetailsData => {
64     if ('kind' in res) {
65         switch (res.kind) {
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);
72             default:
73                 return new EmptyDetails(res);
74         }
75     } else {
76         return new FileDetails(res);
77     }
78 };
79
80 const mapStateToProps = ({ detailsPanel, resources, collectionPanelFiles }: RootState) => {
81     const resource = getResource(detailsPanel.resourceUuid)(resources) as DetailsResource | undefined;
82     const file = getNode(detailsPanel.resourceUuid)(collectionPanelFiles);
83     return {
84         isOpened: detailsPanel.isOpened,
85         tabNr: detailsPanel.tabNr,
86         item: getItem(resource || (file && file.value) || EMPTY_RESOURCE),
87     };
88 };
89
90 const mapDispatchToProps = (dispatch: Dispatch) => ({
91     onCloseDrawer: () => {
92         dispatch<any>(toggleDetailsPanel());
93     },
94     setActiveTab: (tabNr: number) => {
95         dispatch<any>(openDetailsPanel(undefined, tabNr));
96     },
97 });
98
99 export interface DetailsPanelDataProps {
100     onCloseDrawer: () => void;
101     setActiveTab: (tabNr: number) => void;
102     isOpened: boolean;
103     tabNr: number;
104     item: DetailsData;
105 }
106
107 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
108
109 export const DetailsPanel = withStyles(styles)(
110     connect(mapStateToProps, mapDispatchToProps)(
111         class extends React.Component<DetailsPanelProps> {
112             handleChange = (event: any, value: number) => {
113                 this.props.setActiveTab(value);
114             }
115
116             render() {
117                 const { classes, isOpened } = this.props;
118                 return (
119                     <Grid
120                         container
121                         direction="column"
122                         className={classnames([classes.root, { [classes.opened]: isOpened }])}>
123                         <Transition
124                             in={isOpened}
125                             timeout={SLIDE_TIMEOUT}
126                             unmountOnExit>
127                             {this.renderContent()}
128                         </Transition>
129                     </Grid>
130                 );
131             }
132
133             renderContent() {
134                 const { classes, onCloseDrawer, item, tabNr } = this.props;
135                 return <Grid
136                     container
137                     direction="column"
138                     item
139                     xs
140                     className={classes.container} >
141                     <Grid
142                         item
143                         className={classes.headerContainer}
144                         container
145                         alignItems='center'
146                         justify='space-around'
147                         wrap="nowrap">
148                         <Grid item xs={2}>
149                             {item.getIcon(classes.headerIcon)}
150                         </Grid>
151                         <Grid item xs={8}>
152                             <Tooltip title={item.getTitle()}>
153                                 <Typography variant='h6' noWrap>
154                                     {item.getTitle()}
155                                 </Typography>
156                             </Tooltip>
157                         </Grid>
158                         <Grid item>
159                             <IconButton color="inherit" onClick={onCloseDrawer}>
160                                 <CloseIcon />
161                             </IconButton>
162                         </Grid>
163                     </Grid>
164                     <Grid item>
165                         <Tabs onChange={this.handleChange}
166                             value={(item.getTabLabels().length >= tabNr+1) ? tabNr : 0}>
167                             { item.getTabLabels().map((tabLabel, idx) =>
168                                 <Tab key={`tab-label-${idx}`} disableRipple label={tabLabel} />)
169                             }
170                         </Tabs>
171                     </Grid>
172                     <Grid item xs className={this.props.classes.tabContainer} >
173                         {item.getDetails(tabNr)}
174                     </Grid>
175                 </Grid >;
176             }
177         }
178     )
179 );