fix(parcel-sync): re-apply custom layers after basemap switch

MapViewer destroys and recreates the map when basemap changes. The
readiness polling now detects when custom sources are missing (new map
instance) and resets appliedSirutaRef + prevCheckSirutaRef, which
triggers all effects to re-run: siruta filter, enrichment overlay,
boundary mismatch GeoJSON, and fitBounds.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-24 18:10:13 +02:00
parent 3da45a4cab
commit 1dac5206e4
@@ -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;