All files / applications/go-by-transit/src/view/nearby/components NearbyRouteInfo.tsx

88.46% Statements 46/52
85.71% Branches 24/28
80.95% Functions 17/21
90.47% Lines 38/42

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                                        65x 65x 65x 65x 65x 65x 65x 65x   65x 20x 20x 20x 20x 20x 20x 20x 19x 19x 21x     19x   1x   20x     65x 37x 15x 15x     65x         1x 1x 1x 1x                     1x                                 39x         22x           59x       1x 1x 1x                              
import { Button } from "@wintermuted/ui-react";
import React, { useEffect, useRef, useState } from "react";
import { Link, useLocation } from "react-router-dom";
import FontAwesome from "react-fontawesome";
import { getRouteById } from "../../../api/trimet/routeConfig";
import { TrimetRoute } from "../../../api/trimet/interfaces/routes";
import useStopHighlight from "../util/useStopHighlight";
import { stopDetailUrl } from "../util/stopNavigation";
 
export default function NearbyRouteInfo({
  routeId,
  onClose,
  onMapChanged,
  onStopHighlightChanged,
}: {
  routeId: number;
  onClose: () => void;
  onMapChanged?: (route: TrimetRoute, onError: () => void) => () => void;
  onStopHighlightChanged?: (stopId?: number) => void;
}) {
  const [route, setRoute] = useState<TrimetRoute>();
  const [error, setError] = useState(false);
  const [mapError, setMapError] = useState(false);
  const [retry, setRetry] = useState(0);
  const [mapRetry, setMapRetry] = useState(0);
  const closeButton = useRef<HTMLButtonElement>(null);
  const location = useLocation();
  const { clearHighlight, focusStop, hoverStop } = useStopHighlight(onStopHighlightChanged);
 
  useEffect(() => {
    const sidebar = closeButton.current?.closest(".transit-nearby-results");
    if (sidebar) sidebar.scrollTop = 0;
    closeButton.current?.focus({ preventScroll: true });
    let active = true;
    setError(false);
    setRoute(undefined);
    getRouteById(routeId).then((data) => {
      const found = data?.route?.find((item) => item.route === routeId);
      Iif (!found || !Array.isArray(found.dir) || !found.dir.length ||
          !found.dir.every((direction) => Array.isArray(direction.stop))) {
        throw new Error("Invalid route details");
      }
      if (active) setRoute(found);
    }).catch(() => {
      Eif (active) setError(true);
    });
    return () => { active = false; };
  }, [routeId, retry]);
 
  useEffect(() => {
    if (!route || !onMapChanged) return;
    setMapError(false);
    return onMapChanged(route, () => setMapError(true));
  }, [route, onMapChanged, mapRetry]);
 
  return (
    <section
      className="transit-destination-panel transit-route-info"
      aria-label={`Route ${routeId} details`}
      onKeyDown={(event) => {
        Eif (event.key === "Escape") {
          event.preventDefault();
          event.stopPropagation();
          onClose();
        }
      }}
    >
      <div className="transit-destination-panel-header">
        <h4>Route {routeId}{route?.desc ? `: ${route.desc}` : ""}</h4>
        <Button ref={closeButton} variant="ghost" onClick={onClose}>Close</Button>
      </div>
      {error ? (
        <div role="alert">
          <p>Route details could not be loaded.</p>
          <Button variant="secondary" onClick={() => setRetry((value) => value + 1)}>Retry</Button>
        </div>
      ) : !route ? (
        <p role="status">Loading route details...</p>
      ) : (
        <>
          <Link to={`/lines/${routeId}`}>View full route details, schedules and alerts</Link>
          {mapError && (
            <div role="alert">
              <p>Some route lines could not be loaded. Stop locations are still shown.</p>
              <Button variant="secondary" onClick={() => {
                clearHighlight();
                setMapRetry((value) => value + 1);
              }}>Retry map</Button>
            </div>
          )}
          {route.dir.map((direction) => (
            <details
              key={direction.dir}
              className="transit-simple-route-direction"
              open
              onToggle={(event) => {
                if (!event.currentTarget.open) clearHighlight();
              }}
            >
              <summary><FontAwesome name="caret-right" aria-hidden="true" />{direction.desc}</summary>
              <ol className="transit-route-info-stops">
                {direction.stop.map((stop) => (
                  <li key={`${stop.locid}-${stop.seq}`}>
                    <Link
                      to={stopDetailUrl(stop.locid, `${location.pathname}${location.search}${location.hash}`)}
                      data-stop-id={stop.locid}
                      onMouseEnter={() => hoverStop(stop.locid)}
                      onMouseLeave={() => hoverStop()}
                      onFocus={() => focusStop(stop.locid)}
                      onBlur={() => focusStop()}
                    >
                      <span>{stop.desc}</span><small>{stop.locid}</small>
                    </Link>
                  </li>
                ))}
              </ol>
            </details>
          ))}
        </>
      )}
    </section>
  );
}