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

100% Statements 19/19
66.66% Branches 12/18
100% Functions 10/10
100% Lines 19/19

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                            11x               52x 52x 52x   52x                                                       44x             52x                               92x                               152x 152x 152x                 152x             152x               1x 1x 6x 2x                                                                     4x 4x                    
import React from "react";
import { Badge, ListGroup, ListItem } from "@wintermuted/ui-react";
import { faBus, faCableCar, faTrain, faTrainTram } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import FontAwesome from "react-fontawesome";
import { Link } from "react-router-dom";
import { PLANNER_BUS_COLOR } from "../../../api/trimet/plannerRouteColors";
import { stopMode } from "../../../api/trimet/stopCatalog";
import { TrimetRoute } from "../../../api/trimet/interfaces/types";
import { getNormalizedDistanceString } from "../util/turfUtils";
import { getTimeUntilArrival } from "../util/timeUtils";
import { WalkingDistance } from "./common/WalkingDistance";
import { getArrivalTime, getDisplayRoutes } from "./nearbyRouteArrivals";
 
const SERVICE_ICONS = {
  Bus: faBus,
  Rail: faTrain,
  Streetcar: faTrainTram,
  "Aerial tram": faCableCar
};
 
function ExploreRouteBadge({ route }: { route: TrimetRoute }) {
  const mode = stopMode(route);
  const isFx = /^\s*FX\s*\d+\b/i.test(route.desc);
  const color = mode === "Bus" && !isFx ? PLANNER_BUS_COLOR : undefined;
 
  return (
    <Badge className="transit-explore-route-badge" style={color ? {
      borderColor: color,
      backgroundColor: `color-mix(in srgb, ${color} 12%, var(--wm-color-surface-solid))`
    } : undefined}>
      {mode && <FontAwesomeIcon icon={SERVICE_ICONS[mode]} aria-hidden="true" />}
      <span className="wm-sr-only">{isFx ? "FX bus" : mode ? `${mode} route` : "Route"} </span>
      <strong>{route.route}</strong>
    </Badge>
  );
}
interface Props {
  displayRoutes: ReturnType<typeof getDisplayRoutes>;
  currentLocation: number[];
  walkingEnabled: boolean;
  walkingLoading: boolean;
  focusStop: (stopId?: number) => void;
  hoverStop: (stopId?: number) => void;
  clearHighlight: () => void;
  handleDisclosureToggle: React.ReactEventHandler<HTMLDetailsElement>;
  infoButton: React.RefObject<HTMLAnchorElement | null>;
  routeInfoSearch: (routeId: number) => string;
}
 
export default function SimpleRouteGroups({
  displayRoutes, currentLocation, walkingEnabled, walkingLoading,
  focusStop, hoverStop, clearHighlight, handleDisclosureToggle, infoButton, routeInfoSearch
}: Props) {
  return (
    <div
      className="transit-route-groups"
      role="group"
      aria-label="Nearby route arrivals"
    >
      {displayRoutes.map(({ route, routeName, directions }) => (
        <div key={route.route} className="transit-simple-route">
          <details
            className="transit-simple-route-group"
            onToggle={handleDisclosureToggle}
            aria-labelledby={`nearby-route-${route.route}`}
            open
          >
            <summary
              id={`nearby-route-${route.route}`}
              className="transit-simple-route-heading"
            >
              <FontAwesome name="caret-right" aria-hidden="true" />
              <ExploreRouteBadge route={route} />
              <span title={routeName}>{routeName}</span>
            </summary>
            {directions.map((direction) => (
              <details
                key={direction.key}
                className="transit-simple-route-direction"
                onToggle={handleDisclosureToggle}
                aria-labelledby={`nearby-direction-${direction.key}`}
                open
              >
                <summary id={`nearby-direction-${direction.key}`}>
                  <FontAwesome name="caret-right" aria-hidden="true" />
                  {direction.direction.desc}
                </summary>
                <ListGroup
                  aria-label={`Route ${route.route}, ${direction.direction.desc} stops`}
                >
                  {direction.stops.map(
                    ({ stop, arrivals, distanceMiles, walkingEstimate }) => {
                      const arrival = arrivals[0];
                      const arrivalTime = getArrivalTime(arrival);
                      const arrivalLabel = !arrival
                        ? "No arrivals reported"
                        : arrivalTime == null
                          ? "Time unavailable"
                          : getTimeUntilArrival(
                              arrival.estimated,
                              arrival.scheduled
                            );
                      const distanceLabel =
                        distanceMiles == null
                          ? "Distance unavailable"
                          : getNormalizedDistanceString(
                              currentLocation,
                              [stop.lng, stop.lat]
                            );
 
                      return (
                        <ListItem
                          key={stop.locid}
                          className="transit-simple-route-item"
                        >
                          <Link
                            className="transit-simple-route-stop"
                            data-stop-id={stop.locid}
                            onBlur={() => focusStop()}
                            onFocus={() => focusStop(stop.locid)}
                            onMouseEnter={() => hoverStop(stop.locid)}
                            onMouseLeave={() => hoverStop()}
                            to={`/nearby/simple-routes/${route.route}?stop=${stop.locid}&direction=${direction.direction.dir}`}
                          >
                            <span className="transit-simple-route-stop-name">
                              {stop.desc}
                              <small className="transit-simple-route-stop-id">Stop {stop.locid}</small>
                            </span>
                            <span className="transit-simple-route-arrivals">
                              <strong>{arrivalLabel}</strong>
                              <WalkingDistance
                                enabled={walkingEnabled}
                                loading={walkingLoading}
                                estimate={walkingEstimate}
                                straightLine={distanceLabel}
                              />
                            </span>
                            <span className="transit-simple-route-action">
                              View arrivals{" "}
                              <span aria-hidden="true">›</span>
                            </span>
                          </Link>
                        </ListItem>
                      );
                    }
                  )}
                </ListGroup>
              </details>
            ))}
          </details>
          <Link
            className="transit-simple-route-details-link"
            to={{ search: routeInfoSearch(route.route) }}
            aria-label={`View Route ${route.route} details`}
            title={`Route ${route.route} details`}
            onClick={(event) => {
              infoButton.current = event.currentTarget;
              clearHighlight();
            }}
          >
            <FontAwesome name="info-circle" aria-hidden="true" />
          </Link>
        </div>
      ))}
    </div>
  );
}