improve header
[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 Drawer from '@material-ui/core/Drawer';
7 import IconButton from "@material-ui/core/IconButton";
8 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
9 import { ArvadosTheme } from '../../common/custom-theme';
10 import Tabs from '@material-ui/core/Tabs';
11 import Tab from '@material-ui/core/Tab';
12 import Typography from '@material-ui/core/Typography';
13 import Grid from '@material-ui/core/Grid';
14 import * as classnames from "classnames";
15 import { connect, Dispatch } from 'react-redux';
16 import { RootState } from '../../store/store';
17 import actions from "../../store/details-panel/details-panel-action";
18 import { ProjectResource } from '../../models/project';
19 import { CollectionResource } from '../../models/collection';
20 import IconBase, { IconTypes } from '../../components/icon/icon';
21 import { ProcessResource } from '../../models/process';
22 import DetailsPanelFactory from '../../components/details-panel-factory/details-panel-factory';
23 import AbstractItem from '../../components/details-panel-factory/items/abstract-item';
24 import { ResourceKind } from '../../models/resource';
25 import { EmptyResource } from '../../models/empty';
26
27 export interface DetailsPanelDataProps {
28     onCloseDrawer: () => void;
29     isOpened: boolean;
30     item: AbstractItem;
31 }
32
33 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
34
35 class DetailsPanel extends React.Component<DetailsPanelProps, {}> {
36     state = {
37         tabsValue: 0
38     };
39
40     handleChange = (event: any, value: boolean) => {
41         this.setState({ tabsValue: value });
42     }
43
44     renderTabContainer = (children: React.ReactElement<any>) =>
45         <Typography className={this.props.classes.tabContainer} component="div">
46             {children}
47         </Typography>
48
49     render() {
50         const { classes, onCloseDrawer, isOpened, item } = this.props;
51         const { tabsValue } = this.state;
52         return (
53             <Typography component="div" className={classnames([classes.container, { [classes.opened]: isOpened }])}>
54                 <Drawer variant="permanent" anchor="right" classes={{ paper: classes.drawerPaper }}>
55                     <Typography component="div" className={classes.headerContainer}>
56                         <Grid container wrap="nowrap" alignItems='center' justify='space-around'>
57                             <Grid item>
58                                 <IconBase className={classes.headerIcon} icon={item.getIcon()} />
59                             </Grid>
60                             <Grid item xs={8}>
61                                 <Typography variant="title">
62                                     {item.getTitle()}
63                                 </Typography>
64                             </Grid>
65                             <Grid item>
66                                 <IconButton color="inherit" onClick={onCloseDrawer}>
67                                     <IconBase icon={IconTypes.CLOSE} />
68                                 </IconButton>
69                             </Grid>
70                         </Grid>
71                     </Typography>
72                     <Tabs value={tabsValue} onChange={this.handleChange}>
73                         <Tab disableRipple label="Details" />
74                         <Tab disableRipple label="Activity" disabled />
75                     </Tabs>
76                     {tabsValue === 0 && this.renderTabContainer(
77                         <Grid container direction="column">
78                             {item.buildDetails()}
79                         </Grid>
80                     )}
81                     {tabsValue === 1 && this.renderTabContainer(
82                         <Grid container direction="column" />
83                     )}
84                 </Drawer>
85             </Typography>
86         );
87     }
88
89 }
90
91 type CssRules = 'drawerPaper' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
92
93 const drawerWidth = 320;
94 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
95     container: {
96         width: 0,
97         position: 'relative',
98         height: 'auto',
99         transition: 'width 0.5s ease',
100         '&$opened': {
101             width: drawerWidth
102         }
103     },
104     opened: {},
105     drawerPaper: {
106         position: 'relative',
107         width: drawerWidth
108     },
109     headerContainer: {
110         color: theme.palette.grey["600"],
111         margin: `${theme.spacing.unit}px 0`,
112         textAlign: 'center'
113     },
114     headerIcon: {
115         fontSize: "34px"
116     },
117     tabContainer: {
118         padding: theme.spacing.unit * 3
119     }
120 });
121
122 // TODO: move to models
123 export type DetailsPanelResource = ProjectResource | CollectionResource | ProcessResource | EmptyResource;
124
125 const getItem = (res: DetailsPanelResource) => {
126     return DetailsPanelFactory.createItem(res);
127 };
128
129 const getDefaultItem = () => {
130     return DetailsPanelFactory.createItem({ kind: ResourceKind.UNKNOWN, name: 'Projects'});
131 };
132
133 const mapStateToProps = ({ detailsPanel }: RootState) => {
134     const { isOpened, item } = detailsPanel;
135     return {
136         isOpened,
137         item: item ? getItem(item as DetailsPanelResource) : getDefaultItem()
138     };
139 };
140
141 const mapDispatchToProps = (dispatch: Dispatch) => ({
142     onCloseDrawer: () => {
143         dispatch(actions.TOGGLE_DETAILS_PANEL());
144     }
145 });
146
147 const DetailsPanelContainer = connect(mapStateToProps, mapDispatchToProps)(DetailsPanel);
148
149 export default withStyles(styles)(DetailsPanelContainer);