import SearchState from '@/components/search-state'; import { useEffect, useState } from 'react'; import TerritoryGeometryPreview from './territory-geometry-preview'; import InvestmentMapCoverage from './investment-map-coverage'; import InteractiveInvestmentMap, { type InvestmentMapFeature, type InvestmentMapArea, } from './interactive-investment-map'; export default function InvestmentMapPanel({ filters, territoryId, }: { filters: Record; territoryId?: string; }) { const key = JSON.stringify(filters); const [points, setPoints] = useState(null); const [missing, setMissing] = useState< { id: number; title: string; url: string }[] >([]); const [error, setError] = useState(''); const [retry, setRetry] = useState(0); const [areas, setAreas] = useState([]); const [truncated, setTruncated] = useState(false); const [showPoints, setShowPoints] = useState(true); const [showAreas, setShowAreas] = useState(true); const [matched, setMatched] = useState(0); useEffect(() => { const controller = new AbortController(); const params = new URLSearchParams(JSON.parse(key)); setPoints(null); setError(''); setMissing([]); setAreas([]); setTruncated(false); setMatched(0); fetch(`/api/v1/investment/map?${params}`, { signal: controller.signal, headers: { Accept: 'application/json' }, }) .then(async (response) => { if (!response.ok) throw new Error('Mapa indisponível.'); return response.json(); }) .then((data) => { if (controller.signal.aborted) return; setPoints(data.features); setAreas(data.areas ?? []); setMissing(data.unlocated ?? []); setTruncated(Boolean(data.truncated)); setMatched( data.matched_count ?? new Set( (data.features as InvestmentMapFeature[]).map( (point) => point.properties.id, ), ).size + (data.unlocated?.length ?? 0), ); }) .catch(() => { if (!controller.signal.aborted) setError('Não foi possível concluir a pesquisa.'); }); return () => controller.abort(); }, [key, retry]); return (
setRetry((value) => value + 1)} />
Camadas desta consulta
{!showPoints && !showAreas && (

Camadas ocultas. Active uma camada para voltar a consultar os registos no mapa.

)} {points !== null && ( )} {missing.length > 0 && (

{missing.length} investimentos sem ponto público utilizável. Consulte as fichas para localizações documentadas sem coordenadas:

)}
); }