Passed
Pull Request — main (#332)
by Alejandro
03:32
created

src/common/MenuLayout.tsx   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 81
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
wmc 3
eloc 71
mnd 3
bc 3
fnc 0
dl 0
loc 81
ccs 2
cts 18
cp 0.1111
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { FC, useEffect } from 'react';
2
import { Route, Switch } from 'react-router-dom';
3
import { useSwipeable } from 'react-swipeable';
4
import { faBars as burgerIcon } from '@fortawesome/free-solid-svg-icons';
5
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
6
import classNames from 'classnames';
7
import { withSelectedServer } from '../servers/helpers/withSelectedServer';
8
import { useToggle } from '../utils/helpers/hooks';
9
import { versionMatch } from '../utils/helpers/version';
10
import { isReachableServer } from '../servers/data';
11
import NotFound from './NotFound';
12
import { AsideMenuProps } from './AsideMenu';
13
import './MenuLayout.scss';
14
15
const MenuLayout = (
16
  TagsList: FC,
17
  ShortUrls: FC,
18
  AsideMenu: FC<AsideMenuProps>,
19
  CreateShortUrl: FC,
20
  ShortUrlVisits: FC,
21
  TagVisits: FC,
22
  ServerError: FC,
23
) => withSelectedServer(({ location, selectedServer }) => {
24
  const [ sidebarVisible, toggleSidebar, showSidebar, hideSidebar ] = useToggle();
25
26
  useEffect(() => hideSidebar(), [ location ]);
27
28 2
  if (!isReachableServer(selectedServer)) {
29
    return <ServerError />;
30
  }
31
32
  const addTagsVisitsRoute = versionMatch(selectedServer.version, { minVersion: '2.2.0' });
33
  const burgerClasses = classNames('menu-layout__burger-icon', {
34
    'menu-layout__burger-icon--active': sidebarVisible,
35
  });
36
  const swipeMenuIfNoModalExists = (callback: () => void) => (e: any) => {
37
    const swippedOnVisitsTable = (e.event.composedPath() as HTMLElement[]).some(
38
      ({ classList }) => classList?.contains('visits-table'),
39
    );
40
41 4
    if (swippedOnVisitsTable || document.querySelector('.modal')) {
42
      return;
43
    }
44
45
    callback();
46
  };
47
  const swipeableProps = useSwipeable({
48
    delta: 40,
49
    onSwipedLeft: swipeMenuIfNoModalExists(hideSidebar),
50
    onSwipedRight: swipeMenuIfNoModalExists(showSidebar),
51
  });
52
53
  return (
54
    <>
55
      <FontAwesomeIcon icon={burgerIcon} className={burgerClasses} onClick={toggleSidebar} />
56
57
      <div {...swipeableProps} className="menu-layout__swipeable">
58
        <div className="row menu-layout__swipeable-inner">
59
          <AsideMenu className="col-lg-2 col-md-3" selectedServer={selectedServer} showOnMobile={sidebarVisible} />
60
          <div className="col-lg-10 offset-lg-2 col-md-9 offset-md-3" onClick={() => hideSidebar()}>
61
            <div className="menu-layout__container">
62
              <Switch>
63
                <Route exact path="/server/:serverId/list-short-urls/:page" component={ShortUrls} />
64
                <Route exact path="/server/:serverId/create-short-url" component={CreateShortUrl} />
65
                <Route exact path="/server/:serverId/short-code/:shortCode/visits" component={ShortUrlVisits} />
66
                {addTagsVisitsRoute && <Route exact path="/server/:serverId/tag/:tag/visits" component={TagVisits} />}
67
                <Route exact path="/server/:serverId/manage-tags" component={TagsList} />
68
                <Route
69
                  render={() => <NotFound to={`/server/${selectedServer.id}/list-short-urls/1`}>List short URLs</NotFound>}
70
                />
71
              </Switch>
72
            </div>
73
          </div>
74
        </div>
75
      </div>
76
    </>
77
  );
78
}, ServerError);
79
80
export default MenuLayout;
81