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 | 13x 14x 14x 270x 270x 270x 288x 288x 186x 975x 186x 270x 270x 270x 5268x 5268x 5268x 5268x 5268x 460x 5268x 270x 460x 128x 128x 128x 128x 128x 128x 128x 128x 53x 128x 128x 2165x 128x 128x 128x 128x 41x 41x 2x 135x 87x 2x | import { Button } from "@wintermuted/ui-react";
import { GlobalPlaceSearch, PlaceResultRow } from "@overworld/ui";
import React, { useEffect, useMemo, useState } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Link, useLocation } from "react-router-dom";
import {
categoryName,
normalizePlaceSearch,
PLACE_CATEGORIES,
placeDetailLink,
searchPlaces,
usePlaceCatalog,
} from "@overworld/places/catalog";
import PlacesLoadStatus, {
placeSourceIssues,
} from "../../nearby/components/PlacesLoadStatus";
import { placeIconDefinition } from "../../nearby/util/mapbox/placeIcons";
import { useSharedDeviceLocation } from "../../../api/geolocation/geoLocateCurrentPosition";
import { getDistance } from "../../nearby/util/turfUtils";
import "./GlobalPlacesSearch.scss";
const PAGE_SIZE = 10;
function distanceLabel(
origin: readonly [number, number],
destination: [number, number],
) {
const miles = getDistance([...origin], destination);
return miles < 1
? `${Math.round(miles * 5280)} ft`
: `${miles.toFixed(1)} mi`;
}
function highlightMatches(text: string, query: string) {
const normalized = normalizePlaceSearch(text);
const matches = new Set<number>();
for (const term of normalizePlaceSearch(query).split(/\s+/).filter(Boolean)) {
let start = normalized.indexOf(term);
while (start >= 0) {
for (let index = start; index < start + term.length; index++)
matches.add(index);
start = normalized.indexOf(term, start + 1);
}
}
const parts: { text: string; matched: boolean }[] = [];
let offset = 0;
for (const character of text) {
const length = normalizePlaceSearch(character).length;
const previous = parts[parts.length - 1];
// Keep original accents, combining marks and surrogate pairs intact.
const matched = length
? Array.from({ length }, (_, index) => matches.has(offset + index)).some(
Boolean,
)
: previous?.matched || false;
if (previous && previous.matched === matched) previous.text += character;
else parts.push({ text: character, matched });
offset += length;
}
return parts.map((part, index) =>
part.matched ? <mark key={index}>{part.text}</mark> : part.text,
);
}
export default function GlobalPlacesSearch() {
const [activated, setActivated] = useState(false);
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
const [limit, setLimit] = useState(PAGE_SIZE);
const catalog = usePlaceCatalog(activated);
const deviceLocation = useSharedDeviceLocation();
const location = useLocation();
const matches = useMemo(
() => searchPlaces(catalog.places, query, deviceLocation),
[catalog.places, query, deviceLocation],
);
const issues = placeSourceIssues(catalog.sources, catalog.retry);
const loading =
activated &&
PLACE_CATEGORIES.some(
(category) =>
catalog.sources[category].loading ||
(!catalog.sources[category].snapshot &&
!catalog.sources[category].error),
);
const hasQuery = Boolean(query.trim());
useEffect(() => setOpen(false), [location]);
useEffect(() => setLimit(PAGE_SIZE), [query]);
return (
<GlobalPlaceSearch
query={query}
onQueryChange={setQuery}
open={open}
onOpenChange={(next) => {
if (next) setActivated(true);
setOpen(next);
}}
hint="Search every place category across Portland, regardless of map filters. Use Arrow Down or Tab to browse results. Escape closes search."
scope="All categories, across Portland. Map filters stay unchanged."
loadStatus={
<PlacesLoadStatus
label="All places"
issues={issues}
loading={loading && !issues.length}
/>
}
status={
!hasQuery
? "Search by name, category, address or source-listed type."
: matches.length
? `Showing ${Math.min(limit, matches.length)} of ${matches.length} ${matches.length === 1 ? "place" : "places"}${loading ? " found so far" : ""}.`
: loading
? "Loading the place catalog..."
: issues.length
? "No matching places in the loaded categories. Retry unavailable categories above."
: "No places match. Try another name, category or address."
}
moreAction={
hasQuery &&
matches.length > limit && (
<Button
className="transit-global-search-more"
size="sm"
variant="secondary"
onClick={() => setLimit((previous) => previous + PAGE_SIZE)}
>
Show more places ({matches.length - limit} remaining)
</Button>
)
}
>
{hasQuery && matches.length > 0 && (
<ul
aria-label="Matching places"
className="transit-global-search-results"
>
{matches.slice(0, limit).map((place) => (
<PlaceResultRow
key={place.id}
variant="search"
name={highlightMatches(place.name, query)}
secondary={highlightMatches(
`${categoryName(place.category)}${place.address ? ` / ${place.address}` : ""}`,
query,
)}
icon={
<FontAwesomeIcon
icon={placeIconDefinition(place.category)}
aria-hidden="true"
className={`transit-place-icon transit-place-icon--${place.category}`}
/>
}
renderLink={(props) => (
<Link
{...props}
to={placeDetailLink(place.id)}
data-global-place={place.id}
onClick={() => setOpen(false)}
/>
)}
trailing={
deviceLocation && (
<span className="transit-global-search-distance">
<span>
{distanceLabel(deviceLocation, place.coordinates)}
</span>
<small>
straight-line
<span className="wm-sr-only">
{" "}
from your shared location
</span>
</small>
</span>
)
}
/>
))}
</ul>
)}
</GlobalPlaceSearch>
);
}
|