Consolida rastreabilidade territorial e plano autenticado V2
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import type { Map as LeafletMap, Marker } from 'leaflet';
|
||||
import type { GeoJsonObject } from 'geojson';
|
||||
import 'leaflet/dist/leaflet.css';
|
||||
|
||||
export type InvestmentMapFeature = {
|
||||
@@ -13,13 +14,25 @@ export type InvestmentMapFeature = {
|
||||
clusters?: string[];
|
||||
corridors?: string[];
|
||||
projects?: string[];
|
||||
source_reference?: string | null;
|
||||
source_date?: string | null;
|
||||
};
|
||||
};
|
||||
export type InvestmentMapArea = {
|
||||
geometry: GeoJsonObject;
|
||||
title: string;
|
||||
source_reference?: string | null;
|
||||
source_date?: string | null;
|
||||
url: string;
|
||||
};
|
||||
const noAreas: InvestmentMapArea[] = [];
|
||||
|
||||
export default function InteractiveInvestmentMap({
|
||||
points,
|
||||
areas = noAreas,
|
||||
}: {
|
||||
points: InvestmentMapFeature[] | null;
|
||||
areas?: InvestmentMapArea[];
|
||||
}) {
|
||||
const element = useRef<HTMLDivElement>(null);
|
||||
const map = useRef<LeafletMap | null>(null);
|
||||
@@ -35,6 +48,9 @@ export default function InteractiveInvestmentMap({
|
||||
const instance = L.map(element.current, {
|
||||
scrollWheelZoom: false,
|
||||
keyboard: true,
|
||||
zoomAnimation: false,
|
||||
fadeAnimation: false,
|
||||
markerZoomAnimation: false,
|
||||
}).setView([-18.5, 35], 5);
|
||||
map.current = instance;
|
||||
L.tileLayer(
|
||||
@@ -55,6 +71,19 @@ export default function InteractiveInvestmentMap({
|
||||
.addTo(instance);
|
||||
L.control.scale({ imperial: false }).addTo(instance);
|
||||
const bounds: [number, number][] = [];
|
||||
for (const area of areas) {
|
||||
const label = document.createElement('p');
|
||||
label.textContent = `${area.title} · Polígono cadastrado, limites oficiais por verificar · Fonte: ${area.source_reference ?? 'Não indicada'} · Data: ${area.source_date ?? 'Não documentada'}`;
|
||||
const layer = L.geoJSON(area.geometry)
|
||||
.bindPopup(label)
|
||||
.addTo(instance);
|
||||
const extent = layer.getBounds();
|
||||
if (extent.isValid())
|
||||
bounds.push(
|
||||
[extent.getSouth(), extent.getWest()],
|
||||
[extent.getNorth(), extent.getEast()],
|
||||
);
|
||||
}
|
||||
for (const [index, point] of (points ?? []).entries()) {
|
||||
const [longitude, latitude] = point.geometry.coordinates;
|
||||
if (
|
||||
@@ -72,6 +101,7 @@ export default function InteractiveInvestmentMap({
|
||||
for (const text of [
|
||||
p.category,
|
||||
p.location,
|
||||
`Fonte: ${p.source_reference ?? 'Não indicada'} · Data: ${p.source_date ?? 'Não documentada'}`,
|
||||
`Corredor: ${p.corridors?.join(', ') || 'Não associado'}`,
|
||||
`Cluster: ${p.clusters?.join(', ') || 'Não associado'}`,
|
||||
`Projecto âncora: ${p.projects?.join(', ') || 'Não associado'}`,
|
||||
@@ -98,18 +128,22 @@ export default function InteractiveInvestmentMap({
|
||||
alt: p.title,
|
||||
keyboard: true,
|
||||
})
|
||||
.bindPopup(popup)
|
||||
.on('click', () => setSelected(p.id))
|
||||
.bindPopup(popup, { autoPan: false })
|
||||
.on('click', () => setSelected(index))
|
||||
.addTo(instance);
|
||||
markers.current.set(p.id, marker);
|
||||
markers.current.set(index, marker);
|
||||
bounds.push([latitude, longitude]);
|
||||
}
|
||||
if (bounds.length)
|
||||
instance.fitBounds(bounds, {
|
||||
padding: [40, 40],
|
||||
maxZoom: 14,
|
||||
animate: false,
|
||||
});
|
||||
observer = new ResizeObserver(() => instance.invalidateSize());
|
||||
observer = new ResizeObserver(() => {
|
||||
if (!cancelled)
|
||||
instance.invalidateSize({ animate: false, pan: false });
|
||||
});
|
||||
observer.observe(element.current);
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -122,7 +156,7 @@ export default function InteractiveInvestmentMap({
|
||||
map.current = null;
|
||||
markers.current.clear();
|
||||
};
|
||||
}, [points]);
|
||||
}, [points, areas]);
|
||||
return (
|
||||
<section
|
||||
className="space-y-3 rounded-xl border bg-white p-4 text-slate-900"
|
||||
@@ -143,15 +177,15 @@ export default function InteractiveInvestmentMap({
|
||||
<p role="status">A carregar localizações…</p>
|
||||
) : (
|
||||
<p role="status">
|
||||
{points.length} investimentos com coordenadas disponíveis.
|
||||
{points.length} locais com coordenadas disponíveis.
|
||||
</p>
|
||||
)}
|
||||
<ul className="grid gap-2 md:grid-cols-2">
|
||||
{(points ?? []).map((p) => (
|
||||
{(points ?? []).map((p, index) => (
|
||||
<li
|
||||
key={p.properties.id}
|
||||
key={index}
|
||||
className={
|
||||
selected === p.properties.id
|
||||
selected === index
|
||||
? 'rounded border-2 border-emerald-700 p-3'
|
||||
: 'rounded border p-3'
|
||||
}
|
||||
@@ -159,18 +193,17 @@ export default function InteractiveInvestmentMap({
|
||||
<button
|
||||
type="button"
|
||||
className="text-left font-bold underline"
|
||||
aria-pressed={selected === p.properties.id}
|
||||
aria-pressed={selected === index}
|
||||
onClick={() => {
|
||||
const marker = markers.current.get(
|
||||
p.properties.id,
|
||||
);
|
||||
const marker = markers.current.get(index);
|
||||
if (marker) {
|
||||
map.current?.setView(
|
||||
marker.getLatLng(),
|
||||
14,
|
||||
{ animate: false },
|
||||
);
|
||||
marker.openPopup();
|
||||
setSelected(p.properties.id);
|
||||
setSelected(index);
|
||||
}
|
||||
}}
|
||||
>
|
||||
@@ -179,12 +212,28 @@ export default function InteractiveInvestmentMap({
|
||||
<p>
|
||||
{p.properties.category} · {p.properties.location}
|
||||
</p>
|
||||
<p>
|
||||
Fonte:{' '}
|
||||
{p.properties.source_reference ?? 'Não indicada'} ·
|
||||
Data:{' '}
|
||||
{p.properties.source_date ?? 'Não documentada'}
|
||||
</p>
|
||||
<a className="underline" href={p.properties.url}>
|
||||
Abrir ficha
|
||||
</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
{areas.map((area, index) => (
|
||||
<p key={index}>
|
||||
<a className="underline" href={area.url}>
|
||||
{area.title}
|
||||
</a>{' '}
|
||||
· Polígono cadastrado, limites oficiais por verificar ·
|
||||
Fonte: {area.source_reference ?? 'Não indicada'} · Data:{' '}
|
||||
{area.source_date ?? 'Não documentada'}
|
||||
</p>
|
||||
))}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user