@@ -1,6 +1,6 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { Map as LeafletMap, Marker } from "leaflet";
|
||||
import "leaflet/dist/leaflet.css";
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import type { Map as LeafletMap, Marker } from 'leaflet';
|
||||
import 'leaflet/dist/leaflet.css';
|
||||
|
||||
export type InvestmentMapFeature = {
|
||||
geometry: { coordinates: [number, number] };
|
||||
@@ -25,11 +25,11 @@ export default function InteractiveInvestmentMap({
|
||||
const map = useRef<LeafletMap | null>(null);
|
||||
const markers = useRef(new Map<number, Marker>());
|
||||
const [selected, setSelected] = useState<number | null>(null);
|
||||
const [error, setError] = useState("");
|
||||
const [error, setError] = useState('');
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
let observer: ResizeObserver | undefined;
|
||||
void import("leaflet")
|
||||
void import('leaflet')
|
||||
.then((L) => {
|
||||
if (cancelled || !element.current) return;
|
||||
const instance = L.map(element.current, {
|
||||
@@ -39,17 +39,17 @@ export default function InteractiveInvestmentMap({
|
||||
map.current = instance;
|
||||
L.tileLayer(
|
||||
import.meta.env.VITE_MAP_TILE_URL ||
|
||||
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
|
||||
'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
||||
{
|
||||
maxZoom: 19,
|
||||
attribution:
|
||||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
|
||||
},
|
||||
)
|
||||
.on("tileerror", () => {
|
||||
.on('tileerror', () => {
|
||||
if (!cancelled)
|
||||
setError(
|
||||
"Mapa base indisponível. Os marcadores e a lista continuam acessíveis.",
|
||||
'Mapa base indisponível. Os marcadores e a lista continuam acessíveis.',
|
||||
);
|
||||
})
|
||||
.addTo(instance);
|
||||
@@ -65,29 +65,29 @@ export default function InteractiveInvestmentMap({
|
||||
)
|
||||
continue;
|
||||
const { properties: p } = point;
|
||||
const popup = document.createElement("div");
|
||||
const heading = document.createElement("strong");
|
||||
const popup = document.createElement('div');
|
||||
const heading = document.createElement('strong');
|
||||
heading.textContent = p.title;
|
||||
popup.append(heading);
|
||||
for (const text of [
|
||||
p.category,
|
||||
p.location,
|
||||
`Corredor: ${p.corridors?.join(", ") || "Não associado"}`,
|
||||
`Cluster: ${p.clusters?.join(", ") || "Não associado"}`,
|
||||
`Projecto âncora: ${p.projects?.join(", ") || "Não associado"}`,
|
||||
`Corredor: ${p.corridors?.join(', ') || 'Não associado'}`,
|
||||
`Cluster: ${p.clusters?.join(', ') || 'Não associado'}`,
|
||||
`Projecto âncora: ${p.projects?.join(', ') || 'Não associado'}`,
|
||||
]) {
|
||||
const line = document.createElement("p");
|
||||
line.textContent = text ?? "";
|
||||
const line = document.createElement('p');
|
||||
line.textContent = text ?? '';
|
||||
popup.append(line);
|
||||
}
|
||||
if (p.url.startsWith("/") && !p.url.startsWith("//")) {
|
||||
const link = document.createElement("a");
|
||||
if (p.url.startsWith('/') && !p.url.startsWith('//')) {
|
||||
const link = document.createElement('a');
|
||||
link.href = p.url;
|
||||
link.textContent = "Abrir ficha detalhada";
|
||||
link.textContent = 'Abrir ficha detalhada';
|
||||
popup.append(link);
|
||||
}
|
||||
const icon = L.divIcon({
|
||||
className: "",
|
||||
className: '',
|
||||
html: `<span style="display:grid;place-items:center;width:32px;height:32px;border-radius:50%;background:#065f46;color:white;border:2px solid white;font-weight:bold">${index + 1}</span>`,
|
||||
iconSize: [32, 32],
|
||||
iconAnchor: [16, 16],
|
||||
@@ -99,17 +99,21 @@ export default function InteractiveInvestmentMap({
|
||||
keyboard: true,
|
||||
})
|
||||
.bindPopup(popup)
|
||||
.on("click", () => setSelected(p.id))
|
||||
.on('click', () => setSelected(p.id))
|
||||
.addTo(instance);
|
||||
markers.current.set(p.id, marker);
|
||||
bounds.push([latitude, longitude]);
|
||||
}
|
||||
if (bounds.length) instance.fitBounds(bounds, { padding: [40, 40], maxZoom: 14 });
|
||||
if (bounds.length)
|
||||
instance.fitBounds(bounds, {
|
||||
padding: [40, 40],
|
||||
maxZoom: 14,
|
||||
});
|
||||
observer = new ResizeObserver(() => instance.invalidateSize());
|
||||
observer.observe(element.current);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setError("Não foi possível iniciar o mapa.");
|
||||
if (!cancelled) setError('Não foi possível iniciar o mapa.');
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
@@ -126,8 +130,8 @@ export default function InteractiveInvestmentMap({
|
||||
>
|
||||
<h2 className="text-xl font-bold">Mapa dos investimentos</h2>
|
||||
<p>
|
||||
Arraste para navegar; use +/− para ampliar. Pode seleccionar um investimento na
|
||||
lista para o localizar.
|
||||
Arraste para navegar; use +/− para ampliar. Pode seleccionar um
|
||||
investimento na lista para o localizar.
|
||||
</p>
|
||||
{error && <p role="status">{error}</p>}
|
||||
<div
|
||||
@@ -138,7 +142,9 @@ export default function InteractiveInvestmentMap({
|
||||
{points === null ? (
|
||||
<p role="status">A carregar localizações…</p>
|
||||
) : (
|
||||
<p role="status">{points.length} investimentos com coordenadas disponíveis.</p>
|
||||
<p role="status">
|
||||
{points.length} investimentos com coordenadas disponíveis.
|
||||
</p>
|
||||
)}
|
||||
<ul className="grid gap-2 md:grid-cols-2">
|
||||
{(points ?? []).map((p) => (
|
||||
@@ -146,8 +152,8 @@ export default function InteractiveInvestmentMap({
|
||||
key={p.properties.id}
|
||||
className={
|
||||
selected === p.properties.id
|
||||
? "rounded border-2 border-emerald-700 p-3"
|
||||
: "rounded border p-3"
|
||||
? 'rounded border-2 border-emerald-700 p-3'
|
||||
: 'rounded border p-3'
|
||||
}
|
||||
>
|
||||
<button
|
||||
@@ -155,9 +161,14 @@ export default function InteractiveInvestmentMap({
|
||||
className="text-left font-bold underline"
|
||||
aria-pressed={selected === p.properties.id}
|
||||
onClick={() => {
|
||||
const marker = markers.current.get(p.properties.id);
|
||||
const marker = markers.current.get(
|
||||
p.properties.id,
|
||||
);
|
||||
if (marker) {
|
||||
map.current?.setView(marker.getLatLng(), 14);
|
||||
map.current?.setView(
|
||||
marker.getLatLng(),
|
||||
14,
|
||||
);
|
||||
marker.openPopup();
|
||||
setSelected(p.properties.id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user