// /components/mainComponent/hooks/useInitializeMap.js import { useEffect } from "react"; import { initializeMap } from "../../../utils/initializeMap"; const useInitializeMap = ( map, mapRef, setMap, setOms, setMenuItemAdded, addItemsToMapContextMenu, hasRights, setPolylineEventsDisabled, mapOptions ) => { useEffect(() => { let cancelled = false; function tryInit(firstAttempt = true) { if (cancelled) return; // Only try to initialize if mapRef.current is ready and in DOM if ( mapRef.current && mapRef.current instanceof HTMLElement && document.body.contains(mapRef.current) && !map && !mapRef.current._leaflet_id ) { try { const result = initializeMap( mapRef.current, // pass DOM node, not ref setMenuItemAdded, addItemsToMapContextMenu, hasRights, setPolylineEventsDisabled, firstAttempt // log error only on first real attempt ); if (result && result.map && result.oms) { setMap(result.map); setOms(result.oms); } } catch (error) { if (process.env.NODE_ENV === "development") { // eslint-disable-next-line no-console console.warn("Map initialization error:", error); } } } else if (!map && !cancelled) { // If not ready, just retry after a short delay, do not call initializeMap at all setTimeout(() => tryInit(false), 50); } } tryInit(true); return () => { cancelled = true; }; }, [mapRef, map, hasRights, setPolylineEventsDisabled, mapOptions]); }; export default useInitializeMap;