Merge branch '15803-unsetup' refs #15803
[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 * as 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 } 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         item: getItem(resource || (file && file.value) || EMPTY_RESOURCE),
86     };
87 };
88
89 const mapDispatchToProps = (dispatch: Dispatch) => ({
90     onCloseDrawer: () => {
91         dispatch<any>(toggleDetailsPanel());
92     }
93 });
94
95 export interface DetailsPanelDataProps {
96     onCloseDrawer: () => void;
97     isOpened: boolean;
98     item: DetailsData;
99 }
100
101 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
102
103 export const DetailsPanel = withStyles(styles)(
104     connect(mapStateToProps, mapDispatchToProps)(
105         class extends React.Component<DetailsPanelProps> {
106             state = {
107                 tabsValue: 0
108             };
109
110             handleChange = (event: any, value: boolean) => {
111                 this.setState({ tabsValue: value });
112             }
113
114             render() {
115                 const { classes, isOpened } = this.props;
116                 return (
117                     <Grid
118                         container
119                         direction="column"
120                         className={classnames([classes.root, { [classes.opened]: isOpened }])}>
121                         <Transition
122                             in={isOpened}
123                             timeout={SLIDE_TIMEOUT}
124                             unmountOnExit>
125                             {this.renderContent()}
126                         </Transition>
127                     </Grid>
128                 );
129             }
130
131             renderContent() {
132                 const { classes, onCloseDrawer, item } = this.props;
133                 const { tabsValue } = this.state;
134                 return <Grid
135                     container
136                     direction="column"
137                     item
138                     xs
139                     className={classes.container} >
140                     <Grid
141                         item
142                         className={classes.headerContainer}
143                         container
144                         alignItems='center'
145                         justify='space-around'
146                         wrap="nowrap">
147                         <Grid item xs={2}>
148                             {item.getIcon(classes.headerIcon)}
149                         </Grid>
150                         <Grid item xs={8}>
151                             <Tooltip title={item.getTitle()}>
152                                 <Typography variant='h6' noWrap>
153                                     {item.getTitle()}
154                                 </Typography>
155                             </Tooltip>
156                         </Grid>
157                         <Grid item>
158                             <IconButton color="inherit" onClick={onCloseDrawer}>
159                                 <CloseIcon />
160                             </IconButton>
161                         </Grid>
162                     </Grid>
163                     <Grid item>
164                         <Tabs value={tabsValue} onChange={this.handleChange}>
165                             <Tab disableRipple label="Details" />
166                             <Tab disableRipple label="Activity" disabled />
167                         </Tabs>
168                     </Grid>
169                     <Grid item xs className={this.props.classes.tabContainer} >
170                         {tabsValue === 0
171                             ? item.getDetails()
172                             : null}
173                     </Grid>
174                 </Grid >;
175             }
176         }
177     )
178 );