All files / applications/go-by-transit/src/view/tripPlanner TripPlannerView.tsx

92.3% Statements 12/13
93.18% Branches 41/44
85.71% Functions 6/7
91.66% Lines 11/12

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                          34x 34x 34x 136x 136x   34x       346x         346x   346x                   2280x                                     1x                                                                            
import React from "react";
import { Button } from "@wintermuted/ui-react";
import { categoryName, PLACE_CATEGORIES } from "@overworld/places/catalog";
import { SHORTCUTS } from "../../api/localstorage/bookmarkShortcuts";
import { useBookmarkShortcuts } from "@overworld/bookmarks/react";
import { useBookmarks } from "../../state/bookmarks/BookmarksProvider";
import type { TripPlannerProps } from "./TripPlannerModel";
import useTripPlannerState from "./useTripPlannerState";
import TripPlannerForm from "./TripPlannerForm";
import TripPlannerResults, { TripPlannerJourney } from "./TripPlannerResults";
import "./TripPlannerView.scss";
 
export function ConnectedTripPlannerView(props: TripPlannerProps) {
  const { stops: bookmarks } = useBookmarks();
  const { shortcuts, error } = useBookmarkShortcuts(bookmarks);
  const savedStops = SHORTCUTS.flatMap(({ key, kind, label }) => {
    const id = shortcuts[key];
    return kind === "stop" && id !== undefined && bookmarks[id] ? [{ id, label }] : [];
  });
  return <>{error && <p role="alert">{error}</p>}<TripPlannerView {...props} savedStops={savedStops} /></>;
}
 
export default function TripPlannerView({ inSidebar = false, directionsFooter, renderMapStatus, renderPlaceDestination, savedStops }: TripPlannerProps) {
  const state = useTripPlannerState({ inSidebar, directionsFooter });
  const {
    toInterestPlace, inJourney, planningContent, fromPlaceId, toPlaceId, placeCatalog,
    tripState, fromLinkedLocation, fromParam, toLinkedLocation, toParam, setTripRetry,
    response, historyUnavailable,
  } = state;
 
  return (
    <>
    {renderPlaceDestination?.(toInterestPlace)}
    <section
      className={`transit-planner${inSidebar ? " transit-planner--sidebar" : ""}`}
      aria-labelledby="transit-planner-title"
      hidden={inJourney}
      ref={planningContent}
    >
      <TripPlannerForm state={state} inSidebar={inSidebar} savedStops={savedStops} />
      {(fromPlaceId || toPlaceId) && PLACE_CATEGORIES.map(category => placeCatalog.sources[category].error && (
        <p role="alert" key={category}>
          Could not load {categoryName(category).toLowerCase()}.{" "}
          <Button size="sm" variant="secondary" onClick={() => placeCatalog.retry(category)}>Retry loading places</Button>
        </p>
      ))}
 
      {tripState.status === "loading" && (
        <p className="transit-planner-message" role="status">
          Finding trips…
        </p>
      )}
      {tripState.status === "error" && (
        <div className="transit-planner-message" role="alert">
          <p>{tripState.message}</p>
          {(fromLinkedLocation ?? fromParam) && (toLinkedLocation ?? toParam) && (
            <Button
              type="button"
              variant="secondary"
              onClick={() => setTripRetry((value) => value + 1)}
            >
              Retry trip search
            </Button>
          )}
        </div>
      )}
      {tripState.status === "idle" && !fromParam && !toParam && fromLinkedLocation === null && toLinkedLocation === null && (
        <p className="transit-planner-message" role="status">
          Choose an origin and a destination to see trip options.
        </p>
      )}
      {tripState.status === "idle" && Boolean(fromParam || toParam || fromLinkedLocation || toLinkedLocation) && (
        <p className="transit-planner-message" role="status">
          Complete both locations to plan this trip.
        </p>
      )}
 
      {response?.partial && !inSidebar && (
        <p className="transit-planner-partial" role="status">
          TriMet returned some incomplete itinerary details. Available trip
          information is shown.
        </p>
      )}
      {historyUnavailable && (
        <p role="status">This trip could not be saved to recent journeys on this device.</p>
      )}
      {tripState.status === "empty" && (
        <p className="transit-planner-message" role="status">
          No trips were found between these locations. Try another pair of locations.
        </p>
      )}
      <TripPlannerResults state={state} renderMapStatus={renderMapStatus} />
    </section>
    <TripPlannerJourney state={state} directionsFooter={directionsFooter} />
    </>
  );
}