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

95.83% Statements 23/24
93.33% Branches 28/30
100% Functions 11/11
95.83% Lines 23/24

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                                                    66x 68x                                                                             23x     23x           23x 15x     23x       23x 23x 2x 7x 7x   7x   7x 7x       23x                                                               64x     4x 1x 3x   2x       1x                                   66x                                        
import {
  Badge,
  Button,
  Card,
  CardFooter,
  CardHeader,
  ListGroup,
  ListItem
} from "@wintermuted/ui-react";
import React, { useEffect } from "react";
import { Link } from "react-router-dom";
import {
  StopData,
  StopLocation,
  TrimetRoute
} from "../../../api/trimet/interfaces/types";
import RouteIndicator from "../../../component/route/RouteIndicator";
import Loading from "../../loading/Loading";
import { getNormalizedDistanceString } from "../util/turfUtils";
import useStopHighlight from "../util/useStopHighlight";
import useWalkingEstimates from "../util/useWalkingEstimates";
import NearbySubNav from "./common/NearbySubNav";
import { WalkingDistance } from "./common/WalkingDistance";
import "./NearbyViewComponent.scss";
 
function getRouteDirections(route: TrimetRoute, stop: StopLocation) {
  return route.dir.map(direction => (
    <ListItem
      key={`${route.route}-${direction.dir}`}
      className="transit-nearby-stop-route"
    >
      <span className="transit-nearby-stop-route-description">
        <RouteIndicator routeId={route.route} /> {direction.desc}
      </span>
      <Link
        className="wm-btn wm-btn-primary transit-nearby-stop-route-go"
        to={`/nearby/directions?route=${route.route}&direction=${direction.dir}&from=${stop.locid}`}
        aria-label={`Go on route ${route.route}, ${direction.desc}, from ${stop.desc} (${stop.locid})`}
      >
        Go
      </Link>
    </ListItem>
  ));
}
 
export interface NearbyStopsProps {
  nearbyStops: StopData;
  radiusSize: number;
  handleRadiusSelectionChange: (e: any) => void;
  stopCount: number;
  routeCount: number;
  currentLocation: number[];
  onStopHighlightChanged?: (stopId?: number) => void;
  walkingEnabled?: boolean;
}
 
export default function NearbyStops({
  nearbyStops,
  radiusSize,
  handleRadiusSelectionChange,
  routeCount,
  stopCount,
  currentLocation,
  onStopHighlightChanged,
  walkingEnabled = false
}: NearbyStopsProps) {
  const { clearHighlight, focusStop, hoverStop } = useStopHighlight(
    onStopHighlightChanged
  );
  const walking = useWalkingEstimates(
    currentLocation,
    nearbyStops?.location ?? [],
    walkingEnabled
  );
 
  useEffect(() => {
    clearHighlight();
  }, [nearbyStops, radiusSize, walkingEnabled, walking.status, clearHighlight]);
 
  Iif (!nearbyStops) {
    return <Loading />;
  }
 
  const stopLocations = [...nearbyStops.location];
  if (walkingEnabled && walking.status === "ready") {
    stopLocations.sort((first, second) => {
      const firstEstimate = walking.estimates.get(first.locid);
      const secondEstimate = walking.estimates.get(second.locid);
      const firstDistance =
        firstEstimate?.status === "ready" ? firstEstimate.meters : Infinity;
      const secondDistance =
        secondEstimate?.status === "ready" ? secondEstimate.meters : Infinity;
      return firstDistance - secondDistance || first.locid - second.locid;
    });
  }
 
  return (
    <div className="stops-wrapper scrollarea">
      <NearbySubNav routeCount={routeCount} stopCount={stopCount} />
      <br />
      {!walkingEnabled && (
        <p>
          Enable walking estimates in Feature flags to sort by walking distance.
        </p>
      )}
      {walkingEnabled && walking.status === "loading" && (
        <p role="status">Calculating walking distances...</p>
      )}
      {walkingEnabled && walking.status === "error" && (
        <div role="alert">
          <p>
            Walking estimates unavailable. {walking.error} Stops are not ranked
            by walking distance.
          </p>
          <Button variant="secondary" onClick={walking.refresh}>
            Retry walking estimates
          </Button>
        </div>
      )}
      {walkingEnabled && walking.status === "ready" && (
        <>
          <p role="status">Closest walk first; unavailable estimates last.</p>
          <Button variant="link" onClick={walking.refresh}>
            Refresh walking estimates
          </Button>
        </>
      )}
      {stopLocations.map((stopLocation: StopLocation) => (
        <React.Fragment key={stopLocation.locid}>
          <Card
            data-stop-id={stopLocation.locid}
            onMouseEnter={() => hoverStop(stopLocation.locid)}
            onMouseLeave={() => hoverStop()}
            onFocusCapture={() => focusStop(stopLocation.locid)}
            onBlurCapture={event => {
              if (
                !(event.relatedTarget instanceof Node) ||
                !event.currentTarget.contains(event.relatedTarget)
              ) {
                focusStop();
              }
            }}
          >
            <CardHeader as="h6" className="transit-nearby-stop-heading">
              <Link
                className="transit-nearby-stop-heading-link"
                to={`/nearby/stops/${stopLocation.locid}`}
              >
                <span className="transit-nearby-stop-name">
                  {stopLocation.desc}
                </span>
                <span className="transit-nearby-stop-id">
                  <Badge>{stopLocation.locid}</Badge>
                </span>
              </Link>
            </CardHeader>
            <ListGroup>
              {stopLocation.route.map(r => getRouteDirections(r, stopLocation))}
            </ListGroup>
            <CardFooter style={{ color: "var(--wm-color-text-muted)" }}>
              <WalkingDistance
                enabled={walkingEnabled}
                loading={walking.status === "loading"}
                estimate={walking.estimates.get(stopLocation.locid)}
                straightLine={getNormalizedDistanceString(currentLocation, [
                  stopLocation.lng,
                  stopLocation.lat
                ])}
              />
            </CardFooter>
          </Card>
          <br />
        </React.Fragment>
      ))}
    </div>
  );
}