remove imports from panel details
[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 EmptyState from '../../components/empty-state/empty-state';
17 import { RootState } from '../../store/store';
18 import actions from "../../store/details-panel/details-panel-action";
19 import { ProjectResource } from '../../models/project';
20 import { CollectionResource } from '../../models/collection';
21 import IconBase, { IconTypes } from '../../components/icon/icon';
22 import { ProcessResource } from '../../models/process';
23 import DetailsPanelFactory from '../../components/details-panel-factory/details-panel-factory';
24
25 export interface DetailsPanelDataProps {
26     onCloseDrawer: () => void;
27     isOpened: boolean;
28     icon: IconTypes;
29     title: string;
30     details: React.ReactElement<any>;
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, icon, title, details } = 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={icon} />
58                             <Typography variant="title">
59                                 {title}
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                             {details}
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 type DetailsPanelResource = ProjectResource | CollectionResource | ProcessResource;
117
118 const getEmptyState = () => {
119     return <EmptyState icon={ IconTypes.ANNOUNCEMENT } 
120         message='Select a file or folder to view its details.' />;
121 };
122
123 const getItem = (res: DetailsPanelResource) => {
124     const item = DetailsPanelFactory.createItem(res);
125     return {
126         title: item.getTitle(),
127         icon: item.getIcon(),
128         details: item.buildDetails() 
129     };    
130 };
131
132 const getDefaultItem = () => {
133     return {
134         title: 'Projects',
135         icon: IconTypes.FOLDER,
136         details: getEmptyState()
137     };
138 };
139
140 const mapStateToProps = ({ detailsPanel }: RootState) => {
141     const { isOpened, item } = detailsPanel;
142     const newItem = item ? getItem(item as DetailsPanelResource) : getDefaultItem();
143     return {
144         isOpened,
145         ...newItem
146     };
147 };
148
149 const mapDispatchToProps = (dispatch: Dispatch) => ({
150     onCloseDrawer: () => {
151         dispatch(actions.TOGGLE_DETAILS_PANEL());
152     }
153 });
154
155 const DetailsPanelContainer = connect(mapStateToProps, mapDispatchToProps)(DetailsPanel);
156
157 export default withStyles(styles)(DetailsPanelContainer);