Skip to content

Commit 7d14133

Browse files
committed
harden map search cleanup
1 parent 27454d7 commit 7d14133

2 files changed

Lines changed: 48 additions & 39 deletions

File tree

src/components/MapThumbnail/MapThumbnail.tsx

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
sharedMediaFrameRadius,
66
sharedMediaFrameShapeStyles,
77
} from "@/styles/mediaFrame";
8-
import { useMemo } from "react";
8+
import { forwardRef, useMemo } from "react";
99

10-
import Map, { AttributionControl } from "react-map-gl/maplibre";
10+
import Map, { AttributionControl, type MapRef } from "react-map-gl/maplibre";
1111
import "maplibre-gl/dist/maplibre-gl.css";
1212
import { useLocale } from "next-intl";
1313

@@ -17,45 +17,48 @@ import { handleMapError } from "@/features/map/lib/mapErrors";
1717
import { createProtomapsStyle } from "@/features/map/lib/protomapsStyle";
1818
import { usePreferredMapFlavor } from "@/features/map/hooks/usePreferredMapFlavor";
1919

20+
type MapThumbnailProps = {
21+
height?: number | string;
22+
children?: ReactNode;
23+
} & Omit<ComponentProps<typeof Map>, "children" | "style" | "mapStyle">;
24+
2025
const MapContainer = styled.div`
2126
${sharedMediaFrameShapeStyles}
2227
${sharedMediaFrameBorderStyles}
2328
background-color: ${theme.colors.background.map};
2429
position: relative;
2530
`;
2631

27-
export default function MapThumbnail({
28-
height = "300",
29-
children,
30-
...props
31-
}: {
32-
height?: number | string;
33-
children?: ReactNode;
34-
} & Omit<ComponentProps<typeof Map>, "children" | "style" | "mapStyle">) {
35-
const locale = useLocale();
36-
const mapFlavor = usePreferredMapFlavor();
37-
const mapStyle = useMemo(
38-
() => createProtomapsStyle({ flavorName: mapFlavor, locale }),
39-
[locale, mapFlavor]
40-
);
32+
const MapThumbnail = forwardRef<MapRef, MapThumbnailProps>(
33+
function MapThumbnail({ height = "300", children, ...props }, ref) {
34+
const locale = useLocale();
35+
const mapFlavor = usePreferredMapFlavor();
36+
const mapStyle = useMemo(
37+
() => createProtomapsStyle({ flavorName: mapFlavor, locale }),
38+
[locale, mapFlavor]
39+
);
40+
41+
return (
42+
<MapContainer>
43+
<Map
44+
ref={ref}
45+
attributionControl={false} // Customised below
46+
mapStyle={mapStyle}
47+
onError={handleMapError}
48+
renderWorldCopies={true}
49+
style={{
50+
width: "100%",
51+
height: height,
52+
borderRadius: sharedMediaFrameRadius,
53+
}}
54+
{...props}
55+
>
56+
<AttributionControl compact={true} />
57+
{children}
58+
</Map>
59+
</MapContainer>
60+
);
61+
}
62+
);
4163

42-
return (
43-
<MapContainer>
44-
<Map
45-
attributionControl={false} // Customised below
46-
mapStyle={mapStyle}
47-
onError={handleMapError}
48-
renderWorldCopies={true}
49-
style={{
50-
width: "100%",
51-
height: height,
52-
borderRadius: sharedMediaFrameRadius,
53-
}}
54-
{...props}
55-
>
56-
<AttributionControl compact={true} />
57-
{children}
58-
</Map>
59-
</MapContainer>
60-
);
61-
}
64+
export default MapThumbnail;

src/features/map/hooks/useGeocodingSearch.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ export function useGeocodingSearch({
5555

5656
setStatus("loading");
5757

58+
let cancelled = false;
59+
5860
const timeout = window.setTimeout(() => {
5961
const apiKey = process.env.NEXT_PUBLIC_MAPTILER_API_KEY;
6062

6163
if (!apiKey) {
62-
if (requestIdRef.current === requestId) {
64+
if (!cancelled && requestIdRef.current === requestId) {
6365
setFeatures([]);
6466
setStatus("error");
6567
}
@@ -77,13 +79,13 @@ export function useGeocodingSearch({
7779
types: MAP_GEOCODING_TYPES,
7880
})
7981
.then((result) => {
80-
if (requestIdRef.current !== requestId) return;
82+
if (cancelled || requestIdRef.current !== requestId) return;
8183

8284
setFeatures(result.features);
8385
setStatus("success");
8486
})
8587
.catch((error) => {
86-
if (requestIdRef.current !== requestId) return;
88+
if (cancelled || requestIdRef.current !== requestId) return;
8789

8890
console.warn("Could not search MapTiler geocoding:", error);
8991
setFeatures([]);
@@ -92,6 +94,10 @@ export function useGeocodingSearch({
9294
}, debounceMs);
9395

9496
return () => {
97+
cancelled = true;
98+
if (requestIdRef.current === requestId) {
99+
requestIdRef.current += 1;
100+
}
95101
window.clearTimeout(timeout);
96102
};
97103
}, [countryCode, debounceMs, enabled, limit, minLength, proximity, query]);

0 commit comments

Comments
 (0)