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

100% Statements 29/29
85.71% Branches 18/21
100% Functions 12/12
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                                    65x 65x 65x 65x   65x 27x 27x 27x 27x   28x 25x 20x   7x 27x     65x 65x   44x   65x                       1x           69x 69x                 6x                            
import {
  Card,
  CardBody,
  CardText,
  Button
} from "@wintermuted/ui-react";
import React, { useEffect, useState } from "react";
import { getNearbyStops } from "../../../../api/trimet/stops";
import { StopLocation, TrimetRoute } from "../../../../api/trimet/interfaces/types";
 
interface StopInfoParams {
  stopLocation: StopLocation;
  arrivalRoutes: number[];
  selectedRoutes: number[];
  onToggleRoute: (route: number) => void;
}
 
function StopInfo({ stopLocation, arrivalRoutes, selectedRoutes, onToggleRoute }: StopInfoParams) {
  const [routes, setRoutes] = useState<TrimetRoute[]>();
  const [error, setError] = useState(false);
  const [retry, setRetry] = useState(0);
  const { id, lat, lng } = stopLocation;
 
  useEffect(() => {
    let active = true;
    setRoutes(undefined);
    setError(false);
    getNearbyStops({ coords: { latitude: lat, longitude: lng } }, 100)
      .then((data) => {
        const stop = data?.location?.find((location) => location.locid === id);
        if (!stop || !Array.isArray(stop.route)) throw new Error("Stop routes unavailable");
        Eif (active) setRoutes(stop.route);
      })
      .catch(() => { Eif (active) setError(true); });
    return () => { active = false; };
  }, [id, lat, lng, retry]);
 
  const routeNames = new Map<number, string>((routes || []).map((route) => [route.route, route.desc]));
  const routeNumbers = Array.from(new Set([
    ...Array.from(routeNames.keys()), ...arrivalRoutes, ...selectedRoutes
  ])).sort((a, b) => a - b);
 
  return (
    <Card>
      <CardBody>
        <CardText>
          <small style={{ color: "var(--wm-color-text-muted)" }}>
            StopID: {stopLocation.id}
          </small>
        </CardText>
        <div className="transit-destination-connections" role="group" aria-label="Filter departures by route">
          {error ? (
            <span role="alert">
              Routes could not be loaded.{" "}
              <Button variant="link" onClick={() => setRetry((value) => value + 1)}>Retry routes</Button>
            </span>
          ) : !routes ? (
            <span role="status">Loading routes...</span>
          ) : null}
          {routeNumbers.map((route) => {
            const name = routeNames.get(route);
            return (
              <button
                type="button"
                className="wm-badge transit-stop-route-filter"
                key={route}
                title={name || `Route ${route}`}
                aria-label={name ? `Route ${route}: ${name}` : `Route ${route}`}
                aria-pressed={selectedRoutes.includes(route)}
                aria-controls="stop-departures"
                onClick={() => onToggleRoute(route)}
              >
                {route}
              </button>
            );
          })}
          {routes && !routeNumbers.length && <span>No routes reported.</span>}
        </div>
      </CardBody>
    </Card>
  );
}
 
export default StopInfo;