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

100% Statements 28/28
93.54% Branches 29/31
100% Functions 8/8
100% Lines 23/23

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                                                              94x 94x 94x 94x 94x   94x 37x 27x 27x   10x 10x 10x   9x     1x 1x                 10x       94x     94x 43x     94x       1x   94x 31x                                                        
import { Button } from "@wintermuted/ui-react";
import { Feature, LineString } from "geojson";
import React, { useEffect, useState } from "react";
import {
  getWalkingRoute,
  WalkingRoute,
  WalkingStop,
} from "../../../api/mapbox/walking";
import { getNormalizedDistanceString } from "../util/turfUtils";
import { WalkingDistance } from "./common/WalkingDistance";
 
interface Props {
  currentLocation: number[];
  stop: WalkingStop;
  enabled: boolean;
  title?: string;
  onRouteChanged: (feature: Feature<LineString>) => void | (() => void);
}
 
type State =
  | { key: string; status: "loading" }
  | { key: string; status: "error"; message: string }
  | { key: string; status: "ready"; route: WalkingRoute };
 
export default function WalkingAccess({
  currentLocation,
  stop,
  enabled,
  onRouteChanged,
  title = "Walk to boarding stop",
}: Props) {
  const lng = currentLocation?.[0];
  const lat = currentLocation?.[1];
  const key = JSON.stringify([lng, lat, stop.locid, stop.lng, stop.lat]);
  const [state, setState] = useState<State>({ key, status: "loading" });
  const [retry, setRetry] = useState(0);
 
  useEffect(() => {
    if (!enabled) {
      setState({ key, status: "loading" });
      return;
    }
    const request = new AbortController();
    setState({ key, status: "loading" });
    getWalkingRoute([lng, lat], stop, request.signal)
      .then((route) => {
        if (!request.signal.aborted) setState({ key, route, status: "ready" });
      })
      .catch((error) => {
        Eif (!request.signal.aborted)
          setState({
            key,
            message:
              error instanceof Error
                ? error.message
                : "Could not load the walking route.",
            status: "error",
          });
      });
    return () => request.abort();
  }, [enabled, lng, lat, stop.locid, stop.lng, stop.lat, key, retry]);
 
  const route =
    enabled && state.key === key && state.status === "ready"
      ? state.route
      : undefined;
  useEffect(() => {
    if (route?.status === "ready") return onRouteChanged(route.feature);
  }, [route, onRouteChanged]);
 
  const straightLine = [lng, lat, stop.lng, stop.lat].every(Number.isFinite)
    ? getNormalizedDistanceString([lng, lat], [stop.lng, stop.lat])
    : "Distance unavailable";
  function refreshWalkingRoute() {
    setRetry((value) => value + 1);
  }
  if (!enabled) return null;
  return (
    <section className="transit-walking-access" aria-label={title}>
      <h4>{title}</h4>
      <WalkingDistance
        enabled
        estimate={route}
        loading={state.key !== key || state.status === "loading"}
        straightLine={straightLine}
      />
      {state.key === key && state.status === "error" && (
        <p role="alert">Could not load walking directions. {state.message}</p>
      )}
      {route?.status === "uncertain" && (
        <p role="status">
          Mapbox could not match this location reliably to nearby walking paths.
        </p>
      )}
      {route?.status === "ready" && (
        <p>The dashed green line shows the estimated walking path.</p>
      )}
      <Button variant="link" onClick={refreshWalkingRoute}>
        {state.status === "error"
          ? "Retry walking directions"
          : "Refresh walking directions"}
      </Button>
    </section>
  );
}