Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / views-components / main-content-bar / main-content-bar.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6
7 import { Toolbar, StyleRulesCallback, IconButton, Tooltip, Grid, WithStyles, withStyles } from "@material-ui/core";
8 import { DetailsIcon } from "components/icon/icon";
9 import { Breadcrumbs } from "views-components/breadcrumbs/breadcrumbs";
10 import { connect } from 'react-redux';
11 import { RootState } from 'store/store';
12 import * as Routes from 'routes/routes';
13 import { toggleDetailsPanel } from 'store/details-panel/details-panel-action';
14 import RefreshButton from "components/refresh-button/refresh-button";
15 import { loadSidePanelTreeProjects } from "store/side-panel-tree/side-panel-tree-actions";
16 import { Dispatch } from "redux";
17
18 type CssRules = 'mainBar' | 'breadcrumbContainer' | 'infoTooltip';
19
20 const styles: StyleRulesCallback<CssRules> = theme => ({
21     mainBar: {
22         flexWrap: 'nowrap',
23     },
24     breadcrumbContainer: {
25         overflow: 'hidden',
26     },
27     infoTooltip: {
28         marginTop: '-10px',
29         marginLeft: '10px',
30     }
31 });
32
33 interface MainContentBarProps {
34     onRefreshPage: () => void;
35     onDetailsPanelToggle: () => void;
36     buttonVisible: boolean;
37 }
38
39 const isButtonVisible = ({ router }: RootState) => {
40     const pathname = router.location ? router.location.pathname : '';
41     return Routes.matchCollectionsContentAddressRoute(pathname) ||
42         Routes.matchPublicFavoritesRoute(pathname) ||
43         Routes.matchGroupDetailsRoute(pathname) ||
44         Routes.matchGroupsRoute(pathname) ||
45         Routes.matchUsersRoute(pathname) ||
46         Routes.matchSearchResultsRoute(pathname) ||
47         Routes.matchSharedWithMeRoute(pathname) ||
48         Routes.matchProcessRoute(pathname) ||
49         Routes.matchCollectionRoute(pathname) ||
50         Routes.matchProjectRoute(pathname) ||
51         Routes.matchAllProcessesRoute(pathname) ||
52         Routes.matchTrashRoute(pathname) ||
53         Routes.matchFavoritesRoute(pathname);
54 };
55
56 const mapStateToProps = (state: RootState) => ({
57     buttonVisible: isButtonVisible(state),
58     projectUuid: state.detailsPanel.resourceUuid,
59 });
60
61 const mapDispatchToProps = () => (dispatch: Dispatch) => ({
62     onDetailsPanelToggle: () => dispatch<any>(toggleDetailsPanel()),
63     onRefreshButtonClick: (id) => {
64         dispatch<any>(loadSidePanelTreeProjects(id));
65     }
66 });
67
68 export const MainContentBar = connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(
69     (props: MainContentBarProps & WithStyles<CssRules> & any) =>
70         <Toolbar><Grid container className={props.classes.mainBar}>
71             <Grid container item xs alignItems="center" className={props.classes.breadcrumbContainer}>
72                 <Breadcrumbs />
73             </Grid>
74             <Grid item>
75                 <RefreshButton onClick={() => {
76                     props.onRefreshButtonClick(props.projectUuid);
77                 }} />
78             </Grid>
79             <Grid item>
80                 {props.buttonVisible && <Tooltip title="Additional Info">
81                     <IconButton data-cy="additional-info-icon"
82                         color="inherit"
83                         className={props.classes.infoTooltip}
84                         onClick={props.onDetailsPanelToggle}>
85                         <DetailsIcon />
86                     </IconButton>
87                 </Tooltip>}
88             </Grid>
89         </Grid></Toolbar>
90 ));