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