Skip to content

Commit 7ba5fe6

Browse files
committed
address geocoding review
1 parent ad890df commit 7ba5fe6

3 files changed

Lines changed: 54 additions & 28 deletions

File tree

src/components/LocationSelect/LocationSelect.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const ZOOM_LEVEL = 16;
4343
import { config, geocoding, geolocation } from "@maptiler/client";
4444

4545
// Reverse geocoding for legible location (area_name)
46-
config.apiKey = process.env.NEXT_PUBLIC_MAPTILER_API_KEY ?? "";
46+
const mapTilerApiKey = process.env.NEXT_PUBLIC_MAPTILER_API_KEY ?? "";
47+
config.apiKey = mapTilerApiKey;
4748

4849
// const maptilerClient = new maptilersdk.Maptiler();
4950

@@ -148,14 +149,12 @@ async function getAreaNameMatch(
148149
latitude: number
149150
): Promise<AreaNameMatch | null> {
150151
try {
151-
const apiKey = process.env.NEXT_PUBLIC_MAPTILER_API_KEY;
152-
153-
if (!apiKey) {
152+
if (!mapTilerApiKey) {
154153
return null;
155154
}
156155

157156
const coordinates = await geocoding.reverse([longitude, latitude], {
158-
apiKey,
157+
apiKey: mapTilerApiKey,
159158
});
160159

161160
return getBestAreaNameMatchFromFeatures(
@@ -191,6 +190,10 @@ export default function LocationSelect({
191190

192191
useEffect(() => {
193192
if (autoDetectCountry && !countryCode) {
193+
if (!mapTilerApiKey) {
194+
return;
195+
}
196+
194197
let isMounted = true; // Track if component is mounted
195198

196199
const initializeLocation = async () => {

src/features/map/components/GeocodingSearch.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,28 @@ const GeocodingSearch = forwardRef<GeocodingSearchHandle, GeocodingSearchProps>(
257257
const [query, setQuery] = useState("");
258258
const [isFocused, setIsFocused] = useState(false);
259259
const [activeIndex, setActiveIndex] = useState(0);
260+
const hasSearchableQuery =
261+
query.trim().length >= MAP_GEOCODING_MIN_QUERY_LENGTH;
262+
const canShowResults = variant === "palette" || isFocused;
263+
const showResults = hasSearchableQuery && canShowResults;
260264
const { features, isError, isLoading, isReady } = useGeocodingSearch({
261265
query,
262266
countryCode,
267+
enabled: showResults,
263268
proximity,
264269
});
265-
const hasSearchableQuery =
266-
query.trim().length >= MAP_GEOCODING_MIN_QUERY_LENGTH;
267-
const showResults =
268-
hasSearchableQuery && (variant === "palette" || isFocused);
270+
const hasVisibleListbox = showResults && features.length > 0;
269271
const showNoResults =
270272
showResults && isReady && !isLoading && !isError && features.length === 0;
271-
const statusMessage = isLoading
272-
? loadingMessage
273-
: isError
274-
? error || errorMessage
275-
: showNoResults
276-
? noResultsMessage
277-
: "";
273+
const statusMessage = showResults
274+
? isLoading
275+
? loadingMessage
276+
: isError
277+
? error || errorMessage
278+
: showNoResults
279+
? noResultsMessage
280+
: ""
281+
: "";
278282
const shouldRenderStatusMessage =
279283
variant === "palette" || !onStatusMessageChange;
280284
const activeFeature = showResults ? features[activeIndex] : undefined;
@@ -345,8 +349,8 @@ const GeocodingSearch = forwardRef<GeocodingSearchHandle, GeocodingSearchProps>(
345349
autoFocus={autoFocus}
346350
placeholder={placeholder}
347351
aria-autocomplete="list"
348-
aria-controls={listId}
349-
aria-expanded={showResults}
352+
aria-controls={hasVisibleListbox ? listId : undefined}
353+
aria-expanded={hasVisibleListbox}
350354
aria-label={ariaLabel}
351355
aria-labelledby={ariaLabelledBy}
352356
aria-invalid={ariaInvalid}
@@ -389,7 +393,7 @@ const GeocodingSearch = forwardRef<GeocodingSearchHandle, GeocodingSearchProps>(
389393
</StatusMessage>
390394
) : null}
391395

392-
{showResults && features.length > 0 ? (
396+
{hasVisibleListbox ? (
393397
<ResultsPanel $variant={variant}>
394398
<ResultList id={listId} role="listbox">
395399
{features.map((feature, index) => (

src/features/map/components/MapSearch.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useTranslations } from "next-intl";
66
import { keyframes, styled } from "next-yak";
77
import type { GeocodingFeature } from "@maptiler/client";
88

9+
import IconButton from "@/components/IconButton";
910
import { theme } from "@/styles/theme.yak";
1011
import GeocodingSearch from "./GeocodingSearch";
1112

@@ -115,39 +116,57 @@ const DialogContent = styled(Dialog.Content)`
115116
}
116117
`;
117118

119+
const DialogCloseButton = styled(IconButton)`
120+
position: absolute;
121+
right: -0.625rem;
122+
top: -0.625rem;
123+
border-radius: 0.7rem;
124+
box-shadow: 0 0.25rem 0.75rem rgba(0, 0, 0, 0.13);
125+
`;
126+
118127
export default function MapSearch({
119128
countryCode,
120129
onOpenChange,
121130
onPick,
122131
open,
123132
}: MapSearchProps) {
124-
const t = useTranslations("Map");
133+
const actionsT = useTranslations("Actions");
134+
const mapT = useTranslations("Map");
135+
const closeLabel = actionsT("close");
125136

126137
return (
127138
<Dialog.Root open={open} onOpenChange={onOpenChange}>
128139
<Dialog.Portal>
129140
<DialogOverlay />
130141
<DialogContent data-testid="map-search-dialog">
131142
<VisuallyHidden.Root>
132-
<Dialog.Title>{t("searchDialogTitle")}</Dialog.Title>
133-
<Dialog.Description>{t("searchPlaceholder")}</Dialog.Description>
143+
<Dialog.Title>{mapT("searchDialogTitle")}</Dialog.Title>
144+
<Dialog.Description>{mapT("searchPlaceholder")}</Dialog.Description>
134145
</VisuallyHidden.Root>
135146
<GeocodingSearch
136-
ariaLabel={t("searchDialogTitle")}
147+
ariaLabel={mapT("searchDialogTitle")}
137148
autoFocus={true}
138-
clearLabel={t("searchClear")}
149+
clearLabel={mapT("searchClear")}
139150
countryCode={countryCode}
140-
errorMessage={t("searchError")}
141-
loadingMessage={t("searchLoading")}
142-
noResultsMessage={t("searchNoResults")}
151+
errorMessage={mapT("searchError")}
152+
loadingMessage={mapT("searchLoading")}
153+
noResultsMessage={mapT("searchNoResults")}
143154
onPick={(feature) => {
144155
onPick(feature);
145156
onOpenChange(false);
146157
}}
147-
placeholder={t("searchPlaceholder")}
158+
placeholder={mapT("searchPlaceholder")}
148159
proximity="ip"
149160
variant="palette"
150161
/>
162+
<Dialog.Close asChild>
163+
<DialogCloseButton
164+
icon="close"
165+
aria-label={closeLabel}
166+
title={closeLabel}
167+
type="button"
168+
/>
169+
</Dialog.Close>
151170
</DialogContent>
152171
</Dialog.Portal>
153172
</Dialog.Root>

0 commit comments

Comments
 (0)