Force window resize after open/close details panel because of material-ui issue
[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 { detailsPanelActions } from "~/store/details-panel/details-panel-action";
14 import { CloseIcon } from '~/components/icon/icon';
15 import { EmptyResource } from '~/models/empty';
16 import { Dispatch } from "redux";
17 import { ResourceKind } from "~/models/resource";
18 import { ProjectDetails } from "./project-details";
19 import { CollectionDetails } from "./collection-details";
20 import { ProcessDetails } from "./process-details";
21 import { EmptyDetails } from "./empty-details";
22 import { DetailsData } from "./details-data";
23 import { DetailsResource } from "~/models/details";
24 import { getResource } from '~/store/resources/resources';
25 import { ResourceData } from "~/store/resources-data/resources-data-reducer";
26 import { getResourceData } from "~/store/resources-data/resources-data";
27 import { toggleDetailsPanel } from '~/store/details-panel/details-panel-action';
28
29 type CssRules = 'root' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
30
31 const DRAWER_WIDTH = 320;
32 const SLIDE_TIMEOUT = 500;
33 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34     root: {
35         background: theme.palette.background.paper,
36         borderLeft: `1px solid ${theme.palette.divider}`,
37         height: '100%',
38         overflow: 'hidden',
39         transition: `width ${SLIDE_TIMEOUT}ms ease`,
40         width: 0,
41     },
42     opened: {
43         width: DRAWER_WIDTH,
44     },
45     container: {
46         maxWidth: 'none',
47         width: DRAWER_WIDTH,
48     },
49     headerContainer: {
50         color: theme.palette.grey["600"],
51         margin: `${theme.spacing.unit}px 0`,
52         textAlign: 'center',
53     },
54     headerIcon: {
55         fontSize: '2.125rem',
56     },
57     tabContainer: {
58         overflow: 'auto',
59         padding: theme.spacing.unit * 3,
60     },
61 });
62
63 const getItem = (resource: DetailsResource, resourceData?: ResourceData): DetailsData => {
64     const res = resource || { kind: undefined, name: 'Projects' };
65     switch (res.kind) {
66         case ResourceKind.PROJECT:
67             return new ProjectDetails(res);
68         case ResourceKind.COLLECTION:
69             return new CollectionDetails(res, resourceData);
70         case ResourceKind.PROCESS:
71             return new ProcessDetails(res);
72         default:
73             return new EmptyDetails(res as EmptyResource);
74     }
75 };
76
77 const mapStateToProps = ({ detailsPanel, resources, resourcesData }: RootState) => {
78     const resource = getResource(detailsPanel.resourceUuid)(resources) as DetailsResource;
79     const resourceData = getResourceData(detailsPanel.resourceUuid)(resourcesData);
80     return {
81         isOpened: detailsPanel.isOpened,
82         item: getItem(resource, resourceData)
83     };
84 };
85
86 const mapDispatchToProps = (dispatch: Dispatch) => ({
87     onCloseDrawer: () => {
88         dispatch<any>(toggleDetailsPanel());
89     }
90 });
91
92 export interface DetailsPanelDataProps {
93     onCloseDrawer: () => void;
94     isOpened: boolean;
95     item: DetailsData;
96 }
97
98 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
99
100 export const DetailsPanel = withStyles(styles)(
101     connect(mapStateToProps, mapDispatchToProps)(
102         class extends React.Component<DetailsPanelProps> {
103             state = {
104                 tabsValue: 0
105             };
106
107             handleChange = (event: any, value: boolean) => {
108                 this.setState({ tabsValue: value });
109             }
110
111             render() {
112                 const { classes, isOpened } = this.props;
113                 return (
114                     <Grid
115                         container
116                         direction="column"
117                         className={classnames([classes.root, { [classes.opened]: isOpened }])}>
118                         <Transition
119                             in={isOpened}
120                             timeout={SLIDE_TIMEOUT}
121                             unmountOnExit>
122                             {this.renderContent()}
123                         </Transition>
124                     </Grid>
125                 );
126             }
127
128             renderContent() {
129                 const { classes, onCloseDrawer, item } = this.props;
130                 const { tabsValue } = this.state;
131                 return <Grid
132                     container
133                     direction="column"
134                     item
135                     xs
136                     className={classes.container} >
137                     <Grid
138                         item
139                         className={classes.headerContainer}
140                         container
141                         alignItems='center'
142                         justify='space-around'
143                         wrap="nowrap">
144                         <Grid item xs={2}>
145                             {item.getIcon(classes.headerIcon)}
146                         </Grid>
147                         <Grid item xs={8}>
148                             <Tooltip title={item.getTitle()}>
149                                 <Typography variant="title" noWrap>
150                                     {item.getTitle()}
151                                 </Typography>
152                             </Tooltip>
153                         </Grid>
154                         <Grid item>
155                             <IconButton color="inherit" onClick={onCloseDrawer}>
156                                 <CloseIcon />
157                             </IconButton>
158                         </Grid>
159                     </Grid>
160                     <Grid item>
161                         <Tabs value={tabsValue} onChange={this.handleChange}>
162                             <Tab disableRipple label="Details" />
163                             <Tab disableRipple label="Activity" disabled />
164                         </Tabs>
165                     </Grid>
166                     <Grid item xs className={this.props.classes.tabContainer} >
167                         {tabsValue === 0
168                             ? item.getDetails()
169                             : null}
170                     </Grid>
171                 </Grid >;
172             }
173         }
174     )
175 );