diff --git a/src/modules/parcel-sync/components/tabs/map-tab.tsx b/src/modules/parcel-sync/components/tabs/map-tab.tsx index 4d6025b..8b5c2aa 100644 --- a/src/modules/parcel-sync/components/tabs/map-tab.tsx +++ b/src/modules/parcel-sync/components/tabs/map-tab.tsx @@ -101,6 +101,7 @@ export function MapTab({ siruta, sirutaValid, sessionConnected, syncLocalCount, const [mapReady, setMapReady] = useState(false); const appliedSirutaRef = useRef(""); const boundsRef = useRef<[number, number, number, number] | null>(null); + const prevCheckSirutaRef = useRef(""); /* Boundary check results */ const [mismatchSummary, setMismatchSummary] = useState<{ @@ -119,18 +120,34 @@ export function MapTab({ siruta, sirutaValid, sessionConnected, syncLocalCount, administrativ: false, }); - /* ── Detect when map is ready ──────────────────────────────── */ + /* ── Detect when map is ready (also re-detects after basemap switch) ── */ useEffect(() => { if (!sirutaValid) return; const check = setInterval(() => { const map = asMap(mapHandleRef.current); - if (map && map.isStyleLoaded()) { - setMapReady(true); - clearInterval(check); + if (!map || !map.isStyleLoaded()) { + // Map destroyed (basemap switch) — reset so effects re-run + if (mapReady) { + setMapReady(false); + appliedSirutaRef.current = ""; + prevCheckSirutaRef.current = ""; + } + return; } - }, 200); + // Map is ready — check if our custom source exists + // If not (new map instance after basemap switch), reset and re-apply + if (mapReady && !map.getSource("gis_terenuri_status")) { + appliedSirutaRef.current = ""; + prevCheckSirutaRef.current = ""; + setMapReady(false); + // Small delay to let the new map fully initialize + setTimeout(() => setMapReady(true), 100); + } else if (!mapReady) { + setMapReady(true); + } + }, 300); return () => clearInterval(check); - }, [sirutaValid]); + }, [sirutaValid, mapReady]); /* ── Fetch UAT bounds ──────────────────────────────────────── */ const prevBoundsSirutaRef = useRef(""); @@ -302,7 +319,6 @@ export function MapTab({ siruta, sirutaValid, sessionConnected, syncLocalCount, }, [mapReady, siruta, sirutaValid]); /* ── Boundary cross-check: load mismatched parcels ─────────── */ - const prevCheckSirutaRef = useRef(""); useEffect(() => { if (!mapReady || !sirutaValid || !siruta) return; if (prevCheckSirutaRef.current === siruta) return;