All files / applications/go-by-transit/src/view/mainNav/components MainNavigationMenu.tsx

100% Statements 24/24
98.03% Branches 50/51
100% Functions 6/6
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                            30x 30x 30x 30x 30x 30x   30x 30x   30x       30x   30x     4x       4x         3x     3x 1x 1x 1x   2x 2x 2x       30x                                                                                                                                                                                                                                                            
import { Button, Nav } from "@wintermuted/ui-react";
import React, { useEffect, useId, useRef, useState } from "react";
import FontAwesome from "react-fontawesome";
import { Link, useLocation } from "react-router-dom";
import TimeDisplayMenu from "../../../component/time/TimeDisplayMenu";
import GlobalPlacesSearch from "./GlobalPlacesSearch";
import "./MainNavigationMenu.scss";
 
interface Props {
  numberOfBookmarks: number;
  timeOfLastLoad: string;
}
 
export default function MainNavigationMenu(props: Props) {
  const { pathname } = useLocation();
  const [menuOpen, setMenuOpen] = useState(false);
  const menuId = useId();
  const menuButton = useRef<HTMLButtonElement>(null);
  const homeActive = pathname === "/";
  const nearbyActive = pathname === "/nearby" || pathname.startsWith("/nearby/");
  const plannerActive =
    pathname === "/planner" || pathname.startsWith("/planner/");
  const linesActive = pathname === "/lines" || pathname.startsWith("/lines/");
  const stopsActive =
    pathname === "/stops" ||
    pathname === "/stop" ||
    pathname.startsWith("/stop/");
  const bookmarksActive =
    pathname === "/bookmarks" || pathname.startsWith("/bookmarks/");
 
  useEffect(() => setMenuOpen(false), [pathname]);
 
  function closeMenu() {
    setMenuOpen(false);
  }
 
  function toggleMenu() {
    setMenuOpen((open) => !open);
  }
 
  function handleKeyDown(event: React.KeyboardEvent<HTMLElement>) {
    const timeMenu =
      event.currentTarget.querySelector<HTMLDetailsElement>(
        ".transit-time-menu",
      );
    if (event.key === "Escape" && timeMenu?.open) {
      timeMenu.open = false;
      timeMenu.querySelector("summary")?.focus();
      return;
    }
    Eif (event.key === "Escape" && menuOpen) {
      closeMenu();
      menuButton.current?.focus();
    }
  }
 
  return (
    <header
      className="wm-docs-topbar transit-app-header"
      onKeyDown={handleKeyDown}
    >
      <div className="wm-docs-topbar-inner transit-header-inner">
        <Link
          to="/"
          className="wm-docs-topbar-brand transit-brand"
          onClick={closeMenu}
        >
          <FontAwesome
            name="bus"
            className="transit-brand-icon"
            aria-hidden="true"
          />
          <h1>Go By Transit</h1>
        </Link>
        <Button
          ref={menuButton}
          className="wm-docs-topbar-toggle transit-nav-toggle"
          variant="ghost"
          aria-label={menuOpen ? "Close navigation" : "Open navigation"}
          aria-expanded={menuOpen}
          aria-controls={menuId}
          onClick={toggleMenu}
        >
          <FontAwesome name={menuOpen ? "times" : "bars"} aria-hidden="true" />
          <span className="transit-nav-toggle-label">
            {menuOpen ? "Close" : "Menu"}
          </span>
        </Button>
        <div
          id={menuId}
          className={`transit-navigation-content${menuOpen ? " is-open" : ""}`}
        >
          <Nav
            className="wm-docs-topbar-nav transit-primary-nav"
            aria-label="Primary navigation"
          >
            <Link
              to="/"
              className={`wm-docs-topbar-link-devdocs${
                homeActive ? " is-active" : ""
              }`}
              aria-current={homeActive ? "page" : undefined}
              onClick={closeMenu}
            >
              <FontAwesome name="home" fixedWidth={true} aria-hidden="true" />
              Home
            </Link>
            <Link
              to="/nearby"
              className={`wm-docs-topbar-link-devdocs${
                nearbyActive ? " is-active" : ""
              }`}
              aria-current={nearbyActive ? "page" : undefined}
              onClick={closeMenu}
            >
              <FontAwesome name="location-arrow" fixedWidth aria-hidden="true" />
              Nearby
            </Link>
            <Link
              to="/planner"
              className={`wm-docs-topbar-link-devdocs${
                plannerActive ? " is-active" : ""
              }`}
              aria-current={plannerActive ? "page" : undefined}
              onClick={closeMenu}
            >
              <FontAwesome
                name="exchange"
                fixedWidth={true}
                aria-hidden="true"
              />
              Plan a trip
            </Link>
            <Link
              to="/stops"
              className={`wm-docs-topbar-link-devdocs${
                stopsActive ? " is-active" : ""
              }`}
              aria-current={stopsActive ? "page" : undefined}
              onClick={closeMenu}
            >
              <FontAwesome
                name="map-marker"
                fixedWidth={true}
                aria-hidden="true"
              />
              Stops
            </Link>
            <Link
              to="/lines/all"
              className={`wm-docs-topbar-link-devdocs${
                linesActive ? " is-active" : ""
              }`}
              aria-current={linesActive ? "page" : undefined}
              onClick={closeMenu}
            >
              <FontAwesome name="train" fixedWidth={true} aria-hidden="true" />
              Lines
            </Link>
            <Link
              to="/bookmarks"
              className={`wm-docs-topbar-link-devdocs${
                bookmarksActive ? " is-active" : ""
              }`}
              aria-current={bookmarksActive ? "page" : undefined}
              onClick={closeMenu}
            >
              <FontAwesome
                name="bookmark"
                fixedWidth={true}
                aria-hidden="true"
              />
              Bookmarks
            </Link>
          </Nav>
        </div>
        <GlobalPlacesSearch />
        <TimeDisplayMenu />
      </div>
    </header>
  );
}