fix: auto-scan race condition for no-geometry scan

- handleNoGeomScan accepts optional targetSiruta parameter
- useRef tracks last auto-scanned siruta to prevent duplicate scans
- Show zero result on error instead of hiding card (null)
- Fixes: FELEACU scan disappearing after 2s while COSBUC worked
This commit is contained in:
AI Assistant
2026-03-07 16:35:26 +02:00
parent 5861e06ddb
commit ddde2db900
@@ -679,46 +679,53 @@ export function ParcelSyncModule() {
/* No-geometry scan */
/* ════════════════════════════════════════════════════════════ */
const handleNoGeomScan = useCallback(async () => {
if (!siruta || noGeomScanning) return;
setNoGeomScanning(true);
setNoGeomScan(null);
try {
const res = await fetch("/api/eterra/no-geom-scan", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ siruta }),
});
const data = (await res.json()) as {
totalImmovables?: number;
totalInDb?: number;
noGeomCount?: number;
error?: string;
};
if (data.error) {
setNoGeomScan(null);
} else {
setNoGeomScan({
totalImmovables: data.totalImmovables ?? 0,
totalInDb: data.totalInDb ?? 0,
noGeomCount: data.noGeomCount ?? 0,
});
setNoGeomScanSiruta(siruta);
}
} catch {
const handleNoGeomScan = useCallback(
async (targetSiruta?: string) => {
const s = targetSiruta ?? siruta;
if (!s) return;
setNoGeomScanning(true);
setNoGeomScan(null);
}
setNoGeomScanning(false);
}, [siruta, noGeomScanning]);
setNoGeomScanSiruta(s);
try {
const res = await fetch("/api/eterra/no-geom-scan", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ siruta: s }),
});
const data = (await res.json()) as {
totalImmovables?: number;
totalInDb?: number;
noGeomCount?: number;
error?: string;
};
if (data.error) {
// Show zero result instead of hiding the card entirely
setNoGeomScan({ totalImmovables: 0, totalInDb: 0, noGeomCount: 0 });
} else {
setNoGeomScan({
totalImmovables: data.totalImmovables ?? 0,
totalInDb: data.totalInDb ?? 0,
noGeomCount: data.noGeomCount ?? 0,
});
}
} catch {
// Show zero result on network error
setNoGeomScan({ totalImmovables: 0, totalInDb: 0, noGeomCount: 0 });
}
setNoGeomScanning(false);
},
[siruta],
);
// Auto-scan for no-geometry parcels when UAT is selected + connected
const noGeomAutoScanRef = useRef("");
useEffect(() => {
if (!siruta || !session.connected || noGeomScanning) return;
// Don't re-scan if we already scanned this siruta
if (noGeomScanSiruta === siruta && noGeomScan !== null) return;
void handleNoGeomScan();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [siruta, session.connected]);
if (!siruta || !session.connected) return;
// Don't re-scan if we already scanned (or are scanning) this siruta
if (noGeomAutoScanRef.current === siruta) return;
noGeomAutoScanRef.current = siruta;
void handleNoGeomScan(siruta);
}, [siruta, session.connected, handleNoGeomScan]);
/* ════════════════════════════════════════════════════════════ */
/* Layer feature counts */