Display file preview if file is an image
[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 { 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 { CloseIcon } from '~/components/icon/icon';
14 import { EmptyResource } from '~/models/empty';
15 import { Dispatch } from "redux";
16 import { ResourceKind } from "~/models/resource";
17 import { ProjectDetails } from "./project-details";
18 import { CollectionDetails } from "./collection-details";
19 import { ProcessDetails } from "./process-details";
20 import { EmptyDetails } from "./empty-details";
21 import { DetailsData } from "./details-data";
22 import { DetailsResource } from "~/models/details";
23 import { getResource } from '~/store/resources/resources';
24 import { ResourceData } from "~/store/resources-data/resources-data-reducer";
25 import { getResourceData } from "~/store/resources-data/resources-data";
26 import { toggleDetailsPanel, SLIDE_TIMEOUT } from '~/store/details-panel/details-panel-action';
27 import { FileDetails } from '~/views-components/details-panel/file-details';
28 import { getNode } from '~/models/tree';
29
30 type CssRules = 'root' | 'container' | 'opened' | 'headerContainer' | 'headerIcon' | 'tabContainer';
31
32 const DRAWER_WIDTH = 320;
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 EMPTY_RESOURCE: EmptyResource = { kind: undefined, name: 'Projects' };
64
65 const getItem = (res: DetailsResource, resourceData?: ResourceData): DetailsData => {
66     if ('kind' in res) {
67         switch (res.kind) {
68             case ResourceKind.PROJECT:
69                 return new ProjectDetails(res);
70             case ResourceKind.COLLECTION:
71                 return new CollectionDetails(res, resourceData);
72             case ResourceKind.PROCESS:
73                 return new ProcessDetails(res);
74             default:
75                 return new EmptyDetails(res);
76         }
77     } else {
78         return new FileDetails(res);
79     }
80 };
81
82 const mapStateToProps = ({ detailsPanel, resources, resourcesData, collectionPanelFiles }: RootState) => {
83     const resource = getResource(detailsPanel.resourceUuid)(resources) as DetailsResource | undefined;
84     const file = getNode(detailsPanel.resourceUuid)(collectionPanelFiles);
85     const resourceData = getResourceData(detailsPanel.resourceUuid)(resourcesData);
86     return {
87         isOpened: detailsPanel.isOpened,
88         item: getItem(resource || (file && file.value) || EMPTY_RESOURCE, resourceData)
89     };
90 };
91
92 const mapDispatchToProps = (dispatch: Dispatch) => ({
93     onCloseDrawer: () => {
94         dispatch<any>(toggleDetailsPanel());
95     }
96 });
97
98 export interface DetailsPanelDataProps {
99     onCloseDrawer: () => void;
100     isOpened: boolean;
101     item: DetailsData;
102 }
103
104 type DetailsPanelProps = DetailsPanelDataProps & WithStyles<CssRules>;
105
106 export const DetailsPanel = withStyles(styles)(
107     connect(mapStateToProps, mapDispatchToProps)(
108         class extends React.Component<DetailsPanelProps> {
109             state = {
110                 tabsValue: 0
111             };
112
113             handleChange = (event: any, value: boolean) => {
114                 this.setState({ tabsValue: value });
115             }
116
117             render() {
118                 const { classes, isOpened } = this.props;
119                 return (
120                     <Grid
121                         container
122                         direction="column"
123                         className={classnames([classes.root, { [classes.opened]: isOpened }])}>
124                         <Transition
125                             in={isOpened}
126                             timeout={SLIDE_TIMEOUT}
127                             unmountOnExit>
128                             {this.renderContent()}
129                         </Transition>
130                     </Grid>
131                 );
132             }
133
134             renderContent() {
135                 const { classes, onCloseDrawer, item } = this.props;
136                 const { tabsValue } = this.state;
137                 return <Grid
138                     container
139                     direction="column"
140                     item
141                     xs
142                     className={classes.container} >
143                     <Grid
144                         item
145                         className={classes.headerContainer}
146                         container
147                         alignItems='center'
148                         justify='space-around'
149                         wrap="nowrap">
150                         <Grid item xs={2}>
151                             {item.getIcon(classes.headerIcon)}
152                         </Grid>
153                         <Grid item xs={8}>
154                             <Tooltip title={item.getTitle()}>
155                                 <Typography variant="title" noWrap>
156                                     {item.getTitle()}
157                                 </Typography>
158                             </Tooltip>
159                         </Grid>
160                         <Grid item>
161                             <IconButton color="inherit" onClick={onCloseDrawer}>
162                                 <CloseIcon />
163                             </IconButton>
164                         </Grid>
165                     </Grid>
166                     <Grid item>
167                         <Tabs value={tabsValue} onChange={this.handleChange}>
168                             <Tab disableRipple label="Details" />
169                             <Tab disableRipple label="Activity" disabled />
170                         </Tabs>
171                     </Grid>
172                     <Grid item xs className={this.props.classes.tabContainer} >
173                         {tabsValue === 0
174                             ? item.getDetails()
175                             : null}
176                     </Grid>
177                 </Grid >;
178             }
179         }
180     )
181 );