Header.tsx

  1import React from 'react';
  2import { Link, useLocation } from 'react-router-dom';
  3
  4import AppBar from '@material-ui/core/AppBar';
  5import Tab, { TabProps } from '@material-ui/core/Tab';
  6import Tabs from '@material-ui/core/Tabs';
  7import Toolbar from '@material-ui/core/Toolbar';
  8import Tooltip from '@material-ui/core/Tooltip/Tooltip';
  9import { makeStyles } from '@material-ui/core/styles';
 10
 11import CurrentIdentity from '../CurrentIdentity/CurrentIdentity';
 12import { LightSwitch } from '../Themer';
 13
 14const useStyles = makeStyles((theme) => ({
 15  offset: {
 16    ...theme.mixins.toolbar,
 17  },
 18  filler: {
 19    flexGrow: 1,
 20  },
 21  appBar: {
 22    backgroundColor: theme.palette.primary.dark,
 23    color: theme.palette.primary.contrastText,
 24  },
 25  appTitle: {
 26    ...theme.typography.h6,
 27    color: theme.palette.primary.contrastText,
 28    textDecoration: 'none',
 29    display: 'flex',
 30    alignItems: 'center',
 31  },
 32  lightSwitch: {
 33    padding: '0 20px',
 34  },
 35  logo: {
 36    height: '42px',
 37    marginRight: theme.spacing(2),
 38  },
 39}));
 40
 41function a11yProps(index: any) {
 42  return {
 43    id: `nav-tab-${index}`,
 44    'aria-controls': `nav-tabpanel-${index}`,
 45  };
 46}
 47
 48const DisabledTabWithTooltip = (props: TabProps) => {
 49  /*The span elements around disabled tabs are needed, as the tooltip
 50   * won't be triggered by disabled elements.
 51   * See: https://material-ui.com/components/tooltips/#disabled-elements
 52   * This must be done in a wrapper component, otherwise the TabS component
 53   * cannot pass it styles down to the Tab component. Resulting in (console)
 54   * warnings. This wrapper acceps the passed down TabProps and pass it around
 55   * the span element to the Tab component.
 56   */
 57  const msg = `This feature doesn't exist yet. Come help us build it.`;
 58  return (
 59    <Tooltip title={msg}>
 60      <span>
 61        <Tab disabled {...props} />
 62      </span>
 63    </Tooltip>
 64  );
 65};
 66
 67function Header() {
 68  const classes = useStyles();
 69  const location = useLocation();
 70
 71  // Prevents error of invalid tab selection in <Tabs>
 72  function highlightTab() {
 73    switch (location.pathname) {
 74      case '/':
 75        return '/';
 76      case '/code':
 77        return '/code';
 78      case '/pulls':
 79        return '/pulls';
 80      case '/settings':
 81        return '/settings';
 82      default:
 83        // using false as value for tabs will result in no selected tab
 84        return false;
 85    }
 86  }
 87
 88  return (
 89    <>
 90      <AppBar position="fixed" className={classes.appBar}>
 91        <Toolbar>
 92          <Link to="/" className={classes.appTitle}>
 93            <img src="/logo.svg" className={classes.logo} alt="git-bug logo" />
 94            git-bug
 95          </Link>
 96          <div className={classes.filler} />
 97          <div className={classes.lightSwitch}>
 98            <LightSwitch />
 99          </div>
100          <CurrentIdentity />
101        </Toolbar>
102      </AppBar>
103      <div className={classes.offset} />
104      <Tabs centered value={highlightTab()} aria-label="nav tabs">
105        <DisabledTabWithTooltip label="Code" value="/code" {...a11yProps(1)} />
106        <Tab label="Bugs" value="/" component={Link} to="/" {...a11yProps(2)} />
107        <DisabledTabWithTooltip
108          label="Pull Requests"
109          value="/pulls"
110          {...a11yProps(3)}
111        />
112        <DisabledTabWithTooltip
113          label="Settings"
114          value="/settings"
115          {...a11yProps(4)}
116        />
117      </Tabs>
118    </>
119  );
120}
121
122export default Header;