All files / applications/go-by-transit/src/view/home SearchHome.tsx

84.78% Statements 39/46
70.27% Branches 26/37
75% Functions 18/24
87.8% Lines 36/41

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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                              14x 5x 5x         14x                         2x                         14x 14x 14x 56x   56x 56x   14x 6x   14x 6x   14x 3x   14x 3x   14x                       6x                                                             2x 2x       2x 1x 1x 1x 1x     1x       1x       2x                                       15x 15x                                 1x                     14x 14x                                                  
import { Button } from "@wintermuted/ui-react";
import React, { useEffect, useRef, useState } from "react";
import { Link } from "react-router-dom";
import { SHORTCUTS } from "../../api/localstorage/bookmarkShortcuts";
import { useBookmarkShortcuts } from "@overworld/bookmarks/react";
import { readRecentJourneys } from "../../api/localstorage/recentJourneys";
import { useBookmarks } from "../../state/bookmarks/BookmarksProvider";
import { initializeMap } from "../nearby/util/mapbox/initializeMap";
import AllAlertsCard from "./AllAlertsCard";
import { useHomeAlerts } from "./Home";
import { SystemAlertsCard } from "./SystemAlertsCard";
import StopSearch from "./StopSearch";
import "./SearchHome.scss";
 
function RecentJourneys() {
  const [history] = useState(() => {
    try {
      return { journeys: readRecentJourneys(), error: false };
    } catch {
      return { journeys: [], error: true };
    }
  });
  return (
    <section
      className="transit-search-section"
      aria-labelledby="homepage-recent"
    >
      <h3 id="homepage-recent">Recent journeys</h3>
      {history.error ? (
        <p>Recent journeys are unavailable on this device.</p>
      ) : history.journeys.length === 0 ? (
        <p>Trips you find in the planner appear here.</p>
      ) : (
        <ul className="transit-search-list">
          {history.journeys.map(({ from, to }) => (
            <li key={`${from.locid}-${to.locid}`}>
              <Link to={`/planner?from=${from.locid}&to=${to.locid}`}>
                {from.desc} ({from.locid}) to {to.desc} ({to.locid})
              </Link>
            </li>
          ))}
        </ul>
      )}
    </section>
  );
}
 
function BookmarkPreview() {
  const { stops, routes } = useBookmarks();
  const { shortcuts, error } = useBookmarkShortcuts(stops, routes);
  const quickLinks = SHORTCUTS.flatMap(({ key, kind, label }) => {
    const id = shortcuts[key];
    const bookmark =
      id === undefined ? undefined : (kind === "stop" ? stops : routes)[id];
    return bookmark ? [{ key, kind, id, label, desc: bookmark.desc }] : [];
  });
  const pinnedStops = new Set(
    quickLinks.filter((link) => link.kind === "stop").map((link) => link.id),
  );
  const pinnedRoutes = new Set(
    quickLinks.filter((link) => link.kind === "route").map((link) => link.id),
  );
  const stopEntries = Object.entries(stops)
    .filter(([id]) => !pinnedStops.has(Number(id)))
    .slice(0, 3);
  const routeEntries = Object.values(routes)
    .filter((route) => !pinnedRoutes.has(route.route))
    .slice(0, 3);
  return (
    <section
      className="transit-search-section"
      aria-labelledby="homepage-bookmarks"
    >
      <h3 id="homepage-bookmarks">Bookmarks</h3>
      {error && <p role="alert">{error}</p>}
      {quickLinks.length > 0 ||
      stopEntries.length > 0 ||
      routeEntries.length > 0 ? (
        <ul className="transit-search-list">
          {quickLinks.map(({ key, kind, id, label, desc }) => (
            <li key={key}>
              <Link to={kind === "stop" ? `/stop/${id}` : `/lines/${id}`}>
                <strong>{label}:</strong>{" "}
                {desc || (kind === "stop" ? "Stop" : "Transit route")} ({id})
              </Link>
            </li>
          ))}
          {stopEntries.map(([id, stop]) => (
            <li key={`stop-${id}`}>
              <Link to={`/stop/${id}`}>
                {stop.desc || "Stop"} ({id})
              </Link>
            </li>
          ))}
          {routeEntries.map((route) => (
            <li key={`route-${route.route}`}>
              <Link to={`/lines/${route.route}`}>
                Route {route.route}: {route.desc || "Transit route"}
              </Link>
            </li>
          ))}
        </ul>
      ) : (
        <p>No bookmarks yet. Save a stop or route to see it here.</p>
      )}
      <Link to="/bookmarks">Manage bookmarks</Link>
    </section>
  );
}
 
function MapPreview() {
  const container = useRef<HTMLDivElement>(null);
  const [status, setStatus] = useState<"loading" | "ready" | "error">(
    "loading",
  );
 
  useEffect(() => {
    try {
      const map = initializeMap(-122.6784, 45.5152, container, 10, false);
      map.once("load", () => setStatus("ready"));
      map.on("error", () =>
        setStatus((previous) => (previous === "ready" ? previous : "error")),
      );
      return () => {
        map.remove();
      };
    } catch {
      setStatus("error");
    }
  }, []);
 
  return (
    <>
      {status === "loading" && <p role="status">Loading the map preview…</p>}
      {status === "error" && (
        <p role="status">
          The map preview is unavailable. You can still search stops or open
          Nearby.
        </p>
      )}
      <div
        ref={container}
        className="transit-search-map"
        role="region"
        aria-label="Portland area map preview"
      />
    </>
  );
}
 
function NearbyPreview() {
  const [showMap, setShowMap] = useState(false);
  return (
    <section
      className="transit-search-section"
      aria-labelledby="homepage-nearby"
    >
      <h3 id="homepage-nearby">Nearby</h3>
      <p>
        Search stops without sharing your location. In Nearby, use your location
        or pick an area on the map.
      </p>
      <div className="transit-search-links">
        <Link to="/nearby">Nearby routes</Link>
        <Link to="/nearby/stops">Nearby stops</Link>
        <Button
          type="button"
          variant="secondary"
          aria-expanded={showMap}
          onClick={() => setShowMap((open) => !open)}
        >
          {showMap ? "Hide map preview" : "Preview Portland map"}
        </Button>
      </div>
      {showMap && <MapPreview />}
    </section>
  );
}
 
export default function SearchHome() {
  const { alerts, systemAlerts, ...alertState } = useHomeAlerts();
  return (
    <div className="transit-search-home">
      <div className="transit-search-columns">
        <StopSearch />
        <div className="transit-search-support">
          <NearbyPreview />
          <RecentJourneys />
          <BookmarkPreview />
        </div>
      </div>
      <section
        id="homepage-alerts"
        className="transit-search-section"
        aria-labelledby="homepage-alerts-title"
        tabIndex={-1}
      >
        <h3 id="homepage-alerts-title">Service alerts</h3>
        <div className="transit-search-alerts">
          <SystemAlertsCard systemAlertsData={systemAlerts} {...alertState} />
          <AllAlertsCard alertsData={alerts} {...alertState} />
        </div>
      </section>
    </div>
  );
}