({ markers, setMapInstance, task_type = "LOCATION_LIST" }: ClientMapProps)
| 20 | } |
| 21 | |
| 22 | export default function ClientMap({ markers, setMapInstance, task_type = "LOCATION_LIST" }: ClientMapProps) { |
| 23 | const mapContainer = useRef<HTMLDivElement>(null) |
| 24 | const [mapError, setMapError] = useState<string | null>(null) |
| 25 | const [isMapInitialized, setIsMapInitialized] = useState(false) |
| 26 | // useRef to hold the map instance, to ensure cleanup targets the correct instance |
| 27 | const mapRef = useRef<L.Map | null>(null); |
| 28 | |
| 29 | useEffect(() => { |
| 30 | // Guard against re-initialization if already initialized by this instance |
| 31 | if (!mapContainer.current || isMapInitialized) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | let isActive = true; // Flag to manage async operations for mounted component |
| 36 | |
| 37 | const initializeMap = async () => { |
| 38 | try { |
| 39 | const Leaflet = (await import("leaflet")).default; // Dynamically import Leaflet |
| 40 | if (!isActive || !mapContainer.current) return; |
| 41 | |
| 42 | // Fix Leaflet icon issues |
| 43 | if (Leaflet.Icon && Leaflet.Icon.Default && Leaflet.Icon.Default.prototype) { |
| 44 | delete (Leaflet.Icon.Default.prototype as any)._getIconUrl; |
| 45 | Leaflet.Icon.Default.mergeOptions({ |
| 46 | iconRetinaUrl: "https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon-2x.png", |
| 47 | iconUrl: "https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png", |
| 48 | shadowUrl: "https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png", |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | // Defensive check: if the container DOM node already has leaflet's internal ID |
| 53 | if ((mapContainer.current as any)._leaflet_id) { |
| 54 | console.warn( |
| 55 | "Leaflet map container already had _leaflet_id. This might indicate an incomplete cleanup from a previous instance or a StrictMode interaction." |
| 56 | ); |
| 57 | // Attempt to remove any existing map instance on this container before creating a new one |
| 58 | // This is a more aggressive cleanup for problematic scenarios. |
| 59 | if (mapRef.current) { |
| 60 | mapRef.current.remove(); |
| 61 | mapRef.current = null; |
| 62 | } |
| 63 | // Try to clear the id, though this is delving into Leaflet internals |
| 64 | delete (mapContainer.current as any)._leaflet_id; |
| 65 | } |
| 66 | |
| 67 | const newMapInstance = Leaflet.map(mapContainer.current).setView([35, 105], 4); |
| 68 | mapRef.current = newMapInstance; // Store new map instance in ref |
| 69 | |
| 70 | if (!isActive) { |
| 71 | newMapInstance.remove(); // Clean up if component unmounted during init |
| 72 | mapRef.current = null; |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | Leaflet.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { |
| 77 | attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
| 78 | maxZoom: 19, |
| 79 | }).addTo(newMapInstance); |
nothing calls this directly
no test coverage detected