Merge branch '21448-menu-reorder' into 21224-project-details
[arvados.git] / services / workbench2 / 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     projectUuid: string;
38 }
39
40 const isButtonVisible = ({ router }: RootState) => {
41     const pathname = router.location ? router.location.pathname : '';
42     return Routes.matchCollectionsContentAddressRoute(pathname) ||
43         Routes.matchPublicFavoritesRoute(pathname) ||
44         Routes.matchGroupDetailsRoute(pathname) ||
45         Routes.matchGroupsRoute(pathname) ||
46         Routes.matchUsersRoute(pathname) ||
47         Routes.matchSearchResultsRoute(pathname) ||
48         Routes.matchSharedWithMeRoute(pathname) ||
49         Routes.matchProcessRoute(pathname) ||
50         Routes.matchCollectionRoute(pathname) ||
51         Routes.matchProjectRoute(pathname) ||
52         Routes.matchAllProcessesRoute(pathname) ||
53         Routes.matchTrashRoute(pathname) ||
54         Routes.matchFavoritesRoute(pathname);
55 };
56
57 const mapStateToProps = (state: RootState) => {
58     const currentRoute = state.router.location?.pathname.split('/') || [];
59     const projectUuid = currentRoute[currentRoute.length - 1];
60
61     return {
62         buttonVisible: isButtonVisible(state),
63         projectUuid,
64     }
65 };
66
67 const mapDispatchToProps = () => (dispatch: Dispatch) => ({
68     onDetailsPanelToggle: (uuid: string) => dispatch<any>(toggleDetailsPanel(uuid)),
69     onRefreshButtonClick: (id) => {
70         dispatch<any>(loadSidePanelTreeProjects(id));
71     }
72 });
73
74 export const MainContentBar = connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(
75     (props: MainContentBarProps & WithStyles<CssRules> & any) =>
76         <Toolbar><Grid container className={props.classes.mainBar}>
77             <Grid container item xs alignItems="center" className={props.classes.breadcrumbContainer}>
78                 <Breadcrumbs />
79             </Grid>
80             <Grid item>
81                 <RefreshButton onClick={() => {
82                     props.onRefreshButtonClick(props.projectUuid);
83                 }} />
84             </Grid>
85             <Grid item>
86                 {props.buttonVisible && <Tooltip title="Additional Info" disableFocusListener>
87                     <IconButton data-cy="additional-info-icon"
88                         color="inherit"
89                         className={props.classes.infoTooltip}
90                         onClick={()=>props.onDetailsPanelToggle(props.projectUuid)}>
91                         <DetailsIcon />
92                     </IconButton>
93                 </Tooltip>}
94             </Grid>
95         </Grid></Toolbar>
96 ));