set export as default abstract class
[arvados.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 alignItems='center' justify='space-around'>
57                             <IconBase className={classes.headerIcon} icon={item.getIcon()} />
58                             <Typography variant="title">
59                                 {item.getTitle()}
60                             </Typography>
61                             <IconButton color="inherit" onClick={onCloseDrawer}>
62                                 <IconBase icon={IconTypes.CLOSE} />
63                             </IconButton>
64                         </Grid>
65                     </Typography>
66                     <Tabs value={tabsValue} onChange={this.handleChange}>
67                         <Tab disableRipple label="Details" />
68                         <Tab disableRipple label="Activity" disabled />
69                     </Tabs>
70                     {tabsValue === 0 && this.renderTabContainer(
71                         <Grid container direction="column">
72                             {item.buildDetails()}
73                         </Grid>
74                     )}
75                     {tabsValue === 1 && this.renderTabContainer(
76                         <Grid container direction="column" />
77                     )}
78                 </Drawer>
79             </Typography>
80         );
81     }
82
83 }
84
85 type CssRules = 'drawerPaper' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
86
87 const drawerWidth = 320;
88 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
89     container: {
90         width: 0,
91         position: 'relative',
92         height: 'auto',
93         transition: 'width 0.5s ease',
94         '&$opened': {
95             width: drawerWidth
96         }
97     },
98     opened: {},
99     drawerPaper: {
100         position: 'relative',
101         width: drawerWidth
102     },
103     headerContainer: {
104         color: theme.palette.grey["600"],
105         margin: `${theme.spacing.unit}px 0`
106     },
107     headerIcon: {
108         fontSize: "34px"
109     },
110     tabContainer: {
111         padding: theme.spacing.unit * 3
112     }
113 });
114
115 // TODO: move to models
116 export type DetailsPanelResource = ProjectResource | CollectionResource | ProcessResource | EmptyResource;
117
118 const getItem = (res: DetailsPanelResource) => {
119     return DetailsPanelFactory.createItem(res);
120 };
121
122 const getDefaultItem = () => {
123     return DetailsPanelFactory.createItem({ kind: ResourceKind.UNKNOWN, name: 'Projects'});
124 };
125
126 const mapStateToProps = ({ detailsPanel }: RootState) => {
127     const { isOpened, item } = detailsPanel;
128     return {
129         isOpened,
130         item: item ? getItem(item as DetailsPanelResource) : getDefaultItem()
131     };
132 };
133
134 const mapDispatchToProps = (dispatch: Dispatch) => ({
135     onCloseDrawer: () => {
136         dispatch(actions.TOGGLE_DETAILS_PANEL());
137     }
138 });
139
140 const DetailsPanelContainer = connect(mapStateToProps, mapDispatchToProps)(DetailsPanel);
141
142 export default withStyles(styles)(DetailsPanelContainer);