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 | 3x 32x 32x 32x 32x 32x 23x 13x 19x 13x 13x 32x 32x 32x 8x 17x | import { Button, FieldGroup } from "@wintermuted/ui-react";
import { searchStopCatalog } from "@overworld/trimet-client";
import type { StopCatalog, StopCatalogStop } from "@overworld/trimet-client";
import React, { useId, useMemo, useState } from "react";
import "./StopSearch.scss";
const MATCH_LIMIT = 6;
export interface StopSearchProps {
catalog?: StopCatalog;
loading: boolean;
error: boolean;
onRetry: () => void;
renderStopLinks: (stop: StopCatalogStop) => React.ReactNode;
footer?: React.ReactNode;
inputId?: string;
}
export default function StopSearch({ catalog, loading, error, onRetry, renderStopLinks, footer, inputId }: StopSearchProps) {
const generatedId = useId();
const id = inputId ?? generatedId;
const titleId = `${id}-title`;
const [query, setQuery] = useState("");
const matches = useMemo(() => {
if (!catalog) return [];
const results = searchStopCatalog(catalog.stops, query);
const exact = results.findIndex(stop => String(stop.locid) === query.trim());
if (exact > 0) results.unshift(...results.splice(exact, 1));
return results;
}, [catalog, query]);
const hasQuery = Boolean(query.trim());
const partial = catalog && (catalog.incompleteRouteCount > 0 ||
catalog.incompleteDirectionCount > 0 || catalog.invalidStopCount > 0);
return (
<section className="transit-search-primary" aria-labelledby={titleId}>
<header>
<h2 id={titleId}>Find a stop</h2>
<p>Search TriMet stops to check arrivals or plan a trip.</p>
</header>
<FieldGroup label="Stop name or ID" htmlFor={id}>
<input id={id} className="wm-input" type="search" autoComplete="off" value={query}
onChange={event => setQuery(event.target.value)} />
</FieldGroup>
{!catalog && loading && !error && <p role="status">Loading TriMet stops…</p>}
{error && <>
<p role="alert">Could not load TriMet stops. Check your connection and try again.</p>
<Button type="button" variant="secondary" onClick={onRetry}>Retry loading stops</Button>
</>}
{partial && <p role="status">TriMet returned a partial stop list. Some stops may be missing.</p>}
{catalog && hasQuery && <p role="status">
{!matches.length ? "No stops match. Try another name or stop ID." :
matches.length > MATCH_LIMIT ?
`Showing ${MATCH_LIMIT} of ${matches.length} matches. Refine your search to see fewer results.` :
`${matches.length} ${matches.length === 1 ? "stop matches" : "stops match"}.`}
</p>}
{matches.length > 0 && <ul className="transit-search-list" aria-label="Matching stops">
{matches.slice(0, MATCH_LIMIT).map(stop => <li className="transit-search-result" key={stop.locid}>
<div><strong>{stop.desc}</strong><small>Stop {stop.locid}</small></div>
<div className="transit-search-links">{renderStopLinks(stop)}</div>
</li>)}
</ul>}
{footer && <div className="transit-search-links">{footer}</div>}
</section>
);
}
|